连接池概述
DBCP数据库连接池
dbcp.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb1
username=root
password=root
代码
package cn.tedu.jdbc.pool;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBCPDemo {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Properties p=new Properties();
p.load(new FileInputStream(new File(DBCPDemo.class.
getClassLoader().getResource("dbcp.properties").getPath())));
BasicDataSourceFactory factory=new BasicDataSourceFactory();
DataSource source = factory.createDataSource(p);
conn = source.getConnection();
ps = conn.prepareStatement("select * from exam");
rs = ps.executeQuery();
while (rs.next()){
System.out.println(rs.getInt("id")+","+rs.getString("name"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(rs!=null)
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
rs=null;
}
if(ps!=null)
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
ps=null;
}
if(conn!=null)
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
conn=null;
}
}
}
}
C3P0数据库连接池【不需要读取配置文件】
c3p0.properties
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/mydb1
c3p0.user=root
c3p0.password=root
c3p0工具类
public class C3P0Utiles {
private C3P0Utiles() {
}
private static ComboPooledDataSource source = new ComboPooledDataSource();
public static Connection getConnection() throws ClassNotFoundException, SQLException {
return source.getConnection();
}
public static void close(Connection conn, Statement stat, ResultSet rs) {
if (rs != null)
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
rs = null;
}
if (stat != null)
try {
stat.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
stat = null;
}
if (conn != null)
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
conn = null;
}
}
}
主方法
package cn.tedu.jdbc.pool;
import cn.tedu.jdbc.utiles.C3P0Utiles;
import cn.tedu.jdbc.utiles.JDBCUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class C3P0Demo {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
ComboPooledDataSource source=new ComboPooledDataSource();
try {
conn = C3P0Utiles.getConnection();
ps = conn.prepareStatement("select * from exam");
rs = ps.executeQuery();
while (rs.next()){
System.out.println(rs.getInt("id")+","+rs.getString("name"));
}
} catch (SQLException | ClassNotFoundException throwables) {
throwables.printStackTrace();
}finally {
C3P0Utiles.close(conn,ps,rs);
}
}
}
|