JSP MySQL Class.forName error

2019-09-02 05:52发布

问题:

I'm new to JSP. I'm trying to do a basic program of JSP & MySQL connectivity using ADT Eclipse. On running the index.jsp page, I get the following:

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

I downloaded MySQL Connector J jar and included it in the project

Thanks in advance...

回答1:

Don't add the external jar. Download it and copy in project.

COPY it in WEB-INF--->lib(folder).



回答2:

The error ,

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

says that it couldn't find the class file .

So you should check for the mysql jar to the the project using ,

Project properties-> add jar

before that place the jar in folder somewhere inside the project or add through external jar option.

If you already added . trying remove and adding the jar file again

see how-to-add-mysql-driver-jar-file-in-eclipse and how-to-add-external-jar-without-eclipse for addtional reference