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中查看