我做了个jsp连接数据库的例子来测试我的tomcat服务器,用了javabean,但抛出了异常,错误如下:
希望大虾指教,谢谢哦。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
type Exception reportmessagedescription The server encountered an internal error () that prevented it from fulfilling this request.exceptionorg.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 2 in the jsp file: /index.jsp
Generated servlet error:
Driver.jdbc cannot be resolved or is not a typeAn error occurred at line: 2 in the jsp file: /index.jsp
Generated servlet error:
Driver.jdbc cannot be resolved or is not a typeAn error occurred at line: 2 in the jsp file: /index.jsp
Generated servlet error:
Driver.jdbc cannot be resolved or is not a type
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
Apache Tomcat/5.5.9
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
其中jsp代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<jsp:useBean id="jdbc" scope="page" class="Driver.jdbc"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>我要usebean!</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
</style>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%     Connection con = jdbc.getConn() ;
       Statement stmt = con.createStatement();
       String strSQL = "select * from user";
   ResultSet rs = stmt.executeQuery(strSQL);
       while(rs.next())
   {
    String id = rs.getString("1");
String name = rs.getString("2");
 %>
          <tr>
           <td width="95%" class="A">id=<%=id%>        name=<%=name%>
            <hr></td>
   
          </tr>
<%

}
%>
</body>
</html>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++其中jdbc.class我放在了ub(本项目在webapps下)的\WEB-INF\classes\Driver下,即
C:\Tomcat 5.5\webapps\ub\WEB-INF\classes\Driver下
但不用usebean方式却可以正常访问,也可以调用数据库。不管怎样,谢谢阅读。

解决方案 »

  1.   

    其中我的bean 的写法:package Driver;
    import java.sql.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;public class jdbc {
     
     Connection conn = null ;
     String re = "" ;
     String dbip = "127.0.0.1" ;
     String use = "root" ; 
     String pass = "root" ;
     String dbName = "test" ;
     public jdbc(){} 
     public java.sql.Connection getConn(){
        try{
         Class.forName("org.gjt.mm.mysql.Driver").newInstance();
         String url ="jdbc:mysql://"+dbip+":3306/"+dbName+"?user="+use+"&password="+pass+"&useUnicode=true&characterEncoding=gb2312" ;
         conn= DriverManager.getConnection(url);    }
        catch(Exception e){
        e.printStackTrace();
        }
        return this.conn ;
      }  public String ex_chinese(String s){
         if(s==null){
         s = "" ;
         }
         else{
             try {
            s = new String(s.getBytes("iso-8859-1"),"gb2312") ;
             }
             catch (Exception exception) {
             }
         }
         return s ;
      } public String getTime()
        {
            String s = "";
            try
            {
                SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-M-d HH:ss");
                Date date = new Date();
                s = simpledateformat.format(new Date());
            }
            catch(Exception exception) { }
            return s;
        }//public static void main(String[] args) {
        //UntitledDao dao = new UntitledDao();
        //ResultSet rs = dao.executeQuery("select * from jobs");
        //dao.closedb();
    //} }
      

  2.   

    错误报得很清楚啊
    Driver.jdbc cannot be resolved or is not a type
    Driver.jdbc是不能识别的符号或不是对应的类型
      

  3.   

    你在Server.xml中是如何配置你的应用的?
      

  4.   

    Driver.jdbc楼主的 JAVA 是哪里学地?包名全小写,类名首字大写,这是最一般的编码规范。但这好像不足以引发异常。不管怎么说,楼主先改一下,看上去真别扭。
      

  5.   

    已搞定:
    第一:包和bean的名字最好用小写;
    第二:在jsp中写对路径。