java之編寫資料庫連接池實現串連的複用

來源:互聯網
上載者:User

標籤:

    一般我們在操作資料庫時候,需要頻繁的開啟和關閉串連,而建立資料庫連接往往開銷比較大,因而我們需要避免這種情況的發生,在這裡我們可以建立一個串連池,當操作資料的時候,我們從串連池中取出串連,操作完畢後再將串連放回到池中。

    在這裡我們需要用到集合,大家知道ArrayList結合其實是一個數組,它讀取資料的時候速度比較快,而LinkedList集合在操作的時候要比ArrayList要快的多,所以這裡我們選擇集合LinkedList。

1.編寫一個串連池的類
 1 package cn.mycast.bank.db; 2  3 import java.sql.Connection; 4 import java.util.LinkedList; 5 import cn.mycast.bank.util.JdbcUtil; 6 public class MyDatabasePool { 7     private LinkedList<Connection> connPool=new LinkedList<Connection>();//存放串連 8     public MyDatabasePool(){ 9         for(int i=0;i<10;i++)//串連池中存放10個串連10         {11             this.connPool.addLast(this.CreateConnection());//每次添加到集合最後面12         }13     }14     public Connection CreateConnection(){//獲得串連15         return JdbcUtil.getConnection();16     }17     public Connection GetConnection(){18         return connPool.removeFirst();//取出最上面的一個串連19     }20     public void FreeConnection(Connection conn){//將用完後的串連放回到集合中21         this.connPool.addLast(conn);22     }23 }
2.編寫一個操作資料庫串連的工具類
 1 package cn.mycast.bank.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 import cn.mycast.bank.db.MyDatabasePool;13 public class JdbcUtil {14     private static String driver;15     private static String url;16     private static String username;17     private static String password;18     private static MyDatabasePool connPool;//串連池類19     static{20         // 通過類載入器獲得資源,並以流的方式進行操作21         InputStream is=JdbcUtil.class.getClassLoader().getResourceAsStream("cn/mycast/bank/util/Mysqldb.properties");22         Properties properties=new Properties();23         try {24             properties.load(is);25             url=properties.getProperty("url");26             driver=properties.getProperty("driver");27             username=properties.getProperty("username");28             password=properties.getProperty("password");29         } catch (IOException e) {30             e.printStackTrace();31         }32     }33     static{34         try {35             Class.forName(driver);//註冊驅動36             connPool=new MyDatabasePool();37         } catch (ClassNotFoundException e) {38             e.printStackTrace();39         }40     }41     42     public static Connection getConnection(){43         Connection conn=null;44         try {45             conn=DriverManager.getConnection(url, username, password);//獲得串連46         } catch (SQLException e) {47             e.printStackTrace();48         }finally{49             50         }51         return conn;52     }53     public static void close(Connection conn){54         if(conn!=null)55             connPool.FreeConnection(conn); //將串連放回集合56     }57     public static void close(Statement stm){58         try {59             if(stm!=null)60                 stm.close();61         } catch (SQLException e) {62             e.printStackTrace();63         }64     }65     public static void close(ResultSet rs){66         try {67             if(rs!=null)68                 rs.close();69         } catch (SQLException e) {70             e.printStackTrace();71         }72     }73 }
後面只需調用該工具類的相關方法即可

java之編寫資料庫連接池實現串連的複用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.