数据库连接池的创建与运用
//数据库连接池,用集合保存一大批connection,用户-servlet-dao-db //-->改为用户-servlet-dao-connectionpool--db public class jdbcpool implements DataSource { //连接池里面先保存多个连接,供dao取出连接,要增删改查连接用linkedlist private static LinkedList<Connection>list=new LinkedList<Connection>(); static{ Properties p=new Properties(); InputStream in=jdbcpool.class.getClassLoader().getResourceAsStream("db.properties"); try { p.load(in); for(int i=0;i<10;i++)//为数据库创建10个连接,放到连接池 { Class.forName(p.getProperty("driver")); Connection con=DriverManager.getConnection(p.getProperty("url"), p.getProperty("username"), p.getProperty("password")); list.add(con); } } catch (Exception e) { e.printStackTrace(); } } @Override public Connection getConnection() throws SQLException { //从连接池里面取出数据 if(list.size()<=0) { throw new RuntimeException(); } Connection con=list.removeFirst(); //每次都移除第一个连接,连接池里减少一个,相当于取出了一个连接 //conn.Close();这样释放资源的话只会释放到数据库中,不会释放到连接池中 //相当于我们可以增强con.close()方法,当close()的时候,归还到连接池中去 MyConnection my=new MyConnection(con); return my; } //增强方法有3种方法 //1:写一个connction子类,覆盖close()方法,增强close()方法 //2:用包装设计模式 //3:动态代理 //使用包装设计模式增强才,close()方法,包装设计模式步奏 /* * 1:定义一个类,实现与增强相同的接口 * 2:在类中定义一个变量,记住被增强对象 * 3:定义一个构造函数,接受被增强对象 * 4:覆盖想增强方法 * 5:对于不想增强的方法,直接调用目标对象的方法 */ class MyConnection implements Connection { private Connection con; public MyConnection(Connection con) { this.con=con; } @Override public void close() throws SQLException { list.add(con);//释放数据库资源的时候,直接释放到连接池中去 } @Override public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } @Override public Statement createStatement() throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql) throws SQLException { // TODO Auto-generated method stub return this.con.prepareStatement(sql); } @Override public CallableStatement prepareCall(String sql) throws SQLException { // TODO Auto-generated method stub return null; } @Override public String nativeSQL(String sql) throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setAutoCommit(boolean autoCommit) throws SQLException { // TODO Auto-generated method stub } @Override public boolean getAutoCommit() throws SQLException { // TODO Auto-generated method stub return false; } @Override public void commit() throws SQLException { // TODO Auto-generated method stub } @Override public void rollback() throws SQLException { // TODO Auto-generated method stub } @Override public boolean isClosed() throws SQLException { // TODO Auto-generated method stub return false; } @Override public DatabaseMetaData getMetaData() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setReadOnly(boolean readOnly) throws SQLException { // TODO Auto-generated method stub } @Override public boolean isReadOnly() throws SQLException { // TODO Auto-generated method stub return false; } @Override public void setCatalog(String catalog) throws SQLException { // TODO Auto-generated method stub } @Override public String getCatalog() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setTransactionIsolation(int level) throws SQLException { // TODO Auto-generated method stub } @Override public int getTransactionIsolation() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public SQLWarning getWarnings() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void clearWarnings() throws SQLException { // TODO Auto-generated method stub } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { // TODO Auto-generated method stub return null; } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Map<String, Class<?>> getTypeMap() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setTypeMap(Map<String, Class<?>> map) throws SQLException { // TODO Auto-generated method stub } @Override public void setHoldability(int holdability) throws SQLException { // TODO Auto-generated method stub } @Override public int getHoldability() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public Savepoint setSavepoint() throws SQLException { // TODO Auto-generated method stub return null; } @Override public Savepoint setSavepoint(String name) throws SQLException { // TODO Auto-generated method stub return null; } @Override public void rollback(Savepoint savepoint) throws SQLException { // TODO Auto-generated method stub } @Override public void releaseSavepoint(Savepoint savepoint) throws SQLException { // TODO Auto-generated method stub } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { // TODO Auto-generated method stub return null; } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Clob createClob() throws SQLException { // TODO Auto-generated method stub return null; } @Override public Blob createBlob() throws SQLException { // TODO Auto-generated method stub return null; } @Override public NClob createNClob() throws SQLException { // TODO Auto-generated method stub return null; } @Override public SQLXML createSQLXML() throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isValid(int timeout) throws SQLException { // TODO Auto-generated method stub return false; } @Override public void setClientInfo(String name, String value) throws SQLClientInfoException { // TODO Auto-generated method stub } @Override public void setClientInfo(Properties properties) throws SQLClientInfoException { // TODO Auto-generated method stub } @Override public String getClientInfo(String name) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Properties getClientInfo() throws SQLException { // TODO Auto-generated method stub return null; } @Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Struct createStruct(String typeName, Object[] attributes) throws SQLException { // TODO Auto-generated method stub return null; } } @Override public Connection getConnection(String username, String password) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub } @Override public void setLoginTimeout(int seconds) throws SQLException { // TODO Auto-generated method stub } @Override public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } }
package com.mysql; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import org.junit.Test; public class insertData { @Test public void testInsert() throws SQLException { Connection con=null; try{ jdbcpool p=new jdbcpool(); con=p.getConnection(); String sql="insert into login value('jack','456')"; PreparedStatement ps=con.prepareStatement(sql); int i=ps.executeUpdate(); if(i>0) { System.out.println("insert success"); } } catch (Exception e) { } finally{ con.close();//释放资源到连接池 } } }