常见的数据源连接池:DBCP、C3P0、BoneCP、Druid等
1、C3P0
①导入数据源的坐标和数据库驱动坐标
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
②创建连接池
@Test
public void test1() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://172.16.90.174:3306/dbName");
dataSource.setUser("root");
dataSource.setPassword("123456");
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
1、Druid
①导入数据源的坐标和数据库驱动坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
②创建连接池
@Test
public void test2() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://172.16.90.174:3306/dbName");
dataSource.setUsername("root");
dataSource.setPassword("123456");
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
配置文件提取(jdbc.properties)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://172.16.90.174:3306/dbName
jdbc.username=root
jdbc.password=123456
①代码方式
@Test
public void test3() throws Exception {
ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc");
String driver = resourceBundle.getString("jdbc.driver");
String url = resourceBundle.getString("jdbc.url");
String username = resourceBundle.getString("jdbc.username");
String password = resourceBundle.getString("jdbc.password");
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
②配置文件方式(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 指定连接数据库的驱动-->
<property name="driverClass" value="${jdbc.driver}"/>
<!-- 指定连接数据库的URL-->
<property name="jdbcUrl" value="${jdbc.url}"/>
<!-- 指定连接数据库的用户名-->
<property name="user" value="${jdbc.username}"/>
<!-- 指定连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
③测试
@Test
public void test4() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ComboPooledDataSource dataSource = (ComboPooledDataSource) context.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
网友评论