高分求解:我这样的配置如何使用连接池? 1、连接池一般要你自己写javaBean,不需要配置,调用就可以的。2、调用Bean你应该会吧。3、在Bean中写建立连接、释放连接的方法调用就可以了。如果你需要的话,我可以给一个例子给你,一两句话很难说清楚。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我也在研究这个这是我刚找到的一个,以bean的形式使用的连接池具体我还没弄明白,共同研究import java.lang.*;import java.sql.*;import java.util.*;import java.io.*;import javax.servlet.*;public class ConnectionPool{ private Vector freeConnections = null; private Hashtable nowConnections = null; private String driverName = "sun.jdbc.odbc.JdbcOdbcDriver"; private String jdbcURL = "jdbc:odbc:software"; private int maxConnections = 3; public ConnectionPool () { } public void openPool () throws SQLException { try { nowConnections = new Hashtable (maxConnections); freeConnections = new Vector (maxConnections); Class.forName (driverName); for (int i = 0; i < maxConnections; i ++) freeConnections.addElement (DriverManager.getConnection (jdbcURL)); } catch (Exception ex) { nowConnections = null; freeConnections = null; throw new SQLException (ex.toString ()); } } public void closePool () throws SQLException { if (nowConnections != null) { for (Enumeration e = nowConnections.elements (); e.hasMoreElements (); ) ((Connection) e.nextElement ()).close (); nowConnections.clear (); nowConnections = null; } if (freeConnections != null) { for (Enumeration e = freeConnections.elements (); e.hasMoreElements (); ) ((Connection) e.nextElement ()).close (); freeConnections.removeAllElements (); freeConnections = null; } } public Connection getConnection () throws SQLException { if (freeConnections == null) throw new SQLException ("ConnectionPool还没有建立!"); if (freeConnections.size () == 0) throw new SQLException ("没有空闲的连接,请稍后再请求!"); Connection Conn = (Connection) freeConnections.firstElement (); freeConnections.removeElement (Conn); nowConnections.put (Thread.currentThread (), Conn); return Conn; } public void returnConnection () throws SQLException { Connection Conn = (Connection) nowConnections.remove (Thread.currentThread ()); freeConnections.addElement (Conn); } public void setPoolSwitch (String on_off) throws ServletException { try { if (on_off.equalsIgnoreCase ("ON")) openPool (); if (on_off.equalsIgnoreCase ("OFF")) closePool (); } catch (SQLException ex) { throw new ServletException (ex.toString ()); } } public void setMaxConnections (int maxConnections) { this.maxConnections = maxConnections; } public void setDriverName (String driverName) { this.driverName = driverName; } public void setJdbcURL (String jdbcURL) { this.jdbcURL = jdbcURL; }} 1。我听说Tomcat 4.x可以在服务器上配置连接池了,但是我还不知道怎么用2。调用的时候是每一页都调用和释放这个bean吗?调用的时候是一个什么样的流程,从而提高了访问效率呢,我不是很懂,能解释一下吗? 如果可以,给我发一个例子吧,详细一点最好:[email protected] To:windyloft(侠客行) 怎么和我上次贴上去的内容一模一样?其实内容很简单:在该类中定义了一个Vector对象,一个Hashtable对象,它们两个共同构成了一个虚拟的连接池,在Vector对象中存放连接池中剩余的数据库连接,而Hashtable中存放连接池已被使用的连接,其他方法只是对它们进行操作而已,也就是两方面:一、不再使用的连接放回池中,二、要使用连接时,从池中取出连接,三、当池中连接全部取出时,若此时需要连接,则系统等待,直到池中有空闲的连接为止。 to saintKnight(saintKnight) :请问,我如果要调用这个类,怎么用?是不是在最开始的页面调用openPool,然后需要调用数据库的页面调用getConnection,那么什么时候closePool呢?能不能说详细点啊,你上次贴的内容我没找到。 to:jesun (天平座) 你的系统能不能在用户访问目录时,自动显示目录下的index.jsp文件。如果能,告诉俺是怎么实现的? to:jesun (天平座) 你的系统能不能在用户访问目录时,自动显示目录下的index.jsp文件?如果能,麻烦你告诉俺是怎么实现的! to:jesun (天平座) 你的系统能不能在用户访问目录时,自动显示目录下的index.jsp文件?如果能,麻烦你告诉俺是怎么实现的! 可以在servlet中配置让应用系统一启动就实例化此类的一个对象,并且设定为application范围,则在整个应用程序中随处可以调用,而连接池是不需要关闭的,因为它是整个应用程序范围的全局变量性质,只要应用程序关闭它也是自动关闭了。 “可以在servlet中配置让应用系统一启动就实例化此类的一个对象,并且设定为application范围”————有没有具体代码?应用系统一启动是什么时候,在哪里配置?是server.xml吗? 就象配置环境变量一样,在环境变量设定了初始化参数,系统启动的时候要根据初始化参数来进行其它方面的配置,建议你看一下J2EE服务器编程这本书,前面好象是第三章讲到了这方面的内容。是指在编聊天室的时候首先怎么初始化系统的基本参数的 to:saintKnight(saintKnight)我知道把JAVABEAN设为APPLICATION,但不知道把SERVERLET设为APPLICATION范围,系统一启动就实例化此类的一个对象,能说清楚一点吗?谢谢。 http://jakarta.apache.org/tomcat/tomcat-4.0-doc/jndi-resources-howto.html 学了PHP有必要再去学JSP吗? 关于jsp和js的问题 ext4.0 可编辑的grid 一个非常奇怪的问题,关于struts的,request.getPetemeter 表单提交到另外一个表单提交页面出现的问题 问一下sqlserver2000和java数据类型的关系 SmartUpload使用问题(十万火急) 动态显示结果 ASP调用EXE文件遇上的问题 最强的,增加博客访问量的超级办法。如果你按我的方法去做。访问两没有达到7W以上。我不是人。你砍死我。。。。 请问谁能告诉我jsp链接mssql问题 在ASP中可以轻易实现,不知道在JSP中能否实现?
这是我刚找到的一个,以bean的形式使用的连接池
具体我还没弄明白,共同研究
import java.lang.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;public class ConnectionPool
{
private Vector freeConnections = null;
private Hashtable nowConnections = null;
private String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
private String jdbcURL = "jdbc:odbc:software";
private int maxConnections = 3;
public ConnectionPool ()
{
}
public void openPool () throws SQLException
{
try
{
nowConnections = new Hashtable (maxConnections);
freeConnections = new Vector (maxConnections);
Class.forName (driverName);
for (int i = 0; i < maxConnections; i ++)
freeConnections.addElement (DriverManager.getConnection (jdbcURL));
}
catch (Exception ex)
{
nowConnections = null;
freeConnections = null;
throw new SQLException (ex.toString ());
}
} public void closePool () throws SQLException
{
if (nowConnections != null)
{
for (Enumeration e = nowConnections.elements (); e.hasMoreElements (); )
((Connection) e.nextElement ()).close ();
nowConnections.clear ();
nowConnections = null;
}
if (freeConnections != null)
{
for (Enumeration e = freeConnections.elements (); e.hasMoreElements (); )
((Connection) e.nextElement ()).close ();
freeConnections.removeAllElements ();
freeConnections = null;
}
} public Connection getConnection () throws SQLException
{
if (freeConnections == null)
throw new SQLException ("ConnectionPool还没有建立!");
if (freeConnections.size () == 0)
throw new SQLException ("没有空闲的连接,请稍后再请求!"); Connection Conn = (Connection) freeConnections.firstElement ();
freeConnections.removeElement (Conn);
nowConnections.put (Thread.currentThread (), Conn); return Conn;
}
public void returnConnection () throws SQLException
{
Connection Conn = (Connection) nowConnections.remove (Thread.currentThread ());
freeConnections.addElement (Conn);
} public void setPoolSwitch (String on_off) throws ServletException
{
try
{
if (on_off.equalsIgnoreCase ("ON"))
openPool ();
if (on_off.equalsIgnoreCase ("OFF"))
closePool ();
}
catch (SQLException ex)
{
throw new ServletException (ex.toString ());
}
} public void setMaxConnections (int maxConnections)
{
this.maxConnections = maxConnections;
}
public void setDriverName (String driverName)
{
this.driverName = driverName;
}
public void setJdbcURL (String jdbcURL)
{
this.jdbcURL = jdbcURL;
}
}
2。调用的时候是每一页都调用和释放这个bean吗?调用的时候是一个什么样的流程,从而提高了访问效率呢,我不是很懂,能解释一下吗?
[email protected]
怎么和我上次贴上去的内容一模一样?
其实内容很简单:
在该类中定义了一个Vector对象,一个Hashtable对象,它们两个共同构成了一个虚拟的连接池,在Vector对象中存放连接池中剩余的数据库连接,而Hashtable中存放连接池已被使用的连接,其他方法只是对它们进行操作而已,也就是两方面:一、不再使用的连接放回池中,
二、要使用连接时,从池中取出连接,
三、当池中连接全部取出时,若此时需要连接,则系统等待,直到池中有空闲的连接为止。
请问,我如果要调用这个类,怎么用?
是不是在最开始的页面调用openPool,然后需要调用数据库的页面调用getConnection,那么什么时候closePool呢?
能不能说详细点啊,你上次贴的内容我没找到。
如果能,麻烦你告诉俺是怎么实现的!
如果能,麻烦你告诉俺是怎么实现的!
————有没有具体代码?应用系统一启动是什么时候,在哪里配置?是server.xml吗?
我知道把JAVABEAN设为APPLICATION,但不知道把SERVERLET设为APPLICATION范围,系统一启动就实例化此类的一个对象,能说清楚一点吗?谢谢。