自己封装的一个jdbc,给jdbc增加了一个连接池,但是不知道有没有问题,所以让大神们帮忙看下,我写的这个线程池有没有问题
package tool;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Jdbc {
//连接池中的连接
private Connection conn;
private ResultSet rs;
private ResultSetMetaData md;
private PreparedStatement ps;
private JSONArray list;
private JSONObject map;
private String classpath = getClass().getResource("/").getPath();
private String path = classpath + "jdbc.properties";
private BlockingQueue<Connection> connections = new LinkedBlockingQueue<>();
public Jdbc(){}
/**
* 初始化Jdbc连接池
* @param poolSize 连接池大小
* @throws IOException
* @throws ClassNotFoundException
* @throws SQLException
* @throws InterruptedException
*/
public void initJdbcPool() throws IOException, ClassNotFoundException, SQLException, InterruptedException {
FileInputStream fis = new FileInputStream(path);
Properties prop = new Properties();
prop.load(fis);
String url = prop.getProperty("db.url");
String user = prop.getProperty("db.user");
String psw = prop.getProperty("db.password");
Integer connectionNum = Integer.parseInt(prop.getProperty("connectionNum"));
Class.forName(prop.getProperty("driverClass"));
for (int i = 0; i < connectionNum; i++) {
//获取数据库的连接
Connection dbConn = DriverManager.getConnection(url, user, psw);
//System.out.println(dbConn);
connections.put(dbConn);
}
}
public void destroyJdbcPool() {
connections.clear();
}
/**
* 从连接池中获取链接
* @return
* @throws InterruptedException
*/
private Connection getConnection() throws InterruptedException {
return connections.take();
}
/**
* 把连接放回到连接池中,并关闭声明对象和结果集对象
* @param connection 连接
* @param ps 声明对象
* @param rs 结果集对象
* @throws SQLException
* @throws InterruptedException
*/
private void backConnection(Connection connection,PreparedStatement ps,ResultSet rs) throws SQLException, InterruptedException {
if(ps!=null) ps.close();
if(rs!=null) rs.close();
connections.put(connection);
}
/**
* 执行dml操作
* @param sql sql语句
* @param objs 参数对象
* @return
*/
public boolean dml(String sql, Object... objs) {
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i+1, objs[i]);
}
int flg = ps.executeUpdate();
if(flg==1) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
try {
backConnection(conn,ps,rs);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
}
return false;
}
/**
* 全查
* @param sql
* @param objs
* @return
*/
public JSONArray find(String sql,Object...objs){
list = new JSONArray();
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i+1, objs[i]);
}
rs = ps.executeQuery();
md = rs.getMetaData();
while (rs.next()) {
map = new JSONObject();
for (int i = 1; i <= md.getColumnCount(); i++) {
String key = md.getColumnName(i);
Object value = rs.getObject(i);
map.put(key, value);
}
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
try {
backConnection(conn,ps,rs);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
}
return list;
}
/**
* 单查
* @param sql
* @param objs
* @return
*/
public JSONObject findOne(String sql,Object...objs){
JSONArray list = find(sql,objs);
if(list!=null) {
if(list.size()>0) {
return list.getJSONObject(0);
}
}
return null;
}
/**
* 添加
* @param sql
* @param objs
* @return
*/
public boolean add(String sql,Object...objs) {
return dml(sql, objs);
}
/**
* 修改
* @param sql
* @param objs
* @return
*/
public boolean mod(String sql,Object...objs){
return dml(sql, objs);
}
/**
* 删除
* @param sql
* @param objs
* @return
*/
public boolean del(String sql,Object...objs){
return dml(sql, objs);
}
}
jdbc.properties
db.url=jdbc:mysql://127.0.0.1:3306/juser
db.user=root
db.password=root
driverClass=com.mysql.jdbc.Driver
connectionNum=30