导入依赖
<dependencies>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
编写jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_wlp?serverTimezone=UTC
jdbc.user=root
jdbc.password=8888
编写测试类
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.testng.annotations.Test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
public class myTest {
@Test
public void test1() throws Exception {
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
String driver = rb.getString("jdbc.driver");
String url = rb.getString("jdbc.url");
String user = rb.getString("jdbc.user");
String password = rb.getString("jdbc.password");
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(user);
dataSource.setPassword(password);
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement("select * from user_tab");
ResultSet rs = ps.executeQuery();
while (rs.next()){
System.out.print(rs.getInt("id")+" ");
System.out.print(rs.getString("user")+" ");
System.out.println(rs.getString("pwd"));
}
}
}
使用Spring配置DataSource
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_wlp?serverTimezone=UTC"/>
<property name="user" value="root"/>
<property name="password" value="8888"/>
</bean>
</beans>
测试
@Test
public void test2() throws SQLException {
ApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml");
ComboPooledDataSource dataSource = context.getBean("dataSource", ComboPooledDataSource.class);
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement("select * from user_tab");
ResultSet rs = ps.executeQuery();
while (rs.next()){
System.out.print(rs.getInt("id")+" ");
System.out.print(rs.getString("user")+" ");
System.out.println(rs.getString("pwd"));
}
}
优化Spring配置
<?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">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>