packagecom.wyz.common;importcom.zaxxer.hikari.HikariConfig;importcom.zaxxer.hikari.HikariDataSource;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;import java.sql.*;importjava.util.Properties;public classHikariPoolManager {private staticHikariDataSource dataSource;private static final String DB_CONFIG_FILE = "src/com/wyz/common/jdbc.properties";static{//读取jdbc配置文件
Properties props = newProperties();try{
InputStream in= new FileInputStream(newFile(DB_CONFIG_FILE));
props.load(in);
}catch(IOException e) {
e.printStackTrace();
}//初始化HikariConfig配置
HikariConfig config = newHikariConfig();
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 = newHikariDataSource(config);
}public static Connection getConnection() throwsSQLException {returndataSource.getConnection();
}public static void main(String[] args) throwsSQLException {
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 resultMap = new HashMap();
// 将结果返回成Map,key为列表名,value为该字段的值
for (int j = 1; j <= columnNum; j++) {
resultMap.put(columns.getColumnName(j), rs.getString(j));
}
list.add(resultMap);
}
*
**/}
}