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 ...
随机推荐
- mongodb的查询语句学习摘要
看了些资料,对应只需要知道怎么查询和使用mongodb的我来说,这些足够啦. 左边是mongodb查询语句,右边是sql语句.对照着用,挺方便. db.users.find() select * fr ...
- Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
最近项目中页面比较复杂,springMVC传参过程中遇到这样一个错误:Could not instantiate bean class [java.util.List]: Specified clas ...
- 关于js实现分页效果的简单代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- python selenium中使用ddt进行数据驱动测试
- redis-3.2.5 make 报错
make[]: Entering directory `/usr/local/src/redis-/src' CC adlist.o In file included : zmalloc.h::: e ...
- Jetty官方文档翻译
最近在学习Jetty,没有找到合适的资料,所有只能看官方文档了,但是只有英文的,想着自己翻译着学也是学还不如把学习的过程放到网上,也可以给需要的人看,英文水平毕竟有限,也是用有道翻译着来的,不过也加了 ...
- java基础(四)
一.面向对象的三个基本特征: 1.封装,将对象的实现细节隐藏起来,并通过公共接口暴露相关功能: 2.继承,代码复用的表现,当子类继承父类后,子类作为一种特殊的父类,直接获得父类的属性和方法: 3.多态 ...
- 读取properties文件以及properties的用法
package cn.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties ...
- CSS3实现开门动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&qu ...