
package com.wyz.common; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class HikariPoolManager { private static HikariDataSource dataSource; private static final String DB_CONFIG_FILE = "src/com/wyz/common/jdbc.properties"; static { //读取jdbc配置文件 Properties props = new Properties(); try { InputStream in = new FileInputStream(new File(DB_CONFIG_FILE)); props.load(in); } catch (IOException e) { e.printStackTrace(); } //初始化HikariConfig配置 HikariConfig config = new HikariConfig(); config.setJdbcUrl(props.getProperty("jdbcUrl")); config.setUsername(props.getProperty("username")); config.setPassword(props.getProperty("password")); config.addDataSourceProperty("cachePrepStmts", props.getProperty("dataSource.cachePrepStmts")); config.addDataSourceProperty("prepStmtCacheSize", props.getProperty("dataSource.prepStmtCacheSize")); config.addDataSourceProperty("prepStmtCacheSqlLimit", props.getProperty("dataSource.prepStmtCacheSqlLimit")); config.addDataSourceProperty("useServerPrepStmts", props.getProperty("dataSource.useServerPrepStmts")); config.addDataSourceProperty("useLocalSessionState", props.getProperty("dataSource.useLocalSessionState")); config.addDataSourceProperty("rewriteBatchedStatements", props.getProperty("dataSource.rewriteBatchedStatements")); config.addDataSourceProperty("cacheResultSetMetadata", props.getProperty("dataSource.cacheResultSetMetadata")); config.addDataSourceProperty("cacheServerConfiguration", props.getProperty("dataSource.cacheServerConfiguration")); config.addDataSourceProperty("elideSetAutoCommits", props.getProperty("dataSource.elideSetAutoCommits")); config.addDataSourceProperty("maintainTimeStats", props.getProperty("dataSource.maintainTimeStats")); //初始化HikariDataSource dataSource = new HikariDataSource(config); } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } public static void main(String[] args) throws SQLException { Connection connection = HikariPoolManager.getConnection(); PreparedStatement ps = connection.prepareStatement("select * from user where username=? and password=?"); ps.setString(1,"zhangsan"); ps.setString(2,"123456"); ResultSet ret = ps.executeQuery(); while (ret.next()){ int id = ret.getInt("id"); String username = ret.getString("username"); String password = ret.getString("password"); System.out.println("id:"+id+",username:"+username+",password:"+password); } /** * ResultSetMetaData columns = ret.getMetaData(); int columnCount = columns.getColumnCount(); while (rs.next()) { Map<String, Object> resultMap = new HashMap<String, Object>(); // 将结果返回成Map,key为列表名,value为该字段的值 for (int j = 1; j <= columnNum; j++) { resultMap.put(columns.getColumnName(j), rs.getString(j)); } list.add(resultMap); } * * */ } }

jdbcUrl=jdbc:mysql://localhost:3306/test2?serverTimezone=UTC&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8 username=root password=wyz dataSource.cachePrepStmts=true dataSource.prepStmtCacheSize=250 dataSource.prepStmtCacheSqlLimit=2048 dataSource.useServerPrepStmts=true dataSource.useLocalSessionState=true dataSource.rewriteBatchedStatements=true dataSource.cacheResultSetMetadata=true dataSource.cacheServerConfiguration=true dataSource.elideSetAutoCommits=true dataSource.maintainTimeStats=false
上面是普通java的用法,如果要在servlet中使用,要用类加载器去加载配置文件,配置文件此时的相对路径相对的是当前类加载的根目录: