📄 736726.xml
字号:
<?xml version='1.0' encoding='GB2312'?>
<?xml-stylesheet type='text/xsl' href='../csdn.xsl'?>
<Topic>
<Issue>
<PostUserNickName>雨</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<TopicId>736726</TopicId>
<TopicName>Jsp连接池</TopicName>
<PostUserId>211276</PostUserId>
<PostUserName>hylcx</PostUserName>
<RoomName>JSP</RoomName>
<ReplyNum>20</ReplyNum>
<PostDateTime>2002-5-20 12:57:20</PostDateTime>
<Point>100</Point>
<ReadNum>0</ReadNum>
<RoomId>28</RoomId>
<EndState>2</EndState>
<Content>各位大虾:
      本人想用连接池做一个连接数据库的bean,JDK+tomcat,请问如何实现?
            谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
</Content>
</Issue>
<Replys>
<Reply>
<PostUserNickName>saintKnight</PostUserNickName>
<rank>五级(中级)</rank>
<ranknum>user5</ranknum>
<credit>100</credit>
<ReplyID>4804762</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>237709</PostUserId>
<PostUserName>saintKnight</PostUserName>
<Point>50</Point>
<Content>public class ConnPool {
  private static final int defaultMaxConnections=3;//默认最大连接对象数;
  private Vector freeConnections;//存放还未被配置出去的连接对象;
  private Hashtable boundConnections;//存放目前正被使用的连接对象;
  private String driverName;
  private String jdbcURL;
  private String username;
  private String password;
  private int maxConnections;//最大连接对象数;
  public ConnPool(int numConnections) {
    maxConnections=numConnections;
    boundConnections=null;
    freeConnections=null;
    driverName="";
    jdbcURL="";
    username="";
    password="";
  }
  public ConnPool() {
    this(defaultMaxConnections);
  }
  public void openDB(String drvName,String url,String uname,String passwd) throws SQLException {
    try {
      boundConnections=new Hashtable(maxConnections);
      freeConnections=new Vector(maxConnections);
      Class.forName(drvName);
      for(int i=0;i<maxConnections;i++) {
        freeConnections.addElement(DriverManager.getConnection(url,uname,passwd));
      }
    }catch(Exception ex) {
      boundConnections=null;
      freeConnections=null;
      throw new SQLException(ex.toString());
    }
  }
  public void closeDB() throws SQLException {
    if(boundConnections!=null) {
      for(Enumeration e=boundConnections.elements();e.hasMoreElements();) {
        Connection conn=(Connection)e.nextElement();
        conn.close();
      }
      boundConnections.clear();
      boundConnections=null;
    }
    if(freeConnections!=null) {
      for(Enumeration e=freeConnections.elements();e.hasMoreElements();) {
        Connection conn=(Connection)e.nextElement();
        conn.close();
      }
      freeConnections.removeAllElements();
      freeConnections=null;
    }
  }
  public synchronized Connection getConnection() throws SQLException {
    if(freeConnections==null) {
      throw new SQLException("The connection pool has not been established yet.");
    }
    if(boundConnections.get(Thread.currentThread())!=null)
      throw new SQLException("Cannot get connections over once for this current running thread.");
    try {
      if(freeConnections.size()==0)
        wait();
    }catch(InterruptedException ex) {
      throw new SQLException(ex.toString());
    }
    Connection conn=(Connection)freeConnections.firstElement();
    freeConnections.removeElement(conn);
    boundConnections.put(Thread.currentThread(),conn);
    return conn;
  }
  public synchronized void returnConnection() throws SQLException {
    Connection conn=(Connection)boundConnections.remove(Thread.currentThread());
    if(conn==null)
       throw new SQLException("The connection which this current running thread got isnot found.");
    freeConnections.addElement(conn);
    notify();
  }
  public void setMaxConnections(int numConnections) {
    maxConnections=numConnections;
  }
  public void setDriverName(String drvName) {
    driverName=drvName;
  }
  public void setJdbcURL(String url) {
    jdbcURL=url;
  }
  public void setUserName(String uname) {
    username=uname;
  }
  public void setPassword(String passwd) {
    password=passwd;
  }
}
</Content>
<PostDateTime>2002-5-20 13:24:35</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>晓彬</PostUserNickName>
<rank>两星(中级)</rank>
<ranknum>star2</ranknum>
<credit>135</credit>
<ReplyID>4804855</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>173450</PostUserId>
<PostUserName>Andrawu</PostUserName>
<Point>0</Point>
<Content>这一下子是很说清楚的。
你可以去下载jive论坛去看看它的源码,jive里面有很好的连接池。
你要买本有JDBC的书籍,配合jive自己去研究吧。
如果你要简单我也可以给一个。你也只能是做为参考。</Content>
<PostDateTime>2002-5-20 13:29:12</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>雨</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>4846741</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>211276</PostUserId>
<PostUserName>hylcx</PostUserName>
<Point>0</Point>
<Content>saintKnight(saintKnight):谢谢先!如果使用,是用<jsp:usebean ....>吧,我在每一页都需要数据库查询,每一页都得用<jsp:usebean ....>吗?请告诉我如何使用(来个例子)!
        谢谢!!!!!!!!!!!!!!!!!!!!!!!!</Content>
