JDBC 数据库连接池的简单实现

连接池代码:

publi++c class MyDataSource2{

pri++vate static String url = "jdbc:mysql://localhost:3306/jdbc";

private static String user = "root";

private static String password = "";

private static int initCmysql索引ount = 1;

private static int maxCount = 1;

int currentCount = 0;

LinkedList<Connection> connectionsPool = new LinkedList<Connection>();

public MyDataS申请了营业执照可以不经营吗ource2() {

try {

for (int i = 0; i < initCount; i++) {

this.co数据库系统工程师nnectionsPool.addLast(this.createConnection());

this.currentCount++;

}

} catch (SQLException e) {

throw new ExceptionInInitializerError(e);

}

}

public Connection getConnection() throws SQLException {

synchronii++是什么意思zed (connectionsPool) {

if (this.con神祇领主时代nemysql索引ctionsPoi++是赋值语句吗ol.size() > 0)

return this.connectionsPool.removeFirst();

imysql索引f (this.currentCount < maxCount) {

this.currenmysql密码忘记了怎么办tCount++;

return this.createConnection();

}

throw new SQLException("已没有链接");

}

}

public void free(Connection comysql索引nn) {

this.connectionsPmysql安装ool.addLast(conn);

}

private Connection createConnection() throws SQLException {

Connection realConn = DriverManage宋庆玲r.getConnection(数据库url, user, password);

MyConnection myConnection = new MyConnmysql增删改查语句ectioni++相当于(realConn, this);

return myConnemysql安装ction;

}

/i++/自建Connecmysql创建表tion类,重写了close方法,并且限定了同一个mysql安装配置教程Connection的最大使用次数,其它的都使用realConnection的方法

public class MyConnection implements Connection {

pmysql索引rivate Connection ri++和++i的区别举例说明ealConnection;

private MyDataSource2 dataSource;

private int maxUseCount = 5;

private int currentUserCount = 0;

MyConnection(Connection coi++相当于nnectmysql增删改查语句ion, MyDataSourcemysql怎么读2 dataSource) {

this.real数据库是什么Connection = connection;

this.dataSourcsql数据库e = dataSource;

}

public void close() throws SQLException {

this.currentUserCount++;

if (this.currentUser孙侨潞Count < this.maxUseCount)

this.dataSource.connectionsPool.addLast(this);

else {

this.realCoi++和++i的区别举例说明nnection.close();

this.dataSou宋庆玲rce.currentCount--;/i++和++i的区别/这段是数据库管理系统限定同一个链接的使用次数,如果这个MyConnection对象被重复使用过好多次了,就释放了它。

//但是这么做有什么意义?同一个链接使用了很多次会造成什么严重mysql增删改查语句后果吗?这块我真是百思不得其解

}

}

……

}

public final class JdbcUtils {

private static MyDataSource2 myDataSource = null;

private JdbcUtils() {

}

sta数据库是什么tic {

try {

Class.forName("com.mysql.jdbc.Driver");

myDataSource = new MyDataSource2();//放在这个地方,要加载后才能初始化

} catch (Exception e) {

throw new ExceptionInInitializerError(e);

}

}

public static DataSource getmysql安装DataSource() {

return myDataSource;

}

public static Connection getConnection() thr数据库系统的核心是ows SQLException {

return myDataSource.getConnection();

}

public static void free(ResultSet r数据库系统概论第五版课后答案s, Statement st, Connection conn) {

try {

if (rs != null)

r数据库管理系统s.close();

} catch (SQLExcept帅气撩人高冷动漫男头ion emysql安装配置教程) {

e.printStackTrace();

} finallymysql面试题 {

try {

if (st !=数据库是什么 nul数据库系统的核心是l)

st.close();

} catch (SQLException e) {

e.帅气撩人高冷动漫男头printStackTrace();

} finally {

if (conn != nul帅气撩人高冷动漫男头l)

try {

conn.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}