数据库连接池
来源:互联网 发布:西门子840d攻丝编程 编辑:程序博客网 时间:2024/06/06 03:03
一个数据库连接池,顾名思义。就是放一堆连接,在需要时就从其中找到空闲的连接,并且使用;而使用完毕后,就可以将连接返回到数据库连接池中。
SqlHelper为获得连接的方法:
- import java.sql.*;
- import java.sql.Connection;
- /**
- *
- * @author Administrator
- */
- //返回Connection
- public class SqlHelper {
- public static Connection getConnection()
- {
- Connection conn=null;
- String url1="com.microsoft.jdbc.sqlserver.SQLServerDriver";
- String url2="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
- String username="sa";
- String pwd="";
- try
- {
- Class.forName(url1);
- conn=DriverManager.getConnection(url2,username,pwd);
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- return conn;
- }
- }
ConnectionPool为数据库连接池:
- import java.sql.*;
- import java.util.Hashtable;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Administrator
- */
- public class ConnectionPool {
- //可用连接数为10
- private static final int CONNECTIONPOOL_SIZE=10;
- //Hash表,用于存放连接
- private Hashtable connectionPool=null;
- //用于存放连接的使用情况,true为已经被使用,而false表示尚未被使用
- private boolean[] connectionPool_status=null;
- //唯一的一个ConnectionPool
- private static ConnectionPool self=null;
- //初始化
- public synchronized static void init()
- {
- self=new ConnectionPool();
- self.connectionPool=new Hashtable();
- self.connectionPool_status=new boolean[CONNECTIONPOOL_SIZE];
- buildConnection();
- }
- //建立所有连接
- public synchronized static void buildConnection()
- {
- if(self==null)
- init();
- for(int i=0;i<CONNECTIONPOOL_SIZE;i++)
- {
- Connection conn=SqlHelper.getConnection();
- self.connectionPool.put(i, conn);
- self.connectionPool_status[i]=false;
- }
- }
- //释放所有连接
- public synchronized static void releaseAllConnection()
- {
- if(self==null)
- init();
- for(int i=0;i<CONNECTIONPOOL_SIZE;i++)
- {
- try {
- Connection conn = (Connection) self.connectionPool.get(i);
- conn.close();
- } catch (SQLException ex) {
- Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- System.out.println("已经释放所有连接");
- }
- //获得连接
- public static Connection getConnection()
- {
- boolean tag;
- Connection conn=null;
- int i;
- if(self==null)
- init();
- for( i=0;i<CONNECTIONPOOL_SIZE;i++)
- {
- if((tag=self.connectionPool_status[i])!=true)
- {
- conn=(Connection)self.connectionPool.get(i);
- self.connectionPool_status[i]=true;
- break;
- }
- }
- if(i>=CONNECTIONPOOL_SIZE)
- {
- System.out.println("没有可用连接");
- }
- return conn;
- }
- //返还连接
- public static void returnConnection(Connection conn)
- {
- if(self==null)
- init();
- for(int i=0;i<CONNECTIONPOOL_SIZE;i++)
- {
- if(self.connectionPool.get(i).equals(conn))
- {
- self.connectionPool_status[i]=false;
- System.out.println("返还连接");
- break;
- }
- }
- }
- //重建立连接
- public static void rebuildConnection(Connection conn)
- {
- if(self==null)
- init();
- for(int i=0;i<CONNECTIONPOOL_SIZE;i++)
- {
- if(self.connectionPool.get(i).equals(conn))
- {
- Connection conn1=SqlHelper.getConnection();
- self.connectionPool.put(i, conn1);
- self.connectionPool_status[i]=true;
- System.out.println("重建连接");
- break;
- }
- }
- }
- //重置数据库连接池
- public synchronized static void reset()
- {
- self=null;
- init();
- }
- }
0 0
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 数据库连接池
- 深入理解cookie与session
- 成都天钻调查公司
- 自开发显示表索引程序
- Java学习之Servlet-doGet()与doPost()
- 自开发对比报表的工具程序
- 数据库连接池
- Setup Factory 脚本编辑方法
- 设计模式笔记-桥模式
- 自开发程序下载的工具
- 分享一个连接,python扩展包下载地址分享包括numpy,scipy等,解决了win8系统不能使用这些扩展包的问题!
- 王立平--GUI与GUILayout的区别
- Automake简单使用,网上很多的都报错,参考官方文档改了一些,行了
- C语言中的基本数据类型
- Java:Java中引用的原理