Jdbc 封装, 利用反射, 加入连接池
2018-11-21 本文已影响0人
unique_a311
随着技术的发展, JDBC离我们越来越远, 上传一篇以前好玩封装的jdbc, 创建连接池 获取连接, 利用反射
package com.xx.loopdata;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
/**
* DBUtil
* @author Administrator
*
*/
public final class DBUtil {
private final static String DriverName = "com.mysql.jdbc.Driver";
private final static String URL = "jdbc:mysql://127.0.0.1:3306/atom";
private final static String USER = "root";
private final static String PASSWORD = "123456";
//定义一个链表代表连接池
private static LinkedList<Connection> listConn = new LinkedList<>();
private static Connection conn;
private static PreparedStatement ps;
private static ResultSet rs;
static {
//初始化驱动
try {
Class.forName(DriverName);
//初始化十个链接
for (int i = 0; i < 10; i++) {
listConn.add(DriverManager.getConnection(URL, USER, PASSWORD));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取得到一客户端的连接
* @return
* @throws SQLException
*/
private static Connection getConnection() throws SQLException {
//返回一个链接给客户端
if (listConn.size() > 0) {
//存在链接
//真正的对象
final Connection conn = listConn.removeFirst();
//生成真正的对象代理,重写close方法,还给连接池
Connection proxyConn = (Connection) Proxy.newProxyInstance(DBUtil.class.getClassLoader(), conn.getClass().getInterfaces(),new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] obj) throws Throwable {
//重写close方法
if (method.getName().equals("close")) {
//将数据库连接返回给链表
listConn.add(conn);
return null;
}
else {
return method.invoke(conn, obj);
}
}
});
return proxyConn;
}
else {
throw new SQLException("系统繁忙,请稍后再试");
}
}
/**
* 创建一个 PrepareStatement 对象
* @param sql
* @param objects
* @return
*/
private static PreparedStatement createStatement(String sql,Object[] objects) {
//先获取连接
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
//循环获取参数
for (int i = 0; i < objects.length; i++) {
ps.setObject(i+1, objects[i]);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
close();
}
return ps;
}
/**
* 修改语句
* @param sql
* @param objects
* @return
*/
public static int executeUpdate(String sql,Object...objects) {
ps=createStatement(sql, objects);
int result = 0;
try {
result = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
close();
}
return result;
}
/**
* 查询语句
* @param sql
* @param objects
* @return
*/
public static ResultSet executeQuery(String sql,Object...objects) {
try {
rs = createStatement(sql, objects).executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
close();
}
return rs;
}
/**
* 关闭连接
* @param conn
* @param sm
* @param rs
*/
private static void close() {
try {
rs.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ps.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}