Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2140436
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: Java

2011-10-05 20:17:26

  1. package cn.itcast.jdbc.datasource;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.util.LinkedList;
  6. import java.util.List;

  7. public class MyDataSource {
  8.     
  9.     private static String url="jdbc:mysql://localhost:3306/jdbc";
  10.     private static String user="root";
  11.     private static String password="admin";
  12.     
  13.     private LinkedList<Connection> connectionsPool=new LinkedList<Connection>();
  14.     
  15.     public MyDataSource(){
  16.         for(int i=0;i<10;i++){
  17.              try {
  18.                  //先入先出的算法
  19.                 this.connectionsPool.addLast(this.createConnection());
  20.             } catch (SQLException e) {
  21.                 throw new ExceptionInInitializerError(e);
  22.             }
  23.         }
  24.     }
  25.     
  26.      public Connection getConnection(){
  27.          return this.connectionsPool.removeFirst();
  28.      }
  29.      private Connection createConnection() throws SQLException{
  30.          return DriverManager.getConnection(url, user, password);
  31.      }
  32.     
  33.      public void free(Connection conn){
  34.          this.connectionsPool.addLast(conn);
  35.      }

  36. }
 
  1. package cn.itcast.jdbc;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;

  7. import cn.itcast.jdbc.datasource.MyDataSource;

  8. public final class JdbcUtils {

  9.     private static String url="jdbc:mysql://localhost:3306/jdbc";
  10.     private static String user="root";
  11.     private static String password="admin";
  12.     private static MyDataSource myDataSource=null;
  13.     private JdbcUtils(){    
  14.     }

  15.     static{

  16.         try{
  17.             Class.forName("com.mysql.jdbc.Driver");
  18.             myDataSource=new MyDataSource();
  19.         }catch(ClassNotFoundException e){
  20.             throw new ExceptionInInitializerError(e);
  21.         }
  22.     }

  23.     public static Connection getConnection()throws SQLException{
  24.         //return DriverManager.getConnection(url,user,password);
  25.         return myDataSource.getConnection();
  26.     }

  27.     public static void free(ResultSet rs,Statement st,Connection conn){
  28.         try{
  29.             if(rs!=null)
  30.                 rs.close();
  31.         }catch(SQLException e){
  32.             e.printStackTrace();
  33.         }finally{
  34.             try{
  35.                 if(st!=null)
  36.                     st.close();
  37.             }catch(SQLException e){
  38.                 e.printStackTrace();
  39.             }finally{
  40.                 if(conn!=null)
  41.                     try{
  42.                         //conn.close();
  43.                         myDataSource.free(conn);
  44.                     }catch(Exception e){
  45.                         e.printStackTrace();
  46.                     }
  47.             }
  48.         }
  49.     }
  50. }

编写一个基本的连接池来实现连接的复用。

注意:removeLast是LinkedList所独有的方法。

所以只能写private LinkedList connectionsPool=new LinkedList();

不能写成private List connectionsPool=new LinkedList();这种面向接口的编程。切记,切记!

阅读(2613) | 评论(0) | 转发(0) |
0

上一篇:软件质量模型

下一篇:JDBC性能优化续

给主人留下些什么吧!~~