Druid和HikariCP的基本测试
【摘要】
Druid和HikariCP的配置方法
DruidHikariCPTest
Druid
public class Druid {
/*
# 配置数据库的连接参数
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mxd
username=root
p...
Druid
public class Druid {
/*
# 配置数据库的连接参数
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mxd
username=root
password=**
# 配置连接池的参数
initialSize=5
maxActive=10
maxWait=3000
maxIdle=6
minIdle=3
*/ public static DruidDataSource dataSource; //1.初始化Druid连接池 static { //第二种方式:使用软编码通过配置文件初始化DBCP try { Properties properties = new Properties(); //通过类加载器加载配置文件 InputStream inputStream = Druid.class.getClassLoader().getResourceAsStream("db.properties"); properties.load(inputStream); dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } //2.获取连接 public static Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } //3.关闭连接 public static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } }
}
HikariCP
public class HikariCP {
/*
driverClassName=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mxd
dataSource.user=root
dataSource.password=
#jdbc:mysql://localhost:3306/mxd
#dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
#dataSource.databaseName=mxd
#dataSource.portNumber=3306
#dataSource.serverName=localhost
*/ public static HikariDataSource dataSource=null; static { try { //软连接方法 HikariConfig config=new HikariConfig("/HikariCp.properties"); //硬连接方法 /* HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mxd");//mysql config.setDriverClassName("com.mysql.cj.jdbc.Driver"); config.setUsername("root"); config.setPassword("@#MA847547125**");*/ dataSource=new HikariDataSource(config); } catch (Exception e) { e.printStackTrace(); } } //2.获取连接 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } //3.关闭连接 public static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } }
}
Test
import DBUtils.Druid;
import DBUtils.HikariCP;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtilsTest { @Test public void DruidTest() throws SQLException { String sql="select * from person"; Connection conn=Druid.getConnection(); PreparedStatement preparedStatement=conn.prepareStatement(sql); ResultSet resultSet=preparedStatement.executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getInt(1)+resultSet.getString(2)+resultSet.getInt(3)); } Druid.closeAll(conn,preparedStatement,resultSet); } @Test public void HikariCPTest() throws SQLException { System.out.println(1); String sql="select * from person"; System.out.println(2); Connection connection= HikariCP.getConnection(); PreparedStatement preparedStatement=connection.prepareStatement(sql); ResultSet resultSet=preparedStatement.executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getInt(1)+resultSet.getString(2)+resultSet.getInt(3)); } HikariCP.closeAll(connection,preparedStatement,resultSet); }
}
文章来源: blog.csdn.net,作者:East Horse,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_44613138/article/details/105615934
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)