日期:2014-05-18  浏览次数:20718 次

关于JSP数据库连接池的问题-弄不出来真郁闷
在网上看到的一个例子,我想连ACCESS数据库,弄不出来,现把代码贴出来:
网站根目录下     ConnPool.jsp   :
<%@   page   contentType= "text/html;   charset=GBK "   %>
<%@   page   import   = "java.sql.* "   %>
<jsp:useBean   id= "connManager "   scope= "page "   class= "mybean.DBConnManager "   />
<html>
<head>
<title>
购物商城首页
</title>
</head>
<body   bgcolor= "#B0C4DE ">
<center> <h1> 欢迎访问本购物商城 </h1> </center>
<%
String   accstr=request.getRealPath( "data/shopping.mdb ");     //设置路径
connManager.setConnStr(accstr);
Connection   connA=connManager.getConnection( "access ");
if   (connA==null)
{
    out.print( "数据库正忙,请稍后再访问 ");
}
Statement   stmtA=connA.createStatement();
String   sql= "select   *   from   userinfo ";
ResultSet   rs=stmtA.executeQuery(sql);
while   (rs.next())
{
out.print( " <table   border= '0 '> ");
out.print( " <tr> ");
out.print( " <td> "+rs.getString( "username ")+ " </td> ");
out.print( " <td> "+rs.getString( "userpass ")+ " </td> ");
out.print( " </tr> </table> ");
}
rs.close();
stmtA.close();
connManager.returnConnection( "access ",connA);
%>

</body>
</html>

网站根目录   WEB-INF\classes\   下两个JAVA文件:
DBConnPool.java   :

package   mybean;
import   java.util.*;
import   java.sql.*;
public   class   DBConnPool   {
    private   int   using=0;                     //正在使用连接的数量
    private   Vector   connections=new   Vector();                         //目前可用的连接数     private   int   maxconn=0;                 //最大连接数
    private   String   poolname= " ";         //连接池名
    private   String   dbid= " ";                 //数据库标识
    private   String   drivername= " ";     //驱动程序名
    private   String   username= " ";         //数据库帐号
    private   String   passwd= " ";             //数据库密码

    public   DBConnPool(String   poolname,String   dbid,String   drivername,
                                        String   username,String   passwd,int   maxconn){
        this.poolname=poolname;
        this.drivername=drivername;
        this.dbid   =dbid;
        this.username   =username;
        this.passwd=passwd;
        this.maxconn=maxconn;
    }