环境:jdk 1.8 maven postgreSql11
直接上代码:
maven引入jar包
compile"org.postgresql:postgresql:42.2.6"
注:如果jdk的版本低于1.7,使用低版本的jar包,42.2.6不支持低版本的j低于1.7的版本。
提供HikariDataSource默认使用静态块的方式提供。
package utils;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
public class DBUtil {
private static final HikariDataSourceDS;
static {
HikariConfig config =new HikariConfig();
config.setAutoCommit(true);
config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
config.setUsername("postgresql");
config.setPassword("postgresql");
config.addDataSourceProperty("serverName", "localhost");
config.addDataSourceProperty("portNumber", 5432);
config.addDataSourceProperty("databaseName","test");
DS =new HikariDataSource(config);
}
public static void close() {
DS.close();
}
}
如果配置完成后如果报 如下错误可以参考我的另一篇日记:PostgreSql连接HikariDataSource提示SSL关闭处理
![](https://img.haomeiwen.com/i14955938/239fc62fb892a5f0.png)
网友评论