2、创建C3P0连接池
Test
public void testC3P0 ( ) throws Exception {
//创建数据源
ComboPooledDataSource dataSource = new ComboPooledDataSource () ;
//设置数据库连接参数
dataSource.setDriverClass (“com.mysql.jdbc.Driver” ) ;
dataSource.setJdbcUrl (“jdbc:mysql 😕/localhost:3306/test” ) ;
datasource.setUser (“root”);
dataSource.setPassword (“root”);
//获得连接对象
Connection connection = dataSource.getConnection ();
system.out.println (connection) ;
创建Druid连接池
@Test
public void testDruid ( ) throws Exception {
//创建数据源
DruidDataSource dataSource = new DruidDatasource () ;
//设置数据库连接参数
dataSource.setDriverclassName (“com.mysql.jdbc .Driver”) ;
datasource.setUrl (“jdbc:mysql : //localhost:3306/test”) ;
datasource.setUsername (“root”) ;
datasource.setPassword (“root” ) ;
//获得连接对象
connection connection = dataSource.getConnection ( ) ;
System.out.println (connection) ;
)
3、提取jdbc.properties配置文件
jdbc .driver=com.mysql.jdbc.Driver
jdbc.url=jdbc :mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
4、读取jdbc.properties配置文件创建连接池
@Test
//测试手动创建 c3p0 数据源(加载properties配置文件)
public void test3() throws Exception {
//读取配置文件
ResourceBundle rb = ResourceBundle.getBundle(“jdbc”);
String driver = rb.getString(“jdbc.driver”);
String url = rb.getString(“jdbc.url”);
String username = rb.getString(“jdbc.username”);
String password = rb.getString(“jdbc.password”);
//创建数据源对象 设置连接参数
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
<