自定义一个数据库连接池
①准备工作
1 package com.yxfyg.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 import java.util.Properties; 11 12 public class JDBCUtil { 13 14 static String url = null; 15 static String driverClass = null; 16 17 static { 18 try { 19 Properties properties = new Properties(); 20 InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"); 21 properties.load(is); 22 driverClass = properties.getProperty("driverClass"); 23 url = properties.getProperty("url"); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 } 28 29 public static void release(ResultSet rs,Statement st,Connection conn) { 30 closeRs(rs); 31 closeSt(st); 32 closeConn(conn); 33 } 34 35 public static void release(Statement st,Connection conn) { 36 closeSt(st); 37 closeConn(conn); 38 } 39 40 public static Connection getConn() { 41 Connection conn = null; 42 try { 43 Class.forName(driverClass); 44 conn = DriverManager.getConnection(url); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 return conn; 49 } 50 51 private static void closeRs(ResultSet rs) { 52 try { 53 if(rs != null) { 54 rs.close(); 55 } 56 }catch(SQLException e) { 57 e.printStackTrace(); 58 }finally { 59 rs = null; 60 } 61 } 62 63 private static void closeSt(Statement st) { 64 try { 65 if(st != null) { 66 st.close(); 67 } 68 }catch(SQLException e) { 69 e.printStackTrace(); 70 }finally { 71 st = null; 72 } 73 } 74 75 private static void closeConn(Connection conn) { 76 try { 77 if(conn != null) { 78 conn.close(); 79 } 80 }catch(SQLException e) { 81 e.printStackTrace(); 82 }finally { 83 conn = null; 84 } 85 } 86 }
url=jdbc:mysql://localhost/bank?user=root&password=root driverClass=com.mysql.jdbc.Driver
②使用装饰者模式对Connection对象进行包装,改写close方法的作用为归还连接对象到连接池中,其他方法作用不便
1 public class ConnectionWrap implements Connection{ 2 3 private Connection conn; 4 private List<Connection> list; 5 6 public ConnectionWrap(Connection conn,List<Connection> list) { 7 this.conn = conn; 8 this.list = list; 9 } 10 11 @Override 12 public void close() throws SQLException { 13 list.add(this); 14 } 15 16 ... 17 }
③自定义连接池(实现规范DataSource接口)
1 package com.yxfyg.ds; 2 3 import java.io.PrintWriter; 4 import java.sql.Connection; 5 import java.sql.SQLException; 6 import java.sql.SQLFeatureNotSupportedException; 7 import java.util.LinkedList; 8 import java.util.List; 9 import java.util.logging.Logger; 10 11 import javax.sql.DataSource; 12 13 import com.yxfyg.util.ConnectionWrap; 14 import com.yxfyg.util.JDBCUtil; 15 16 public class MyDataSource implements DataSource{ 17 18 private List<Connection> list = new LinkedList<Connection>(); 19 20 private static MyDataSource instance = new MyDataSource(); 21 22 private MyDataSource(){ 23 for(int i=0;i<20;i++) { 24 Connection conn = JDBCUtil.getConn(); 25 list.add(conn); 26 } 27 } 28 29 public static MyDataSource getInstance() { 30 return instance; 31 } 32 33 @Override 34 public Connection getConnection() throws SQLException { 35 if(list.size() == 0) { 36 for(int i=0;i<3;i++) { 37 Connection conn = JDBCUtil.getConn(); 38 conn = new ConnectionWrap(conn, list); 39 list.add(conn); 40 } 41 } 42 43 return list.remove(0); 44 } 45 46 @Override 47 public PrintWriter getLogWriter() throws SQLException { 48 // TODO Auto-generated method stub 49 return null; 50 } 51 52 @Override 53 public void setLogWriter(PrintWriter out) throws SQLException { 54 // TODO Auto-generated method stub 55 56 } 57 58 @Override 59 public void setLoginTimeout(int seconds) throws SQLException { 60 // TODO Auto-generated method stub 61 62 } 63 64 @Override 65 public int getLoginTimeout() throws SQLException { 66 // TODO Auto-generated method stub 67 return 0; 68 } 69 70 @Override 71 public Logger getParentLogger() throws SQLFeatureNotSupportedException { 72 // TODO Auto-generated method stub 73 return null; 74 } 75 76 @Override 77 public <T> T unwrap(Class<T> iface) throws SQLException { 78 // TODO Auto-generated method stub 79 return null; 80 } 81 82 @Override 83 public boolean isWrapperFor(Class<?> iface) throws SQLException { 84 // TODO Auto-generated method stub 85 return false; 86 } 87 88 @Override 89 public Connection getConnection(String username, String password) throws SQLException { 90 // TODO Auto-generated method stub 91 return null; 92 } 93 94 }
④测试使用
1 package com.yxfyg.test; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.List; 8 9 import org.junit.Test; 10 11 import com.yxfyg.ds.MyDataSource; 12 import com.yxfyg.util.JDBCUtil; 13 14 public class MainTest { 15 16 @Test 17 public void TestMyDataSource() { 18 19 Connection conn = null; 20 PreparedStatement ps = null; 21 ResultSet rs = null; 22 23 try { 24 conn = MyDataSource.getInstance().getConnection(); 25 String sql = "select * from account where id = ?"; 26 ps = conn.prepareStatement(sql); 27 ps.setInt(1, 1); 28 rs = ps.executeQuery(); 29 while(rs.next()) { 30 String name = rs.getString("name"); 31 int money = rs.getInt("money"); 32 System.out.println("name:" + name + " money:" + money); 33 } 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 }finally { 37 JDBCUtil.release(rs, ps, conn); 38 } 39 40 } 41 }
浙公网安备 33010602011771号