<PostDateTime>2002-5-22 16:21:49</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>saintKnight</PostUserNickName>
<rank>五级(中级)</rank>
<ranknum>user5</ranknum>
<credit>100</credit>
<ReplyID>4848297</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>237709</PostUserId>
<PostUserName>saintKnight</PostUserName>
<Point>0</Point>
<Content>可以写在一个包含文件中,</Content>
<PostDateTime>2002-5-22 17:31:41</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>雨</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>4848355</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>211276</PostUserId>
<PostUserName>hylcx</PostUserName>
<Point>0</Point>
<Content>能否具体点?
          谢谢!!!!!!!!!!!!!!
</Content>
<PostDateTime>2002-5-22 17:34:36</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>saintKnight</PostUserNickName>
<rank>五级(中级)</rank>
<ranknum>user5</ranknum>
<credit>100</credit>
<ReplyID>4848534</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>237709</PostUserId>
<PostUserName>saintKnight</PostUserName>
<Point>0</Point>
<Content>或者写在系统变量中即定义在application范围的变量中,在每个JSP页面只要使用pageContext.getServletContext().getInitParameter("")就可调用。</Content>
<PostDateTime>2002-5-22 17:42:09</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>卧龙</PostUserNickName>
<rank>四级(中级)</rank>
<ranknum>user4</ranknum>
<credit>100</credit>
<ReplyID>4848643</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>223374</PostUserId>
<PostUserName>wjfxiao</PostUserName>
<Point>0</Point>
<Content>连接池指是基础啊!你肯定要在其他的方法中使用连接池的方法,从连接池中得到一个connection。你不会是在页面上才直接得到一个连接的吧?
</Content>
<PostDateTime>2002-5-22 17:46:58</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>雨</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>4854663</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>211276</PostUserId>
<PostUserName>hylcx</PostUserName>
<Point>0</Point>
<Content>能否再具体点?
           谢谢!!!!!!!!!!!!!!!!!
</Content>
<PostDateTime>2002-5-23 9:57:09</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>凯恩</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>4855670</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>74427</PostUserId>
<PostUserName>sufish</PostUserName>
<Point>0</Point>
<Content>在类中定义一个public static connpool cp=new connpool();每次使用这个对象就可以了</Content>
<PostDateTime>2002-5-23 10:37:28</PostDateTime>
</Reply>
<Reply>
<PostUserNickName></PostUserNickName>
<rank>三级(初级)</rank>
<ranknum>user3</ranknum>
<credit>100</credit>
<ReplyID>4864263</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>34221</PostUserId>
<PostUserName>artgolf</PostUserName>
<Point>0</Point>
<Content>1、下载一个现成的连接池源程序吧,自己做比较费劲,可能考虑不周造成将来的隐患,我就是用现成的连接池源程序做的数据库应用,非常好。
2、Tomcat4中自带连接池功能,也可以用它做,但做成的应用不好移植到其他种类的JSP服务器。</Content>
<PostDateTime>2002-5-23 16:26:19</PostDateTime>
</Reply>
<Reply>
<PostUserNickName>雨</PostUserNickName>
<rank>一级(初级)</rank>
<ranknum>user1</ranknum>
<credit>100</credit>
<ReplyID>4865588</ReplyID>
<TopicID>736726</TopicID>
<PostUserId>211276</PostUserId>
<PostUserName>hylcx</PostUserName>
<Point>0</Point>
<Content>artgolf():
  能否给我发一个,最好告诉我怎么使用?
  E-mail:hylcx2001@yahoo.com.cn
  谢谢先!!!!!!!!!!!!!!!!!!!!!!!!!!!</Content>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -