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.0modelVersion>

    <groupId>com.javafmgroupId>
    <artifactId>vertx.vertxworldartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>io.vertxgroupId>
            <artifactId>vertx-coreartifactId>
            <version>3.3.3version>
        dependency>
        <dependency>
            <groupId>io.vertxgroupId>
            <artifactId>vertx-webartifactId>
            <version>3.3.3version>
        dependency>
        <dependency>
            <groupId>io.vertxgroupId>
            <artifactId>vertx-web-templ-thymeleafartifactId>
            <version>3.3.3version>
        dependency>
        <dependency>
            <groupId>io.vertxgroupId>
            <artifactId>vertx-jdbc-clientartifactId>
            <version>3.3.3version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.38version>
        dependency>
        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
            <version>0.9.1.2version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.6.0version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                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 <[email protected]> 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张图片

 

原创文章,转载请注明出处。

你可能感兴趣的:(vert.x学习(八),用JDBCClient配合c3p0操作数据库)