KingbaseES数据库配置Hikari数据源

Hikari是一个高性能的数据库连接池,它是Spring Boot 2.x中的默认数据源。

一、下载驱动

打开下面网址:选择对应平台的jdbc驱动程序。

人大金仓-成为世界卓越的数据库产品与服务提供商 (kingbase.com.cn)

这里以x86平台为例:

下载完成后目录里面包含以下文件:根据项目的JDK版本选择对应的驱动

--kingbase8-8.6.0.jar>= jdk 1.8
kingbase8-8.6.0.jar    
--kingbase8-8.6.0.jar = jdk 1.6
kingbase8-8.6.0.jre6.jar
--kingbase8-8.6.0.jar = jdk 1.7
kingbase8-8.6.0.jre7.jar
--kingbase8-8.6.0.jar>= jdk 1.8
postgresql-42.2.9.jar
--kingbase8-8.6.0.jar = jdk 1.6
postgresql-42.2.9.jre6.jar
--kingbase8-8.6.0.jar = jdk 1.7
postgresql-42.2.9.jre7.jar

二、上传驱动jar包到maven仓库

以本地仓库为例:

--执行命令安装驱动到本地仓库
mvn install:install-file -Dfile=C:\DbTools\Interface\kingbase8-8.6.0.jar -DgroupId=com.kingbase8 -DartifactId=kingbase8 -Dversion=8.6.0 -Dpackaging=jar

安装过程:

D:\JAVA\jdk1.8.0_333\bin\java.exe -Dmaven.multiModuleProjectDirectory=C:\Users\realGuob\Desktop\Work\code\Hikari -Djansi.passthrough=true "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\lib\idea_rt.jar=58778:C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2022.3.3 install:install-file -Dfile=C:\DbTools\Interface\kingbase8-8.6.0.jar -DgroupId=com.kingbase8 -DartifactId=kingbase8 -Dversion=8.6.0 -Dpackaging=jar
[WARNING] 
[WARNING] Some problems were encountered while building the effective settings
[WARNING] Unrecognised tag: 'mirror' (position: START_TAG seen ...0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">\r\n    <mirror>... @5:13)  @ C:\Users\realGuob\.m2\settings.xml, line 5, column 13
[WARNING] 
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< org.example:Hikari >-------------------------
[INFO] Building Hikari 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ Hikari ---
[INFO] Installing C:\DbTools\Interface\kingbase8-8.6.0.jar to C:\Users\realGuob\.m2\repository\com\kingbase8\kingbase8\8.6.0\kingbase8-8.6.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.685 s
[INFO] Finished at: 2023-05-31T18:17:48+08:00
[INFO] ------------------------------------------------------------------------

进程已结束,退出代码0

没有报错说明已成功安装,安装成功后建议更新下maven索引。

三、配置pom.xml

添加如下依赖:

<dependencies>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>4.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.kingbase8</groupId>
            <artifactId>kingbase8</artifactId>
            <version>8.6.0</version>
        </dependency>
</dependencies>

添加完成后刷新或者使用maven重新加载下项目。

四、Hikari数据源测试

1.创建HikariConfig对象设置数据库的相关属性:用户名、密码、端口号、数据库名称、数据源对象等

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class DBUtil {
    static final HikariDataSource DS;
    static {
        HikariConfig config = new HikariConfig();
        config.setAutoCommit(true);
        config.setDataSourceClassName("com.kingbase8.ds.KBSimpleDataSource");
        config.setUsername("system");
        config.setPassword("system");
        config.addDataSourceProperty("serverName", "192.168.10.43");
        config.addDataSourceProperty("portNumber", 5432);
        config.addDataSourceProperty("databaseName", "test");
        DS = new HikariDataSource(config);
    }
    public static void close() {
        DS.close();
    }
}

2.测试创建的数据源对象:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
    public static void main(String[] args) {
        try (Connection conn = DBUtil.DS.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("select * from tb limit 10")) {
            while (rs.next()) {
                System.out.println(rs.getLong("id") + "\t" + rs.getDate("pdate") + "\t" + rs.getString("info"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

运行测试用例结果输出:

D:\JAVA\jdk1.8.0_333\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\lib\idea_rt.jar=65176:C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\JAVA\jdk1.8.0_333\jre\lib\charsets.jar;D:\JAVA\jdk1.8.0_333\jre\lib\deploy.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\dnsns.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\jaccess.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\localedata.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\nashorn.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\sunec.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk1.8.0_333\jre\lib\ext\zipfs.jar;D:\JAVA\jdk1.8.0_333\jre\lib\javaws.jar;D:\JAVA\jdk1.8.0_333\jre\lib\jce.jar;D:\JAVA\jdk1.8.0_333\jre\lib\jfr.jar;D:\JAVA\jdk1.8.0_333\jre\lib\jfxswt.jar;D:\JAVA\jdk1.8.0_333\jre\lib\jsse.jar;D:\JAVA\jdk1.8.0_333\jre\lib\management-agent.jar;D:\JAVA\jdk1.8.0_333\jre\lib\plugin.jar;D:\JAVA\jdk1.8.0_333\jre\lib\resources.jar;D:\JAVA\jdk1.8.0_333\jre\lib\rt.jar;D:\app\virtual\product\12.2.0\dbhome_1\jdbc\lib\ojdbc8.jar;C:\Users\realGuob\Desktop\Work\code\Hikari\target\test-classes;C:\Users\realGuob\Desktop\Work\code\Hikari\target\classes;C:\Users\realGuob\.m2\repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;C:\Users\realGuob\.m2\repository\com\kingbase8\kingbase8\8.6.0\kingbase8-8.6.0.jar;C:\Users\realGuob\.m2\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\Users\realGuob\.m2\repository\org\slf4j\slf4j-simple\1.7.30\slf4j-simple-1.7.30.jar Test
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
1    2023-01-01    c3b6f7eeb6
2    2023-01-01    2a7c3bf29a
3    2023-01-01    a4c40621c7
4    2023-01-01    422733f11f
5    2023-01-01    ce3f7bf0b7
6    2023-01-01    fa14aba9aa
7    2023-01-01    bdf13c7e1a
8    2023-01-01    02f2c2e404
9    2023-01-01    e61c473905
10    2023-01-01    20e44227b2

进程已结束,退出代码0
posted @ 2023-09-18 16:17  KINGBASE研究院  阅读(297)  评论(0编辑  收藏  举报