今天学习了下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. RobotFrameWork(五)控制流之if语句——Run Keyword If

    5.1 语句简介 robotframework中的if语句是使用关键字Run Keyword If来代替的 Run Keyword If 函数释义:如果给出的判断条件满足,就执行给出的关键字. 函数结 ...

  2. 转一篇介绍Web session概念的文章

    说的非常好,如果你对web中的session概念不太清楚的话,一定要去看看,可能需要FQ才能阅读:http://fred-zone.blogspot.com/2014/01/web-session.h ...

  3. secureCRT会话导入到xshell中的方法

    官方给出了一个工具,sessionimporter.exe 不过软件有点老了,导入的会话路径和xshell默认的会话路径不一致,导致导入后,xshell没有导入的会话信息 sessionimporte ...

  4. MySQL学习笔记(1/2)

    数据库的学习要求:1.为项目设计表:2.使用SQL(Structure Query Language)语句(SQL编程).其他的都可以使用工具完成. SQL: DDL:创建库.创建表 DML:对数据的 ...

  5. node在centos下的安装

    CentOS 下安装 Node.js 1.下载源码,你需要在https://nodejs.org/en/download/下载最新的Nodejs版本,本文以v4.6.2为例: cd /usr/loca ...

  6. MyBatis学习(三)

    输入和输出映射 resultType 指定输出结果的类型(pojo.简单类型.hashmap等),将sql查询结果映射为java对象 . 注意:sql查询的列名要和resultType指定pojo的属 ...

  7. 【转】算法杂货铺——k均值聚类(K-means)

    k均值聚类(K-means) 4.1.摘要 在前面的文章中,介绍了三种常见的分类算法.分类作为一种监督学习方法,要求必须事先明确知道各个类别的信息,并且断言所有待分类项都有一个类别与之对应.但是很多时 ...

  8. Ubuntu菜鸟入门(三)—— 无用软件卸载,wps等常用软件安装

    一  移除不需要的软件 sudo apt-get remove libreoffice-common sudo apt-get remove unity-webapps-common sudo apt ...

  9. 自定义Toast和RatingBar的简单用例

    Toast是一个包含用户点击消息.Toast类会帮助你创建和显示这些.Android中的Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动 向右划动五角星增加 单击按钮显示自 ...

  10. Linux学习笔记(4)-远程登录

    根据网上的那些说法,如Linux服务器假设在外地(新疆),和程序员工作的环境(北京)相距太远,那么每次出问题都要出差跑到现场去调试的话,那就太烦人了. 所以,人们开发出了一种远程登录的手段,可以让程序 ...