JDBC实现简单的连接池代码

 

连接池类:MyDataSource.java

Java代码    收藏代码
  1. package com.itcast.myjdbc.pool;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6. import java.util.LinkedList;  
  7.   
  8. public class MyDataSource {  
  9.     private static final String url="jdbc:sqlserver://localhost:1433;DatabaseName=mytest";  
  10.     private static final String username="sa";  
  11.     private static final String password="123456";  
  12.       
  13.     //LinkedListy用来添加链接,添加、删除元素比较快。  
  14.     private LinkedList<Connection> pool = new LinkedList<Connection>();  
  15.       
  16.     private int initCount =5;//容器中初始化添加数目  
  17.     private int maxCount = 10;//容器中最大可以添加的数目  
  18.     int currentCount = 0;//当前使用的连接数  
  19.       
  20.     public MyDataSource() {//初始化添加链接  
  21.         for(int i=0; i<initCount; i++) {  
  22.             this.pool.addLast(createConnection());  
  23.             this.currentCount++;  
  24.         }  
  25.     }  
  26.       
  27.     public Connection getConnection() throws SQLException {//获取链接  
  28.         synchronized(pool) {  
  29.             if(this.pool.size()>0) {  
  30.                 return pool.removeFirst();  
  31.             }  
  32.             if(this.currentCount<maxCount) {  
  33.                 return createConnection();  
  34.             }  
  35.             throw new SQLException("已没有链接");  
  36.         }  
  37.     }  
  38.       
  39.     public void free(Connection conn) {//释放链接  
  40.         pool.addLast(conn);  
  41.     }  
  42.       
  43.     public Connection createConnection() {//创建连接  
  44.         Connection conns = null;  
  45.         try {  
  46.             conns = DriverManager.getConnection(url, username, password);  
  47.         } catch (SQLException e) {  
  48.             System.out.println("创建链接失败");  
  49.         }  
  50.         return conns;  
  51.     }  
  52.   
  53.     public LinkedList<Connection> getPool() {  
  54.         return pool;  
  55.     }  
  56.   
  57.     public void setPool(LinkedList<Connection> pool) {  
  58.         this.pool = pool;  
  59.     }  
  60.   
  61. }  

 测试类:Test .java

Java代码    收藏代码
  1. package com.itcast.myjdbc.pool;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.SQLException;  
  5.   
  6. public class Test {  
  7.     public static void main(String[] args) throws SQLException {  
  8.         try {  
  9.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
  10.         } catch (ClassNotFoundException e) {  
  11.             System.out.println("加载驱动失败");  
  12.         }  
  13.           
  14.         MyDataSource myDatasource = new MyDataSource();  
  15.         for(int i=0; i<30; i++) {//输出三十个连接  
  16.             Connection conn = myDatasource.getConnection();  
  17.             System.out.println(conn);  
  18.             myDatasource.free(conn);  
  19.         }  
  20.     }  
  21. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值