我的文件目录为:test/
----WEB-INF/
--------lib/
------------mysql-connector-java-5.1.7-bin.jar
--------classes/
------------ConnMysql.class
----index.jspConnMysql.javaimport java.sql.*;public class ConnMysql {
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");                     
String url ="jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=GBK";
return DriverManager.getConnection(url);
} catch (Exception e) {
System.out.print(e.getMessage());
return null;
}
    }
}
index.jsp<%@page import="java.sql.*" %>
<%@page import="ConnMysql" %>
<%
Connection conn = null;
Statement stat = null;
ResultSet rs = null ; try{
conn = ConnMysql.getConnection();
stat = conn.createStatement();
rs = stat.executeQuery("SELECT * FROM test");
while (rs.next()){
out.print(rs.getString("id") + ":");  
out.println(rs.getString("name"));
}
}catch(Exception e){
out.println(e.getMessage());
}
%>
但是打开index.jsp时显示错误HTTP Status 500 - --------------------------------------------------------------------------------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: 7 in the generated java file
The import ConnMysql cannot be resolvedAn error occurred at line: 9 in the jsp file: /index.jsp
ConnMysql cannot be resolved
6:  ResultSet rs = null ;
7: 
8:  try{
9:  conn = ConnMysql.getConnection();
10:  stat = conn.createStatement();
11:  rs = stat.executeQuery("SELECT * FROM test");
12:  while (rs.next()){
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.
--------------------------------------------------------------------------------Apache Tomcat/6.0.29
应该是说找不到ConnMysql这个类吧,请问各位高手们应该怎么做才能解决这个问题

解决方案 »

  1.   

    stat = conn.createStatement();
    这句话出错,不应该这么写
    1.既然你用封装,应该在ConnMysql里定义好自己的conn方法,然后直接用ConnMysql.getStatement(conn)里调用,
    2.而且你在index里没有关闭相关的资源,这些关闭的方法应该放在ConnMysql类里,然后直接传参调用之
    3.catch里最好把各个Exception写明,如SQLException,等等,不应图省事只写个Exception,否则程序执行效率很低,特别在大型的程序之下,跟别人把Exception写清楚的那些程序相比,这种差距就会体现出来了
      

  2.   

    我照着1楼的说法改写了两个源文件<%@page import="java.sql.*" %>
    <%@page import="ConnMysql" %>
    <%
    Connection conn = null;
    Statement stat = null;
    ResultSet rs = null ; try{
    stat = ConnMysql.getStatement();
    rs = stat.executeQuery("SELECT * FROM test");
    while (rs.next()){
    out.print(rs.getString("id") + ":");  
    out.println(rs.getString("name"));
    }
    rs.close();
    stat.close();
    conn.close();
    }catch(Exception e){
    out.println(e.getMessage());
    }
    %>
    import java.sql.*;public class ConnMysql {
    public static Statement getStatement(){
    try {
    Class.forName("com.mysql.jdbc.Driver");                     
    String url ="jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=GBK";
    Connection conn = DriverManager.getConnection(url);
    return conn.createStatement();
    } catch (Exception e) {
    System.out.print(e.getMessage());
    return null;
    }
        }
    }
    对了,jsp的错误好像有两种
    第一种:
    HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 74:  Connection conn = null;
    5:  Statement stat = null;
    6:  ResultSet rs = null ;
    7: 
    8:  try{
    9:  stat = ConnMysql.getStatement();
    10:  rs = stat.executeQuery("SELECT * FROM test");
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    root cause java.lang.NullPointerException
    org.apache.jsp.index_jsp._jspService(index_jsp.java:61)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.
    --------------------------------------------------------------------------------Apache Tomcat/6.0.29
    第二种:
    HTTP Status 500 - --------------------------------------------------------------------------------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: 7 in the generated java file
    The import ConnMysql cannot be resolvedAn error occurred at line: 9 in the jsp file: /index.jsp
    ConnMysql cannot be resolved
    6:  ResultSet rs = null ;
    7: 
    8:  try{
    9:  stat = ConnMysql.getStatement();
    10:  rs = stat.executeQuery("SELECT * FROM test");
    11:  while (rs.next()){
    12:  out.print(rs.getString("id") + ":");  
    Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.
    --------------------------------------------------------------------------------Apache Tomcat/6.0.29怎么错误还会变来变去?