今天学习了下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. mac mysql cmd

    sudo /usr/local/mysql/support-files/mysql.server start sudo /usr/local/mysql/support-files/mysql.ser ...

  2. mysql 优化实例之索引创建

    mysql 优化实例之索引创建 优化前: pt-query-degist分析结果: # Query 23: 0.00 QPS, 0.00x concurrency, ID 0x78761E301CC7 ...

  3. Pycharm用Ctrl+鼠标滚轮调节代码字体大小

     File --> Setting --> Editor --> General --> 勾选Change font size (zoom) with Ctrl+Mouse W ...

  4. 批处理命令——call 和 start

    一.call命令总结 [1]call命令简介 学过汇编或C的朋友,肯定都知道call指令表示什么意思.其实,在这里它的意思也是一样的.在批处理脚本中,call命令用来从一个批处理脚本中调用另一个批处理 ...

  5. Linux的tmpfs文件系统

    转载:http://blog.csdn.net/wxwsixis/article/details/5752186 前几天发现服务器的内存(ram)和swap使用率非常低,于是就想这么多的资源,不用岂不 ...

  6. Spring Security笔记:Hello World

    本文演示了Spring Security的最最基本用法,二个页面(或理解成二个url),一个需要登录认证后才能访问(比如:../admin/),一个可匿名访问(比如:../welcome) 注:以下内 ...

  7. jquery 20161014

    jquery.fn.extend <!DOCTYPE html> <html> <head lang="en"> <meta charse ...

  8. 8-04流程控制语句BEGIN ..END

     流程控制语句: 是用来控制程序流程的语句. 常用的流程控制语句的分类: 顺序结构:BEGIN...END 分支结构: IF ..ELSE 或CASE ..END 循环结构:WHILE 顺序结构 语法 ...

  9. android中设置控件获得焦点 (转)

    android中,要使控件获得焦点,需要先setFocus,再requestFocus. 以Button为例:                 btn.setFocusable(true);      ...

  10. 【转】Linux下进程/程序网络带宽占用情况查看工具 -- NetHogs

    http://www.cnblogs.com/carbon3/p/5930803.html 之前VPS侦探曾经介绍过流量带宽相关的工具如:iftop.vnstat,这几个都是统计和监控网卡流量的.但是 ...