注意:使用装饰者模式增强Connection的close方法
1. 测试部分
package com.it.jdbc.TestDataSoure;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.management.RuntimeErrorException;
import org.junit.Test;
import com.it.jdbc.DataSoure.MyDataSoure;
import com.it.jdbc.utils.JDBCUtils_V3;
public class TestMyDataSoure {
/**
* 添加用户
* 使用改造Connection
*/
@Test
public void testAdduser1() {
Connection conn = null;
PreparedStatement pstmt = null;
// 自定义连接池对象
MyDataSoure datasoure = new MyDataSoure();
try {
// 从连接池获得链接
conn = datasoure.getConnection();
String sql = "insert into use1 value(null,?,?)";
pstmt =conn.prepareStatement(sql);
pstmt.setString(1, "何一");
pstmt.setString(2, "李二");
int rows = pstmt.executeUpdate();
if (rows > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils_V3.release(conn, pstmt, null);
}
}
}
2.MyConnection
package com.it.jdbc.DataSoure;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
//1,实现同一个接口
public class MyConnection implements Connection{
//3,定义一个变量
private Connection conn;
private LinkedList<Connection> pool;
//写构造方法(参数使用了面向对象-多态)
public MyConnection (Connection conn,LinkedList<Connection> pool){
this.conn =conn;
this.pool=pool;
}
//增强的方法
@Override
public void close() throws SQLException {
// TODO Auto-generated method stub
pool.add(conn);
}
@Override
//此方法必须覆盖,否则空指针异常
public PreparedStatement prepareStatement(String sql) throws SQLException {
// TODO Auto-generated method stub
return conn.prepareStatement(sql);
}
}
3.MyDataSoure
package com.it.jdbc.DataSoure;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import javax.sql.DataSource;
import com.it.jdbc.utils.JDBCUtils_V3;
public class MyDataSoure implements DataSource {
// 1,创建一个容器,存储Connection对象
private static LinkedList<Connection> pool = new LinkedList<Connection>();
// 2,创建5个链接放到容器中
static {
for (int i = 0; i < 5; i++) {
Connection conn = JDBCUtils_V3.getConnection();
MyConnection myconn= new MyConnection(conn, pool);
pool.add(myconn);
}
}
@Override
public Connection getConnection() throws SQLException {
Connection conn = null;
// 3,使用前判断
if (pool.size() == 0) {
// 4,添加链接
for (int i = 0; i < 5; i++) {
conn = JDBCUtils_V3.getConnection();
//连接池中的自定义myconn
MyConnection myconn= new MyConnection(conn, pool);
pool.add(myconn);
}
}
// 5,从连接池获取一个链接对象Connection
conn = pool.remove(0);
return conn;
}
public void backConnection(Connection conn) {
// 归还连接池
pool.add(conn);
}
}