今天学习了下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操作数据库的更多相关文章

  1. JavaWeb学习记录(七)——MVC操作数据库增删改查与分页功能

    一.分页工具类 package blank.util;import java.util.List; import org.springframework.jdbc.core.JdbcTemplate; ...

  2. python学习笔记(九):操作数据库

    我们在写代码的时候,经常会操作数据库,增删改查,数据库有很多类型,关系型数据库和非关系数据库,这里咱们介绍一下python怎么操作mysql.redis和mongodb. 一.python操作mysq ...

  3. PHP学习笔记(11)PHP操作数据库

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. shell脚本编程学习笔记(四)shell操作数据库

    一.数据库基本操作 1)登录mysql服务器:mysql -u root -p 密码 2)查看数据库:show databases 3)查看表:show tales from db; 4)查看表结构: ...

  5. python学习 —— python3简单使用pymysql包操作数据库

    python3只支持pymysql(cpython >= 2.6 or >= 3.3,mysql >= 4.1),python2支持mysqldb. 两个例子: import pym ...

  6. golang学习之beego框架配合easyui实现增删改查及图片上传

    golang学习之beego框架配合easyui实现增删改查及图片上传 demo目录: upload文件夹主要放置上传的头像文件,main是主文件,所有效果如下: 主页面: 具体代码: <!DO ...

  7. Python Tutorial 学习(八)--Errors and Exceptions

    Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...

  8. SVG 学习<八> SVG的路径——path(2)贝塞尔曲线命令、光滑贝塞尔曲线命令

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  9. jquery学习笔记(二):DOM元素操作

    内容来自[汇智网]jquery学习课程 2.1 元素属性操作 1.获取元素的属性 语法:attr(name) 参数name表示属性的名称 2.设置元素的属性 单个属性设置语法:attr(key,val ...

随机推荐

  1. CI 笔记一

    CodeIgniter 说明 CodeIgniter 是为PHP 开发人员提供的一套Web 应用程序工具包.它的目标是能 够让你比从零开始更加快速的完成项目,它提供了一套丰富的的类库来满足我们日常 的 ...

  2. Java设计模式之结构型模式

    结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式. 一.适配器模式: 意图: 将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原本由于接 ...

  3. 【目录】Leetcode

    Leetcode 1.动态规划 Palindrome Partitioning II(hard) ☆ Distinct Subsequences(hard) Edit Distance (hard) ...

  4. gcd推导

    欧几里得算法有性质: gcd(a, b)=gcd(b, a%b); 那么如何证明呢~ 法1: 我们先假设其成立并且有 gcd(a, b)=gcd(b, a%b)=d; a=k*b+c即a%b=c(我们 ...

  5. FP-growth高效频繁项集发现

    FP-growth 算法优缺点: 优点:一般快于Apriori 缺点:实现比较困难,在某些数据上性能下降 适用数据类型:标称型数据 算法思想: FP-growth算法是用来解决频繁项集发现问题的,这个 ...

  6. html学习第二天—— 第七章——CSS样式基本知识

    外部式css样式,写在单独的一个文件中外部式css样式(也可称为外联式)就是把css代码写一个单独的外部文件中,这个css样式文件以“.css”为扩展名,在<head>内(不是在<s ...

  7. js的回调函数 一些例子

    这边用bootstrap 3.0的  上传控件做例子 下面是上传控件的一段完整的 js 操作 代码. <!-- 上传缩略图控件配置 --><script> // 定义这四个全局 ...

  8. win8 系统安装node环境记录

    原先我是用win7环境安装node很方便,到了win8系统突然变了,让我顿时困惑了一段时间,但还是被我找到方式解决了,记录一下解决方案: 首先在网上看了一些资料说win8下安装node环境会出错,但我 ...

  9. 清华学堂 Range

    Descriptioin Let S be a set of n integral points on the x-axis. For each given interval [a, b], you ...

  10. eclipse安装maven

    maven3 安装: 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目, 首先去官网下载 Maven: 下载完成之后将其解压,解压后的文件夹重命 ...