Jdbc 数据库连接池简易实现和JdbcUtils
程序员文章站
2022-03-04 18:05:04
...
public class MyDataSource {
private static String url = "jdbc:mysql://localhost:3306/riverevaluationsys";
private static String user = "root";
private static String password = "root";
private static int initCount = 5;
private static int maxCount = 20;
private int currentCount = 0;
private LinkedList<Connection> connectionsPool = new LinkedList<Connection>();
public MyDataSource() {
try {
for (int i=0; i<5; i++) {
this.connectionsPool.add(this.createConnection());
this.currentCount++;
}
} catch (SQLException e) {
throw new ExceptionInInitializerError(e);
}
}
public Connection getConnection() throws SQLException {
// 添加同步锁
synchronized (connectionsPool) {
if (this.connectionsPool.size() > 0) {
return this.connectionsPool.removeFirst();
} else {
if (this.currentCount < maxCount) {
this.currentCount++ ;
return this.createConnection();
} else {
throw new SQLException("no more connection.");
}
}
}
}
public void free(Connection conn) {
this.connectionsPool.addLast(conn);
}
private Connection createConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
}
public class JdbcUtils {
/*private static String url = "jdbc:mysql://localhost:3306/riverevaluationsys";
private static String user = "root";
private static String password = "root";*/
private static MyDataSource myDataSource ;
// 装入虚拟机,立即执行
static {
try {
Class.forName("com.mysql.jdbc.Driver");
myDataSource = new MyDataSource();
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
/**
*
* getConnection:<br />
* 获取数据库的连接
*
* @author zhangzhaoyu
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
//return DriverManager.getConnection(url, user, password);
return myDataSource.getConnection();
}
/**
*
* close:<br />
* 关闭资源
*
* @author zhangzhaoyu
* @param conn
* @param rs
* @param st
*/
public static void close(Connection conn, ResultSet rs, Statement st) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
*
* close:<br />
* 关闭数据库重载函数
*
* @author zhangzhaoyu
* @param conn
* @param rs
* @param pst
*/
public static void close(Connection conn, ResultSet rs, PreparedStatement pst) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pst != null)
pst.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
*
* close:<br />
* 关闭资源
*
* @author zhangzhaoyu
* @param conn
* @param pst
*/
public static void close(Connection conn, PreparedStatement pst) {
try {
if (pst != null)
pst.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
上一篇: 网址php是什么意思
下一篇: CSS3实现卡片效果
推荐阅读
-
实现JDBC自动加载驱动,简化数据连接和关闭数据库连接
-
用数据库和JDBC完成控制台版本的简易新闻发布系统
-
oracle/mysql连接德鲁伊数据库连接池和使用dbutils第三方jar包简化dao层实现增删改查
-
【数据库学习笔记】(4)JDBC数据源和连接池
-
【数据库学习笔记】(4)JDBC数据源和连接池
-
JDBC基础语法、statement对象和PreparedStatement对象、SQL注入及解决、数据库连接池等详解
-
实现JDBC自动加载驱动,简化数据连接和关闭数据库连接
-
oracle/mysql连接德鲁伊数据库连接池和使用dbutils第三方jar包简化dao层实现增删改查
-
Jdbc 数据库连接池简易实现和JdbcUtils