- 浏览: 20664 次
最近访客 更多访客>>
文章分类
最新评论
-
wmiao89620:
帮了我。
错误整理 servlet生成XML读取失败 about ajax -
zacom1025:
郁闷了好久了,幸亏你整理了下,谢谢
错误整理 servlet生成XML读取失败 about ajax -
elvishehai:
有没有一包啊, 顾··
转贴:主题:ajax、Struts、spring的无缝结合 -
hgq0011:
daoger 写道挺好!给个良好!
这个也挺好啊!
http: ...
利用AJAX实现 web页面的模式对话框 -
daoger:
挺好!给个良好!这个也挺好啊!http://prototype ...
利用AJAX实现 web页面的模式对话框
首先说明一下,这个数据库链接池不是我写的。是我在网上找到的,由于代码并不全,因此进行了一些填充。可以正常运行了。所以贴出来希望能帮到需要的人。
这个数据库链接池的好处在于当用户从数据库链接池中的到连接后,并不需要特殊的方法将链接关闭,直接用close方法就可以关闭了。废话少说,上代码!!!!
再次说明一下,以下的代码是根据网上得到的部分代码填充得到的,并不代表没有错误。如果那位高手能将代码完善感激不尽。
下面的一段代码是一些数据库的属性。
java 代码
- public class ConnectionParam {
- private String driver = null; //数据库驱动程序
- private String host = null; //数据连接的URL
- private String user = null; //数据库用户名
- private String password = null; //数据库密码
- private String dataBase= null;
- private String url = null;
- private int minConnection = 0; //初始化连接数
- private int maxConnection = 0; //最大连接数
- private long timeoutValue = 0; //连接的最大空闲时间
- private long waitTime = 0; //取连接的时候如果没有可用连接最大的等待时间
- public ConnectionParam(){
- super();
- try {
- String tmp = null;
- int value = 0;
- //CommonUtil.getPropertie("");方法是自己写的一个从属性文件中读取信息的方法。
- this.driver = CommonUtil.getPropertie("driver");
- this.host = CommonUtil.getPropertie("host");
- this.user = CommonUtil.getPropertie("user");
- this.password = CommonUtil.getPropertie("password");
- this.dataBase= CommonUtil.getPropertie("dataBase");
- this.url =CommonUtil.getPropertie("url");
- tmp = CommonUtil.getPropertie("maxConnection");
- value = 0;
- if(CommonUtil.isNull(tmp)){
- value = 0;
- }else{
- value = Integer.parseInt(tmp);
- }
- this.maxConnection = value;
- tmp = CommonUtil.getPropertie("timeoutValue");
- value = 0;
- if(CommonUtil.isNull(tmp)){
- value = 0;
- }else{
- value = Integer.parseInt(tmp);
- }
- this.timeoutValue = value;
- tmp = CommonUtil.getPropertie("waitTime");
- value = 0;
- if(CommonUtil.isNull(tmp)){
- value = 0;
- }else{
- value = Integer.parseInt(tmp);
- }
- this.waitTime = value;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
下面这段代码是实现连接池的,其中的一些方法并没有实现。请各位见谅。
java 代码
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import javax.sql.DataSource;
- public class DataSourceImpl implements DataSource {
- private ConnectionParam connParam = null;
- private List conns = null;
- public DataSourceImpl(ConnectionParam param){
- conns = new ArrayList();
- this.connParam = param;
- }
- public Connection getConnection() throws SQLException {
- //首先从连接池中找出空闲的对象
- Connection conn = getFreeConnection(0);
- Connection conn2 = null;
- if(conn == null){
- //判断是否超过最大连接数,如果超过最大连接数
- //则等待一定时间查看是否有空闲连接,否则抛出异常告诉用户无可用连接
- if(getConnectionCount() >= connParam.getMaxConnection())
- conn = getFreeConnection(connParam.getWaitTime());
- else{//没有超过连接数,重新获取一个数据库的连接
- try {
- Class.forName(connParam.getDriver()).newInstance();
- conn2 = DriverManager.getConnection(connParam.getUrl(),
- connParam.getUser(), connParam.getPassword());
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- //代理将要返回的连接对象
- _Connection _conn = new _Connection(conn2,true);
- synchronized(conns){
- conns.add(_conn);
- }
- conn = _conn.getConnection();
- }
- }
- return conn;
- }
- public Connection getConnection(String username, String password)
- throws SQLException {
- //首先从连接池中找出空闲的对象
- Connection conn = getFreeConnection(0);
- Connection conn2 = null;
- if(conn == null){
- //判断是否超过最大连接数,如果超过最大连接数
- //则等待一定时间查看是否有空闲连接,否则抛出异常告诉用户无可用连接
- if(getConnectionCount() >= connParam.getMaxConnection())
- conn = getFreeConnection(connParam.getWaitTime());
- else{//没有超过连接数,重新获取一个数据库的连接
- connParam.setUser(username);
- connParam.setPassword(password);
- try {
- Class.forName(connParam.getDriver()).newInstance();
- conn2 = DriverManager.getConnection(connParam.getUrl(),
- username, password);
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //代理将要返回的连接对象
- _Connection _conn = new _Connection(conn2,true);
- synchronized(conns){
- conns.add(_conn);
- }
- conn = _conn.getConnection();
- }
- }
- return conn;
- }
- public PrintWriter getLogWriter() throws SQLException {
- // TODO Auto-generated method stub
- return null;
- }
- public int getLoginTimeout() throws SQLException {
- // TODO Auto-generated method stub
- return 0;
- }
- public void setLogWriter(PrintWriter out) throws SQLException {
- // TODO Auto-generated method stub
- }
- public void setLoginTimeout(int seconds) throws SQLException {
- // TODO Auto-generated method stub
- }
- public void initConnection(){
- // TODO Auto-generated method stub
- }
- public void stop(){
- // TODO Auto-generated method stub
- }
- /**
- * 从连接池中取一个空闲的连接
- * @param nTimeout 如果该参数值为0则没有连接时只是返回一个null
- * 否则的话等待nTimeout毫秒看是否还有空闲连接,如果没有抛出异常
- * @return Connection
- * @throws SQLException
- */
- protected synchronized Connection getFreeConnection(long nTimeout)
- throws SQLException
- {
- Connection conn = null;
- Iterator iter = conns.iterator();
- while(iter.hasNext()){
- _Connection _conn = (_Connection)iter.next();
- if(!_conn.isInUse()){
- conn = _conn.getConnection();
- _conn.setInUse(true);
- break;
- }
- }
- if(conn == null && nTimeout > 0){
- //等待nTimeout毫秒以便看是否有空闲连接
- try{
- Thread.sleep(nTimeout);
- }catch(Exception e){}
- conn = getFreeConnection(0);
- if(conn == null)
- throw new SQLException("没有可用的数据库连接");
- }
- return conn;
- }
- /**
- * 关闭该连接池中的所有数据库连接
- * @return int 返回被关闭连接的个数
- * @throws SQLException
- */
- public int close() throws SQLException
- {
- int cc = 0;
- SQLException excp = null;
- Iterator iter = conns.iterator();
- while(iter.hasNext()){
- try{
- ((_Connection)iter.next()).close();
- cc ++;
- }catch(Exception e){
- if(e instanceof SQLException)
- excp = (SQLException)e;
- }
- }
- if(excp != null)
- throw excp;
- return cc;
- }
- /**
- * 返回当前链接的个数
- * @return
- */
- private int getConnectionCount(){
- Iterator iter = conns.iterator();
- int count = 0;
- while(iter.hasNext()){
- _Connection _conn = (_Connection)iter.next();
- if(_conn.isInUse()){
- count++;
- }
- }
- return count;
- }
- }
下面的是一个代理类。用来实现直接用close关闭连接。
java 代码
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.sql.Connection;
- import java.sql.SQLException;
- public class _Connection implements InvocationHandler {
- private final static String CLOSE_METHOD_NAME = "close";
- private Connection conn = null;
- //数据库的忙状态
- private boolean inUse = false;
- //用户最后一次访问该连接方法的时间
- private long lastAccessTime = System.currentTimeMillis();
- _Connection(Connection conn, boolean inUse){
- this.conn = conn;
- this.inUse = inUse;
- }
- /**
- * Returns the conn.
- * @return Connection
- */
- public Connection getConnection() {
- //返回数据库连接conn的接管类,以便截住close方法
- Connection conn2 = (Connection)Proxy.newProxyInstance(
- conn.getClass().getClassLoader(),
- conn.getClass().getInterfaces(),this);
- return conn2;
- }
- /**
- * 该方法真正的关闭了数据库的连接
- * @throws SQLException
- */
- void close() throws SQLException{
- //由于类属性conn是没有被接管的连接,因此一旦调用close方法后就直接关闭连接
- conn.close();
- }
- /**
- * Returns the inUse.
- * @return boolean
- */
- public boolean isInUse() {
- return inUse;
- }
- /**
- * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object)
- */
- public Object invoke(Object proxy, Method m, Object[] args)
- throws Throwable
- {
- Object obj = null;
- //判断是否调用了close的方法,如果调用close方法则把连接置为无用状态
- if(CLOSE_METHOD_NAME.equals(m.getName()))
- setInUse(false);
- else
- obj = m.invoke(conn, args);
- //设置最后一次访问时间,以便及时清除超时的连接
- lastAccessTime = System.currentTimeMillis();
- return obj;
- }
- /**
- * Returns the lastAccessTime.
- * @return long
- */
- public long getLastAccessTime() {
- return lastAccessTime;
- }
- /**
- * Sets the inUse.
- * @param inUse The inUse to set
- */
- public void setInUse(boolean inUse) {
- &
相关推荐
经典---JAVA设计模式经典---JAVA设计模式
简介:本系列的前一篇文章讨论了智能卡的相关知识,此篇文章则将着重介绍 JAVA 卡的一些基础知识,如 JAVA 卡的概念、结构和生命期,JAVA 卡的 JAVA 语言和编程接口(API),并通过一个简单的Applet示例引入对 JAVA卡...
论坛转帖工具 .......... 纯HTML
一键转帖 一键8经验签到. 绿易贴吧工具3.7 11.11版.rar
编辑人员转帖去水印工具,很好用,直接选中水印区域后运行伪装即可!
转帖性能测试
J2ME全方位开发讲解基础汇总[转帖] 一、J2ME中需要的Java基础知识 现在有大部分人,都是从零开始学J2ME的,学习J2ME的时候,总是从Java基础开始学习,而且现在讲Java基础的书籍中都是以J2SE来讲基础,这就给学习造成...
UBB论坛转帖圣手.exeUBB论坛转帖圣手.exe
discuz X2转帖工具、采集工具,discuz论坛批量发帖,批量转帖工具
这是一个世界编程大赛第一名写的程序,很好玩的,大家试试看。
1.修改自Convert X转帖工具 2.新增批量替换关键词(原来是单个词语替换,可以利用这个功能删除一些网站的防转帖代码) 3.批量随机新增文字(新增内容可自定义,从而实现伪原创) 4.cookie记录替换和新增关键词(避免每次...
Html2UBBMaxcj_Softii论坛专用转帖工具
转帖图片提取工具可以对论坛图片附件信息进行清除,只保留图片代码,操作很简单,推荐有需要转帖图片工具的朋友下载 转帖图片提取工具使用方法: 将IP138上处理过的东西复制到上方的编辑框内,点击只要图片,下面...
gl8雨刮维修以及调试转帖.doc
转帖PLCDCSFCS三大控制系统的特点和差异.doc
Statspack 使用指南 (转帖) (Eygle eygle@itpub.net ) 作者介绍: 盖国强,曾任ITPUB MS版版主,现任Oracle数据库管理版版主。曾任职于某国家大型企业, 服务于烟草行业,开发过基于Oracle数据库的大型ERP系统,...
关于工作与生活的转帖 一篇论述精辟的文章,值得一看。
用PHP批量生成图片缩略图——活跃论坛转帖
看到论坛里帖子由精美的图片想转过来,或者批量提取地址时很好用