Java连接池是Java学习者必知的一个基础概念。下面给出一个例子,总结了一些学习Java连接池的一些心得。本例子只有三个文件,下面是其源码
resourceBundle.properties文件
connection.username=sa connection.password=sa connection.url=jdbc:sqlserver://localhost:1433;databaseName=collectionpool connection.driverClass=com.microsoft.jdbc.sqlserver.SQLServerDriver
package org.topCSA.connection.db.Resource; import java.util.ResourceBundle; publicclass ResourceManager { privatestatic ResourceBundle r; static{ r = ResourceBundle.getBundle("resourceBundle"); } publicstatic String getDriverClass(){ returnr.getString("connection.driverClass"); } publicstatic String getUrl(){ returnr.getString("connection.url"); } publicstatic String getUsername(){ returnr.getString("connection.username"); } publicstatic String getPassword(){ returnr.getString("connection.password"); } publicstaticintgetPoolSize(){ int poolSize = Integer.valueOf(r.getString("connection.poolSize")); return poolSize; } publicstaticvoid refresh(){ r = ResourceBundle.getBundle("resourceBundle"); } }
DbConnectionPool.java类
package org.topCSA.connection.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.topCSA.connection.db.Resource.ResourceManager; publicclass DbConnectionPool { private List<Connection> pool; privateintpoolSize = 1; private Connection conn = null; privatestatic DbConnectionPool instance = null; /** *单粒模式私有构造方法,获得本类的对象,通过getIstance方法。 */ 本新闻共4页,当前在第1页 1 2 3 4