回 帖 发 新 帖 刷新版面

主题:关于在jsp中调用javabean来连接数据库的问题!!

我建立一个添加用户的界面,我调用javabean用连接池方法来连接数据库!代码都是光盘上来的!但是测试不成功。我不知道那里错了!!望高手指点迷津!!
我把DBConnManager.class文件放在...\Tomcat 4.1\webapps\ROOT\Code\06\WEB-INF\classes\test目录下了!
jsp文件放在...\Tomcat 4.1\webapps\ROOT\Code\06下我不知道是不是所有的class文件都要放在.../web-inf/classes目录下?

add2.html如下:

<html>
<head>
<title>填写新用户信息</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="connManager" scope="application" class="test.DBConnManager" />
<body bgcolor="#CFF1E1">
<center><h2>
填写新用户信息
</h2></center>
<%
String userid=request.getParameter("userid");
if(userid==null||userid.equals("")){
out.println("用户名不能为空,<a href='javascript:history.back();'>请返回</a>");
out.close();
}
String number="1234567890";
for (int i=0;i<userid.length();i++)
{
    char achar=userid.charAt(i);    
    if(!((achar>='a'&&achar<='z')||(achar>='A'&&achar<='Z')||(achar=='_')||(number.indexOf(achar)!=-1)))
    {
        out.println("用户名只能由字母、数字和下划线组成,<a href='javascript:history.back();'>请返回</a>");
    out.close();
    }
}
char achar=userid.charAt(0);
if (!((achar>='a'&&achar<='z')||(achar>='A'&&achar<='Z')))
{
   out.println("用户名首字母必须是字母,<a href='javascript:history.back();'>请返回</a>");
   out.close();
}
%>
<%
Connection con = connManager.getConnection("mysql");
if(con==null)
{
%>对不起,现在数据库忙,请稍后再试
<%
}
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select * from Users where UserID='"+userid+"'");
if(!rs.next())
{
%>
<center>
<form action=add3.jsp method=post>
<input type=hidden name=userid value=<%=userid%>>
<table border=1>
<tr>
<td>用户名</td><td><%=userid%></td>
</tr>
<tr>
<td>姓名</td><td><input name=name></td>
</tr>
<tr>
<td>电子邮件</td><td><input name=email></td>
</tr>
<tr>
<td>住址</td><td><input name=address></td>
</tr>
</table>
<p></p>
<input type=submit value=提交>
<input type=reset value=重置>
</form>
</center>
<%    
}else
{
out.println("此用户名已有人使用!<a href='javascript:history.back();'>请返回</a>");
}
rs.close();
stmt.close();
connManager.releaseConnection("mysql",con);
%>
</body>
</html>

回复列表 (共8个回复)

沙发

/*得到一个指定连接池中的连接*/
    public Connection getConnection(String name) {
        DBConnPool pool = (DBConnPool) connPools.get(name);
        if (pool != null)
            return pool.getConnection();
        return null;
    }
    
    /*关闭所有连接*/
    public synchronized void closeConns() {
        Enumeration allPools = connPools.elements();
        while (allPools.hasMoreElements()) {
            DBConnPool pool = (DBConnPool) allPools.nextElement();
            pool.closeConn();
        }
    }
    
    /*创建连接池*/
    private void createPools() {
        for(int i = 0; i<poolnames.size();i++){
            String poolname = poolnames.elementAt(i).toString();
            String drivername = drivernames.elementAt(i).toString();
            String dbid = dbids.elementAt(i).toString();
            String username = usernames.elementAt(i).toString();
            String passwd = passwds.elementAt(i).toString();
            int maxconn=0;
            try {
                maxconn = Integer.parseInt(maxconns.elementAt(i).toString());
            }
            catch (NumberFormatException e) {
                e.printStackTrace();
            }
            DBConnPool pool = new DBConnPool(poolname, drivername, dbid, username, passwd, maxconn);
            connPools.put(poolname, pool);
        }
    }
}

错误报告如下:
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 6 in the jsp file: /jsp_cs/lwb/add2.jsp

Generated servlet error:
    [javac] Compiling 1 source file

D:\program files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\jsp_cs\lwb\add2_jsp.java:50: package test does not exist
      test.DBConnManager connManager = null;
          ^



An error occurred at line: 6 in the jsp file: /jsp_cs/lwb/add2.jsp

Generated servlet error:
D:\program files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\jsp_cs\lwb\add2_jsp.java:52: package test does not exist
        connManager = (test.DBConnManager) pageContext.getAttribute("connManager", PageContext.APPLICATION_SCOPE);
                           ^



An error occurred at line: 6 in the jsp file: /jsp_cs/lwb/add2.jsp

Generated servlet error:
D:\program files\Apache Group\Tomcat 4.1\work\Standalone\localhost\_\jsp_cs\lwb\add2_jsp.java:55: package test does not exist
            connManager = (test.DBConnManager) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "test.DBConnManager");
                               ^
3 errors


    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:130)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:293)
    at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:353)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:370)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:473)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:190)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)

板凳

DBConnManager.java如下:

package test;

import java.sql.*;
import java.util.*;

/*连接池管理类,可以管理多个数据库连接池*/
public class DBConnManager {
    //连接池名列表
    private Vector poolnames = new Vector();
    //驱动程序名列表
    private Vector drivernames = new Vector();
    //数据库标识列表
    private Vector dbids = new Vector();
    //用户名列表
    private Vector usernames = new Vector();
    //密码列表
    private Vector passwds = new Vector();
    //最大连接数列表
    private Vector maxconns = new Vector();
    //连接池队列
    private Hashtable connPools = new Hashtable();
    
    public DBConnManager() {
        //添加mysql数据库的连接信息
        poolnames.addElement("mysql");
        drivernames.addElement("org.gjt.mm.mysql.Driver");
        dbids.addElement("jdbc:mysql://localhost/testmysql");
        usernames.addElement("root");
        passwds.addElement("");
        maxconns.addElement("10");
        
        //添加access数据库的连接信息
        poolnames.addElement("access");
        drivernames.addElement("sun.jdbc.odbc.JdbcOdbcDriver");
        dbids.addElement("jdbc:odbc:TestData");
        usernames.addElement("");
        passwds.addElement("");
        maxconns.addElement("5");
        
        //创建连接池
        createPools();
    }
    
    /*将连接返回给由指定的连接池*/
    public void releaseConnection(String name, Connection con) {
        DBConnPool pool = (DBConnPool) connPools.get(name);
        if (pool != null)
            pool.releaseConnection(con);
    }
    

3 楼

报错信息报什么错?

4 楼

这个还是我不会调用javabean的问题!!!:(

5 楼

请问你将 DBConnManager.class文件置于哪个目录下? 如果你用Tomcat4,你就必须将其置于"Tomcat/classes/test/"(若是Tomcat5,则置于Tomcat5/common/classes/test下)或"Tomcat/webapps/root/web-inf/classes/test"下,将你的jsp文件置于"Tomcat/webapps/root"下.

6 楼

Tomcat4.1目录下面没有classes文件夹啊!还要新建一个吗?我也已经按照你的意思去做了!在Tomcat4.1下新建一个classes文件夹把DBConnManager.class放到.../Tomcat4.1/classes/test/下然后把add2.html放到.../Tomcat4.1下!用http://localhost:8080/add2.html测试 也没有成功啊!到底是怎么了 我死了!

7 楼

干吗非用 4.1的,5.5的多好用,
不过听说 4.1的要访在 beans\jsp  下把,我也没装过

8 楼

[em19]

我来回复

您尚未登录,请登录后再回复。点此登录或注册