hikaricp mysql_Java操作MySQL--使用HikariCP

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);

}

*

**/}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值