vert.x学习(八),用JDBCClient配合c3p0操作数据库
今天学习了下vert.x的JDBCClient,我这里将今天的学习笔记记录下来。这次学习中使用了c3p0。
用使用JDBCClient和c3p0得现在pom.xml文件里面导入对应的依赖,下面贴出xml文件中的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.javafm</groupId>
<artifactId>vertx.vertxworld</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-templ-thymeleaf</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-jdbc-client</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置好依赖后,就可以编写我们的代码了,创建一个DataAccess.java文件,写入代码
package com.javafm.vertx.helloworld; import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.jdbc.JDBCClient; /**
* Created by lemontea <36634584@qq.com> on 16-12-23.
*/
public class DataAccess {
private static DataAccess dataAccess;
private static JDBCClient jdbcClient;
private static JsonObject config; static {
config = new JsonObject();
config.put("url", "jdbc:mysql://localhost:3306/test");
config.put("driver_class", "com.mysql.jdbc.Driver");
config.put("user", "root");
config.put("password", "password");
} public static DataAccess create(Vertx vertx) {
if (dataAccess == null) {
synchronized (DataAccess.class) {
if (dataAccess == null) {
dataAccess = new DataAccess();
dataAccess.init(vertx);
}
}
}
return dataAccess;
} private void init(Vertx vertx) {
jdbcClient = JDBCClient.createShared(vertx, config);
} public JDBCClient getJDBCClient() {
return jdbcClient;
}
}
编写测试代码,查询test表,并输入索引 0、1位置的数据:
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
DataAccess dataAccess = DataAccess.create(vertx);
dataAccess.getJDBCClient().getConnection(res -> {
if (res.succeeded()) {
SQLConnection conn = res.result();
conn.query("SELECT * FROM `test`", res2 -> {
ResultSet rs = res2.result();
rs.getResults().forEach(e -> {
System.out.println(e.getInteger(0) + " " + e.getString(1));
});
conn.close();
});
} else {
System.out.println(res.cause());
}
});
}
运行这个main方法,会在idea控制台输出结果:

原创文章,转载请注明出处。
vert.x学习(八),用JDBCClient配合c3p0操作数据库的更多相关文章
- JavaWeb学习记录(七)——MVC操作数据库增删改查与分页功能
一.分页工具类 package blank.util;import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; ...
- python学习笔记(九):操作数据库
我们在写代码的时候,经常会操作数据库,增删改查,数据库有很多类型,关系型数据库和非关系数据库,这里咱们介绍一下python怎么操作mysql.redis和mongodb. 一.python操作mysq ...
- PHP学习笔记(11)PHP操作数据库
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- shell脚本编程学习笔记(四)shell操作数据库
一.数据库基本操作 1)登录mysql服务器:mysql -u root -p 密码 2)查看数据库:show databases 3)查看表:show tales from db; 4)查看表结构: ...
- python学习 —— python3简单使用pymysql包操作数据库
python3只支持pymysql(cpython >= 2.6 or >= 3.3,mysql >= 4.1),python2支持mysqldb. 两个例子: import pym ...
- golang学习之beego框架配合easyui实现增删改查及图片上传
golang学习之beego框架配合easyui实现增删改查及图片上传 demo目录: upload文件夹主要放置上传的头像文件,main是主文件,所有效果如下: 主页面: 具体代码: <!DO ...
- Python Tutorial 学习(八)--Errors and Exceptions
Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...
- SVG 学习<八> SVG的路径——path(2)贝塞尔曲线命令、光滑贝塞尔曲线命令
目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...
- jquery学习笔记(二):DOM元素操作
内容来自[汇智网]jquery学习课程 2.1 元素属性操作 1.获取元素的属性 语法:attr(name) 参数name表示属性的名称 2.设置元素的属性 单个属性设置语法:attr(key,val ...
随机推荐
- 曲线救国:IIS7集成模式下如何获取网站的URL
如果我们在Global中的Application_Start事件中访问HttpContext.Current.Request对象,如: protected void Application_Start ...
- php上传图片文件常用的几个方法
1. 前台 <form class="add-form" method="post" action="/person/save" en ...
- s:select下拉框validation验证
S:select下拉框验证: <td colspan="5"> <s:select name="vo.typeVO.corp" list=&q ...
- AJAX里,使用XML返回数据类型,实现简单下拉列表
XML:可扩展标记语言 HTML:超文本标记语言 标签:<标签名></标签名> 特点: 1.必须要有一个根 2.标签名自定义 3.对大小写敏感 4.有开始就要有结束 5.同一级 ...
- mac 安装nginx
首先准备工作,打开mac终端 1.安装brew 输入命令 curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar x ...
- iOS 编码转换
- (NSString *)SaveFileToDocuments:(NSString *)url { // NSString *url = @"http://172.28.250.70/a ...
- python--基础学习(五)参数位置传递、关键字传递、包裹传递及解包裹
python系列均基于python3.4环境 1.位置传递和关键字传递 代码示例 #位置传递 def fun(a,b,c): print("a: {0}, b: {1}, c: {2}&qu ...
- Android 某些配置记录
1, system image 大小配置: devices/intel/baytrail/ffrd8/BoardConfig.mk : BOARD_SYSTEMIMAGE_PARTITION_SIZ ...
- mysql explain用法
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了,如: explai ...
- Epub基础知识介绍
转载自:http://www.cnblogs.com/linlf03/archive/2011/12/13/2286218.html 一.什么是epub epub是一个完全开放和免费的电子书标准.它可 ...