日期:2014-05-16  浏览次数:20552 次

Jdbc 数据库连接池简易实现和JdbcUtils
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) {