java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

时间:2014-02-15 16:42:46

标签: java jsp jdbc classnotfoundexception

请任何人救我的项目! 我正在用JSP编写我的第一个真实/作业 - 项目,即我缺乏JSP经验,但具备该语言的基本知识。

我怀疑错误来自'J Connector'bcoz它缺少.jar扩展名,或问题是CLASSPATH或PATH或FILE PATH。

我创建了'form.jsp'& 'login.jsp page'& 'registration.jsp页面'。在测试时,我收到以下消息:

`HTTP Status 500 - An exception occurred processing JSP page /login.jsp at line 6

type Exception report

message An exception occurred processing JSP page /login.jsp at line 6

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 /login.jsp at line 6

3: <%
4:  String userid = request.getParameter("uname");
5:  String pwd = request.getParameter("pass");
6:  Class.forName("com.mysql.jdbc.Driver");
7:  Connection con = DriverManager.getConnection
8:  ("jdbc:mysql://localhost:3306/REG",
9:      "root", "password");


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:728)

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.login_jsp._jspService(login_jsp.java:93)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
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:728)

root cause

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
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:169)
org.apache.jsp.login_jsp._jspService(login_jsp.java:72)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
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:728)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.50 logs.
Apache Tomcat/7.0.50
`

form.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>Login</title>
        </head>
        <body>
            <form method="post" action="login.jsp">
                <center>
                    <table border="1" width="30%" cellpadding="3">
                    <thead>
                        <tr>
                            <th colspan="2">Login Here</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>User Name:</td>
                            <td><input type="text" name="uname" value="" /></td>
                        </tr>
                        <tr>
                            <td>Password:</td>
                            <td><input type="password" name="pass" value=""/></td>
                        </tr>
                        <tr>
                            <td><input type="submit" value="Login" /></td>
                            <td><input type="reset" value="Reset" /></td>
                        </tr>
                        <tr>
                            <td colspan="2">Yet Not Registered!!<a href="reg.jsp">Register Here</a> </td>
                        </tr>
                    </tbody>    
                    </table>
                </center>
            </form>
        </body>
    </html>

的login.jsp

<%@ page import="java.io.*,java.util.*,java.sql.*" %>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%
    String userid = request.getParameter("uname");
    String pwd = request.getParameter("pass");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection
    ("jdbc:mysql://localhost:3306/REG",
        "root", "password");
    Statement st = con.createStatement();
    ResultSet rs;
    rs = st.executeQuery("select * from Members where uname='" + userid + "' and pass='" + pwd + "'");
    if (rs.next()) {
    session.setAttribute("userid", userid);
    //out.println("Welcome" + userid);
    //out.println("<a href='logout.jsp'>Log out</a>");
    response.sendRedirect("Success.jsp");
    } else {
        out.println("Invalid password <a href='form.jsp'> try again </a>");
   }
    %>

database ='REG'table ='Members' * - mysql命令行。 *

CREATE TABLE Members (
   id int(10) unsigned NOT NULL auto_increment,
   firstName varchar(45) NOT NULL,
   lastName varchar(45) NOT NULL,
   email varchar(45) NOT NULL,
   uname varchar(45) NOT NULL,
   pass varchar(45) NOT NULL,
   regdate date NOT NULL,
   PRIMARY KEY  (id)
   ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

JSP文件路径:C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ webapps \ ROOT

CLASSPATH:C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ lib \ jsp-api; C:\ Program Files \ Java \ jdk1.6.0_38 \ jre \ lib; C:\ Program Files \ MySQL \ MySQL Connector J \ mysql-connector-java-5.1.29-bin

路径:C:\ Program Files \ MySQL \ MySQL Server 5.2 \ bin; C:\ Program Files \ Java \ jdk1.6.0_38 \ bin

1 个答案:

答案 0 :(得分:0)

您需要将MYSQL JDBC驱动程序添加到项目中。如果您正在使用netbeans,请单击项目上的右键 - > gt> properties-&gt; Libraries-&gt; Add Library,然后选择MYSQL JDBC Driver。问题解决了。