⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 736726.xml

📁 论坛精华帖子
💻 XML
📖 第 1 页 / 共 2 页
字号:
<?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>各位大虾:
&#32;&#32;&#32;&#32;&#32;&#32;本人想用连接池做一个连接数据库的bean,JDK+tomcat,请问如何实现?
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
</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&#32;class&#32;ConnPool&#32;{
&#32;&#32;private&#32;static&#32;final&#32;int&#32;defaultMaxConnections=3;//默认最大连接对象数;
&#32;&#32;private&#32;Vector&#32;freeConnections;//存放还未被配置出去的连接对象;
&#32;&#32;private&#32;Hashtable&#32;boundConnections;//存放目前正被使用的连接对象;
&#32;&#32;private&#32;String&#32;driverName;
&#32;&#32;private&#32;String&#32;jdbcURL;
&#32;&#32;private&#32;String&#32;username;
&#32;&#32;private&#32;String&#32;password;
&#32;&#32;private&#32;int&#32;maxConnections;//最大连接对象数;

&#32;&#32;public&#32;ConnPool(int&#32;numConnections)&#32;{
&#32;&#32;&#32;&#32;maxConnections=numConnections;
&#32;&#32;&#32;&#32;boundConnections=null;
&#32;&#32;&#32;&#32;freeConnections=null;
&#32;&#32;&#32;&#32;driverName="";
&#32;&#32;&#32;&#32;jdbcURL="";
&#32;&#32;&#32;&#32;username="";
&#32;&#32;&#32;&#32;password="";
&#32;&#32;}
&#32;&#32;public&#32;ConnPool()&#32;{
&#32;&#32;&#32;&#32;this(defaultMaxConnections);
&#32;&#32;}
&#32;&#32;public&#32;void&#32;openDB(String&#32;drvName,String&#32;url,String&#32;uname,String&#32;passwd)&#32;throws&#32;SQLException&#32;{
&#32;&#32;&#32;&#32;try&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;boundConnections=new&#32;Hashtable(maxConnections);
&#32;&#32;&#32;&#32;&#32;&#32;freeConnections=new&#32;Vector(maxConnections);
&#32;&#32;&#32;&#32;&#32;&#32;Class.forName(drvName);
&#32;&#32;&#32;&#32;&#32;&#32;for(int&#32;i=0;i&lt;maxConnections;i++)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;freeConnections.addElement(DriverManager.getConnection(url,uname,passwd));
&#32;&#32;&#32;&#32;&#32;&#32;}
&#32;&#32;&#32;&#32;}catch(Exception&#32;ex)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;boundConnections=null;
&#32;&#32;&#32;&#32;&#32;&#32;freeConnections=null;
&#32;&#32;&#32;&#32;&#32;&#32;throw&#32;new&#32;SQLException(ex.toString());
&#32;&#32;&#32;&#32;}
&#32;&#32;}
&#32;&#32;public&#32;void&#32;closeDB()&#32;throws&#32;SQLException&#32;{
&#32;&#32;&#32;&#32;if(boundConnections!=null)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;for(Enumeration&#32;e=boundConnections.elements();e.hasMoreElements();)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;Connection&#32;conn=(Connection)e.nextElement();
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;conn.close();
&#32;&#32;&#32;&#32;&#32;&#32;}
&#32;&#32;&#32;&#32;&#32;&#32;boundConnections.clear();
&#32;&#32;&#32;&#32;&#32;&#32;boundConnections=null;
&#32;&#32;&#32;&#32;}
&#32;&#32;&#32;&#32;if(freeConnections!=null)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;for(Enumeration&#32;e=freeConnections.elements();e.hasMoreElements();)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;Connection&#32;conn=(Connection)e.nextElement();
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;conn.close();
&#32;&#32;&#32;&#32;&#32;&#32;}
&#32;&#32;&#32;&#32;&#32;&#32;freeConnections.removeAllElements();
&#32;&#32;&#32;&#32;&#32;&#32;freeConnections=null;
&#32;&#32;&#32;&#32;}
&#32;&#32;}
&#32;&#32;public&#32;synchronized&#32;Connection&#32;getConnection()&#32;throws&#32;SQLException&#32;{
&#32;&#32;&#32;&#32;if(freeConnections==null)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;throw&#32;new&#32;SQLException("The&#32;connection&#32;pool&#32;has&#32;not&#32;been&#32;established&#32;yet.");
&#32;&#32;&#32;&#32;}
&#32;&#32;&#32;&#32;if(boundConnections.get(Thread.currentThread())!=null)
&#32;&#32;&#32;&#32;&#32;&#32;throw&#32;new&#32;SQLException("Cannot&#32;get&#32;connections&#32;over&#32;once&#32;for&#32;this&#32;current&#32;running&#32;thread.");
&#32;&#32;&#32;&#32;try&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;if(freeConnections.size()==0)
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;wait();
&#32;&#32;&#32;&#32;}catch(InterruptedException&#32;ex)&#32;{
&#32;&#32;&#32;&#32;&#32;&#32;throw&#32;new&#32;SQLException(ex.toString());
&#32;&#32;&#32;&#32;}
&#32;&#32;&#32;&#32;Connection&#32;conn=(Connection)freeConnections.firstElement();
&#32;&#32;&#32;&#32;freeConnections.removeElement(conn);
&#32;&#32;&#32;&#32;boundConnections.put(Thread.currentThread(),conn);
&#32;&#32;&#32;&#32;return&#32;conn;
&#32;&#32;}
&#32;&#32;public&#32;synchronized&#32;void&#32;returnConnection()&#32;throws&#32;SQLException&#32;{
&#32;&#32;&#32;&#32;Connection&#32;conn=(Connection)boundConnections.remove(Thread.currentThread());
&#32;&#32;&#32;&#32;if(conn==null)
&#32;&#32;&#32;&#32;&#32;&#32;&#32;throw&#32;new&#32;SQLException("The&#32;connection&#32;which&#32;this&#32;current&#32;running&#32;thread&#32;got&#32;isnot&#32;found.");
&#32;&#32;&#32;&#32;freeConnections.addElement(conn);
&#32;&#32;&#32;&#32;notify();
&#32;&#32;}
&#32;&#32;public&#32;void&#32;setMaxConnections(int&#32;numConnections)&#32;{
&#32;&#32;&#32;&#32;maxConnections=numConnections;
&#32;&#32;}
&#32;&#32;public&#32;void&#32;setDriverName(String&#32;drvName)&#32;{
&#32;&#32;&#32;&#32;driverName=drvName;
&#32;&#32;}
&#32;&#32;public&#32;void&#32;setJdbcURL(String&#32;url)&#32;{
&#32;&#32;&#32;&#32;jdbcURL=url;
&#32;&#32;}
&#32;&#32;public&#32;void&#32;setUserName(String&#32;uname)&#32;{
&#32;&#32;&#32;&#32;username=uname;
&#32;&#32;}
&#32;&#32;public&#32;void&#32;setPassword(String&#32;passwd)&#32;{
&#32;&#32;&#32;&#32;password=passwd;
&#32;&#32;}
}
</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):谢谢先!如果使用,是用&lt;jsp:usebean&#32;....&gt;吧,我在每一页都需要数据库查询,每一页都得用&lt;jsp:usebean&#32;....&gt;吗?请告诉我如何使用(来个例子)!
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;谢谢!!!!!!!!!!!!!!!!!!!!!!!!</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>能否具体点?
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;谢谢!!!!!!!!!!!!!!
</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>能否再具体点?
&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;&#32;谢谢!!!!!!!!!!!!!!!!!
</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&#32;static&#32;connpool&#32;cp=new&#32;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():
&#32;&#32;能否给我发一个,最好告诉我怎么使用?
&#32;&#32;E-mail:hylcx2001@yahoo.com.cn
&#32;&#32;谢谢先!!!!!!!!!!!!!!!!!!!!!!!!!!!</Content>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -