①提高程序性能
②事先实例化数据源,初始化部分连接资源
③使用连接资源时从数据源中获取
④使用完毕后将连接资源归还给数据源
mysql
mysql-connector-java
5.1.49
runtime
junit
junit
4.11
test
com.alibaba
druid
1.1.24
c3p0
c3p0
0.9.1.2
如果不加载配置文件,有很多具体的数据库参数信息都设置在代码中,就可以说这些数据库参数信息与当前Java文件耦合在了一起,如果后期我们更换数据库,就需要找到源码修改这些参数信息,项目还要重新部署上线,因此这种方式不可取,而加载配置文件的方式就可以很好地解决这种问题,将这些数据库参数信息抽取出来放到一个配置文件中,以后即使更换数据库,不需要找到源码,只需要在配置文件中修改相应的参数信息即可,这也就使数据库参数信息与Java文件解耦了,降低了耦合性。
@Test
//测试手动创建 c3p0 数据源
public void test1() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/sn");
dataSource.setUser("root");
dataSource.setPassword("123");
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mchange.v2.c3p0.impl.NewProxyConnection@63a65a25
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sn
jdbc.username=root
jdbc.password=123
@Test
//测试手动创建 c3p0 数据源(加载properties配置文件)
public void test2() throws Exception {
//1. 读取配置文件
//下面的参数"jdbc"是上面创建的jdbc.properties配置文件的前半部分
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");
//2. 创建数据源对象,设置连接参数
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
//3. 获取资源
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mchange.v2.c3p0.impl.NewProxyConnection@6dbb137d
@Test
//测试手动创建 druid 数据源
public void test3() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/sn");
dataSource.setUsername("root");
dataSource.setPassword("123");
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mysql.jdbc.JDBC4Connection@517cd4b
@Test
//测试手动创建 druid 数据源(加载properties配置文件)
public void test4() throws Exception {
//下面的参数"jdbc"是上面创建的jdbc.properties配置文件的前半部分
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");
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mysql.jdbc.JDBC4Connection@679b62af
org.springframework
spring-context
5.2.9.RELEASE
@Test
//测试Spring容器产生 c3p0 数据源对象
public void test5() throws Exception{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = app.getBean(DataSource.class);
// DataSource dataSource = (DataSource) app.getBean("dataSource");//这种方式也行
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mchange.v2.c3p0.impl.NewProxyConnection@1c9b0314
@Test
//测试Spring容器产生 druid 数据源对象
public void test7() throws Exception{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource1 = (DataSource) app.getBean("dataSource1");
Connection connection = dataSource1.getConnection();
System.out.println(connection);
connection.close();
}
运行结果:
com.mysql.jdbc.JDBC4Connection@5082d622
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sn
jdbc.username=root
jdbc.password=123
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">