刚学jsp,现在用javabean做连接数据库的类,Connection和Statement都能够找到,但是结果集ResultSet总是出错,不用bean直接放到jsp页面里结果正常不报错,应该可以排除配置什么的问题,就不知道结果集的bean哪里出错了,望赐教。数据库类如下:
import java.sql.*;
public class DB {

private Connection conn;
private Statement stmt;
private ResultSet rs;
public DB(){
                    此处加载驱动,代码省略
}
public Connection getConn(){
                    此处建立连接,代码省略
}
public Statement getStmt(){
try{
conn=getConn();
stmt=conn.createStatement();
}catch(Exception e){
        e.printStackTrace();

}
return stmt;
}
就这里调用getRs()时jsp页面报错,而在jsp里可以直接调用getStmt()然后直接定义rs又可以通过,不知道哪里有问题
public ResultSet getARs(String sql){
try{
stmt=getStmt();
rs=stmt.executeQuery(sql);
}catch(Exception e){
e.printStackTrace();
}
return rs;
}
 

}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主截止到2008-06-18 22:34:19的汇总数据:
    发帖数:6
    结贴数:0
    结贴率: 0.00%
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
      

  2.   

    出错信息:
    org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 28 in the jsp file: /rygl/sjwh.jsp
    The method getARs(String) is undefined for the type DB

    28:   ResultSet rs=db.getARs(sql);
    Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:93)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:435)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:298)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:277)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:265)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:302)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.26 logs.
      

  3.   

    The method getARs(String) is undefined for the type DB 你的getARs(String)在DB这个类里面没有定义。看看类Db是否有错误。
    重新编译一下。并清空{tomcat}\work\Catalina目录
      

  4.   

    你在调用getARs(String sql)这个方法之前是不是确定你自己已经先调用了getConn()再调用getStmt(){ 这个方法,然后才调用你的这个getARs(String sql)方法,你再自己看一上,应当就这里的问题
      

  5.   

     ResultSet rs=db.getARs(sql);  这一句前把conn关了吧?
     不能在这前关的
      

  6.   

    type Exception reportmessage 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: 29 in the jsp file: /rygl/sjwh.jsp
    The method getARs(String) is undefined for the type DB
    28:   String sql="select * from zw";
    29:   ResultSet rs=db.getARs(sql);
    30:   while(rs.next()){
    31:   %>
    32:   <tr>应该很清楚了啊,如果不用db.getARs()而直接定义ResultSet的话可以通过,这个卡住了,后面只要涉及到数据集的方法都调用不了了,郁闷啊
      

  7.   

    type Exception reportmessage 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: 29 in the jsp file: /rygl/sjwh.jsp
    The method getARs(String) is undefined for the type DB
    28:   String sql="select * from zw";
    29:   ResultSet rs=db.getARs(sql);
    30:   while(rs.next()){
    31:   %>
    32:   <tr>应该很清楚了啊,如果不用db.getARs()而直接定义ResultSet的话可以通过,这个卡住了,后面只要涉及到数据集的方法都调用不了了,郁闷啊
      

  8.   

     ResultSet rs=db.getARs(sql);  这一句前把conn关了吧?
     不能在这前关的
      

  9.   

    还是这个错误。
    这个是关键An error occurred at line: 29 in the jsp file: /rygl/sjwh.jsp 
    The method getARs(String) is undefined for the type DB 你的jsp和javabean编译都没有通过呢。
      

  10.   

    上面方法都试了下,还是同样的错, 把整个类发过来吧package com.db;
    import java.sql.*;
    public class DB {
    int n;
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;
    private String name="sun.jdbc.odbc.JdbcOdbcDriver";
    private String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\webtest\\db\\db.mdb";
    public DB(){
    try{
    Class.forName(name);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    public Connection getConn(){
    try{
    conn=DriverManager.getConnection(url);
    }catch(Exception e){
    conn=null;
    }
    return conn;
    }

    public Statement getStmt(){
    try{
    conn=getConn();
    stmt=conn.createStatement();
    }catch(Exception e){
    e.printStackTrace();

    }
    return stmt;
    }
    public ResultSet getRs(String sql){
    try{
    stmt=getStmt();
    rs=stmt.executeQuery(sql);
    }catch(Exception e){
    e.printStackTrace();
    }
    return rs;
    }
    public int Insert(String sql){
    try{
    n=stmt.executeUpdate(sql);
    }catch(Exception e){
    return 0;
    }
    return n;
    }

    }错误还是找不到方法type Exception reportmessage 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: 29 in the jsp file: /rygl/sjwh.jsp
    The method getRs(String) is undefined for the type DB
    26:   //Statement stmt=db.getStmt();
    27:   //ResultSet rs=stmt.executeQuery(sql);
    28:   String sql="select * from zw";
    29:   ResultSet rs=db.getRs(sql);
    30:   while(rs.next()){
    31:   %>
    32:   <tr>
    getRs反复看了好多遍都没发现问题啊,而且用26,27行就可以通过了,实在搞不懂了
      

  11.   

    jsp页面也发出来
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><jsp:useBean id="db" class="com.db.DB" scope="page"/>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body><table width="500" height="137" border="1" cellpadding="0" cellspacing="1" bgcolor="">
       <%
      int i=1;
      //String sql="select members.name,zw.zwname from members,zw where members.zwid=zw.zwid";
      //Statement stmt=db.getStmt();
      //ResultSet rs=stmt.executeQuery(sql);
      String sql="select * from zw";
      ResultSet rs=db.getRs(sql);
      while(rs.next()){
      %>
      <tr>
        <td><%=i++%></td>
        <td><%=rs.getString(1)%></td>
        <td><%=rs.getString(2)%></td>    
          </tr>
      <%}%>
      <tr>
        <td colspan="5"><div align="right"><a href="add.jsp">增加</a></div></td>
        </tr>
    </table></body>
    </html>
      

  12.   

    你的程序应该没错。建议重新编译部署一下。并清空{tomcat}\work\Catalina目录
      

  13.   

    真是部署问题,我把类的包换了下,再清空了catalina目录,程序通过了,谢谢!