1 数据库配置
配置项的值来自一个叫mysql_en.properties的文件(该文件应该放到对应的CLASSPATH的目录。
mysql_en.properties
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/dfs
- user=root
- password=123456
- characterEncoding=utf8
- minPoolSize = 3
- maxPoolSize = 10
假设我们用来读取配置文件的class叫ResourceManager,我们使用jdk的ResourceBundle来读取配置文件。
- package dbConnectionPool;
- import java.util.Locale;
- import java.util.ResourceBundle;
- public class ResourceManager {
- private static ResourceBundle r;
- static {
- r = ResourceBundle.getBundle("mysql", Locale.ENGLISH);
- }
- public static String getDriverClass() {
- return r.getString("driver");
- }
- public static String getUrl() {
- return r.getString("url");
- }
- public static String getUsername() {
- return r.getString("user");
- }
- public static String getPassword() {
- return r.getString("password");
- }
- public static String getCharacterEncoding() {
- return r.getString("characterEncoding");
- }
- public static int getMinPoolSize() {
- int poolSize = Integer.valueOf(r.getString("minPoolSize"));
- return poolSize;
- }
- public static int getMaxPoolSize() {
- int poolSize = Integer.valueOf(r.getString("maxPoolSize"));
- return poolSize;
- }
- public static void refresh() {
- r = ResourceBundle.getBundle("resourceBundle");
- }
- }
只需要告诉ResourceBundle文件名是"mysql"就足够了。下划线和后面的"en"表示的是本地化信息。这里的en代表"ENGLISH",后缀properties是默认的。
2 连接池代码
- package dbConnectionPool;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- public class DbConnectionPool {
- private List<Connection> pool;
- private int minSize = 3;
- private int maxSize = 10;
- private static DbConnectionPool instance = null;
- /**
- *得到当前连接池的一个实例
- */
- public static DbConnectionPool getInstance() {
- if (instance == null) {
- instance = new DbConnectionPool();
- }
- return instance;
- }
- /**
- *单例模式私有构造方法,获得本类的对象,通过getIstance方法。
- */
- private DbConnectionPool() {
- pool = new ArrayList<Connection>();
- int min_Size = ResourceManager.getMinPoolSize();
- int max_Size = ResourceManager.getMaxPoolSize();
- if (min_Size > 0 && max_Size > 0 && min_Size < max_Size) {
- this.minSize = min_Size;
- this.maxSize = max_Size;
- }
- System.out.println(this.minSize);
- System.out.println(this.maxSize);
- initPool();
- }
- /**
- *连接池初始化,生成最小数目的连接
- */
- private void initPool() {
- Connection conn = null;
- for (int i = 0; i < minSize; i++) {
- conn = createConnection();
- if (conn != null)
- pool.add(conn);
- }
- }
- /**
- *创建连接
- */
- private Connection createConnection() {
- Connection conn = null;
- try {
- Class.forName(ResourceManager.getDriverClass());
- conn = DriverManager.getConnection(ResourceManager.getUrl()
- + "?characterEncoding="
- + ResourceManager.getCharacterEncoding(), ResourceManager
- .getUsername(), ResourceManager.getPassword());
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- /**
- *得到连接池中的一个连接
- */
- public synchronized Connection getConnection() {
- if (pool.size() > 0) {
- Connection conn = pool.get(0);
- pool.remove(conn);
- return conn;
- } else {
- return createConnection();
- }
- }
- /**
- *用完将连接放回到连接池中
- */
- public synchronized void releaseConnection(Connection conn) {
- if (pool.size() < maxSize)
- pool.add(conn);
- else
- try {
- conn.close();
- conn = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- *关闭连接池中的所有连接
- */
- public synchronized void closePool() {
- Connection conn = null;
- for (int i = 0; i < pool.size(); i++) {
- try {
- conn = (Connection) pool.get(i);
- conn.close();
- pool.remove(i);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public synchronized int getConnectionNum(){
- return pool.size();
- }
- }