一丶模拟连接池获取连接:
package jdbc_util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
//模式连接池的操作
public class JdbcUtil02 {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
private static int Normal = 10; //初始化连接数
private static int MAX = 20; //最大的连接数
private static int MIN = 5; //最小的连接数
@SuppressWarnings("unused")
private static int NUM = 0; //当前连接数
//使用集合 模式连接池
private static List<Connection> pool = new ArrayList<Connection>();
//初始化连接池
static{
//读取参数
Properties p = new Properties();
try {
FileInputStream fis = new FileInputStream(new File("src//jdbc.properties"));
p.load(fis);
driver = p.getProperty("driver");
url = p.getProperty("url");
username = p.getProperty("username");
password = p.getProperty("password");
//加载驱动
Class.forName(driver);
//初始化连接池
for(int i = 0; i < Normal; i++){
pool.add(createConnection());
NUM++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//从数据库中获取物理链接
private static Connection createConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//获取链接的方法
public static Connection getConnection(){
if(pool.size() > MIN){
return pool.remove(0);
}else{
if(pool.size() <= MAX){
Connection conn = createConnection();
NUM++;
return conn;
}else{
throw new RuntimeException("链接已经用完,请等待...");
}
}
}
public static void close(Connection conn,Statement stat, ResultSet rs){
if(conn != null){
//关闭 实际上是将连接 放回到连接池中
pool.add(conn);
}
try {
if(stat != null){
stat.close();
}
if(rs != null){
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
properties文件的情况:
二丶使用C3P0获取连接
1.首先加载C3P0的驱动包,
2.再将C3P0的配置文件放在src的目录下,
3.改配置文件里面的密码,和需要访问数据库的名字
代码如下:
package jdbc_util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcUtil03 {
private static DataSource ds ; //数据源
//加载驱动
static{
//读配置文件
ds = new ComboPooledDataSource();
}
public static Connection getConnection(){
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,Statement stat,ResultSet rs){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stat != null){
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
三丶最终的JDBCUtil工具类
需要加载的jar的包有一下: