本文主要是介绍druid和hikariCP 数据库连接池,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
druid和hikariCP 数据库连接池
druid
首先需要导入druid的jar包
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream("src/com/jdbc/druid/druid.properties");
properties.load(fileInputStream);
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
String sql = "select * from student";
PreparedStatement statement =connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString("stuno"));
System.out.println(resultSet.getString("stuname"));
System.out.println("--------------------");
}
connection.close();
其中导入的数据properties文件内容如下
url=jdbc:mysql://localhost:3306/orcl
username=root
password=123456
其中配置参数都可以在druid中的gitHub中找到相应的参数
hikariCP
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/orcl");
config.setUsername("root");
config.setPassword("123456");
HikariDataSource ds = new HikariDataSource(config);
Connection connection = ds.getConnection();
String sql ="select * from student";
PreparedStatement statement =connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString("stuno"));
System.out.println(resultSet.getString("stuname"));
System.out.println("--------------------");
}
connection.close();
连接方法有很多种可以在GitHub中查看
这篇关于druid和hikariCP 数据库连接池的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!