JSP MySQL Class.forName错误

时间:2014-07-09 09:02:39

标签: java mysql jsp java-ee jdbc

我是JSP的新手。我正在尝试做一个JSP& amp;的基本程序。使用ADT Eclipse进行MySQL连接。在运行index.jsp页面时,我得到以下内容:

HTTP Status 500 - An exception occurred processing JSP page /index.jsp at line 24

type Exception report

message An exception occurred processing JSP page /index.jsp at line 24

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 24

21: String sql = "select usertype from userdetail";
22: 
23: try {
24: Class.forName("com.mysql.jdbc.Driver");
25: con = DriverManager.getConnection(url, user, password);
26: ps = con.prepareStatement(sql);
27: rs = ps.executeQuery(); 


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:141)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Class.java:190)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:85)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.30 logs.

Apache Tomcat/7.0.30

我下载了MySQL Connector J jar并将其包含在项目中

Project Explorer

提前致谢...

2 个答案:

答案 0 :(得分:4)

不要添加外部jar。下载并复制到项目中。

WEB-INF--->lib(folder)中复制它。

答案 1 :(得分:0)

错误,

  

javax.servlet.ServletException:java.lang.ClassNotFoundException:   com.mysql.jdbc.Driver

说它无法找到类文件。

因此,您应该使用

检查项目的mysql jar

项目属性 - >添加jar

之前将jar放在项目内某处的文件夹中,或通过外部jar选项添加。

如果您已添加。尝试删除并再次添加jar文件

请参阅how-to-add-mysql-driver-jar-file-in-eclipsehow-to-add-external-jar-without-eclipse了解其他参考资料