1.C3P0是什么?
C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。使用它的开源项目有Hibernate、Spring等。
2.使用C3P0:
ComboPooledDataSource dataSource = new ComboPooledDataSource();//创建c3p0对象
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Dbutilsr {
public static void main(String[] args) {
PreparedStatement pstmt;
ResultSet rs = null;
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/new_schema");
dataSource.setUser("root");
dataSource.setPassword("your-password");
Connection conn = dataSource.getConnection(); //连接数据库
String sql = "select * from user";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("password"));
}
} catch (PropertyVetoException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}}
效果:
2.c3p0配置文件:
打开:c3po的index.html
有:下面代码: c3p0 will look for an XML configuration file in its classloader’s resource path under the name “/c3p0-config.xml”.// c3po将会从xml配置文件读取数据:默认文件名: c3p0-config.xml 所以要创建c3p0-config.xml这个文件,并粘贴下面代码
c3p0-config.xml代码xml里面
<property name="driverClass">com.mysql.jdbc.Driver</property>
name=“driverClass”:等会有一个c3po对象类:会读取这里driverClass 是 这个com.mysql.jdbc.Driver c3po对象类:就可以不用写这个(数据库驱动了) dataSource.setDriverClass(“com.mysql.jdbc.Driver”); 其他类似
<default-config>
<named-config name="oracel"> //name="oracel",切换数据
(c3p0-config.xml代码)
<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/new_schema</property>
<property name="user">root</property>
<property name="password">your-password</property>
</default-config>
<!-- This app is massive! -->
<named-config name="oracel"> //name="oracel",切换数据
<property name="DriverClass">com.mysql.jdbc.Driver</property>
<property name="JdbcUrl">jdbc:mysql://localhost:3306/new_schema</property>
<property name="User">root</property>
<property name="Password">your-password</property>
</named-config>
</c3p0-config>
实现类代码:
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Dbutilsr {
public static void main(String[] args) {
PreparedStatement pstmt;
ResultSet rs = null;
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
// dataSource.setDriverClass("com.mysql.jdbc.Driver");
// dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/new_schema");
// dataSource.setUser("root");
// dataSource.setPassword("your-password");
Connection conn = dataSource.getConnection(); //连接数据库
String sql = "select * from user";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("password"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}}
效果图:
注意: ComboPooledDataSource dataSource = new ComboPooledDataSource(“oracel”); //参数oracel c3p0对象:读取:xml里面的这个 named-config name="oracel"
<!-- This app is massive! -->
<named-config name="oracel"> //name="oracel",切换数据
<property name="DriverClass">com.mysql.jdbc.Driver</property>
<property name="JdbcUrl">jdbc:mysql://localhost:3306/new_schema</property>
<property name="User">root</property>
<property name="Password">your-password</property>
</named-config>
也运行成功了
学习中遇到的问题:
c3p0 版本不合适,出现错误 和jdbc驱动:mysql-connection-java-8.0.27
配置这个:c3p0-0.9.5.5jar 错误 配置这个:c3p0-0.9.1.2jar 成功 c3p0-0.9.1.2jar ;下载链接:
https://mvnrepository.com/artifact/c3p0/c3p0/0.9.1.2
这个: 可以点击:jar
|