|已将MySQL连接器添加到库中,但仍出现错误'java.lang.ClassNotFoundException:com.mysql.jdbc.driver'

时间:2018-07-01 09:05:21

标签: java mysql jsp jdbc

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginChek
 */
@WebServlet("/LoginChek")
public class LoginChek extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginChek() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String Uname=request.getParameter("Uname");
        String password=request.getParameter("password");
        try { 
            Class.forName("com.mysql.jdbc.driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost/3306/mydb","root","root");
            Statement stmt =con.createStatement();
            ResultSet rs=stmt.executeQuery("SELECT uname,upass FROM user_reg WHERE uname='"+Uname+"' and upass='"+password+"' ");

            if(rs.next())
            {
                HttpSession session=request.getSession();
                session.setAttribute("Uname", Uname);
                response.sendRedirect("member.jsp");
            }
            else
            {
                response.sendRedirect("error.jsp");

            }
            } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        doGet(request, response);
    }}

**我正在使用Eclicpse Photon June 2018版本以及MySQL的最新版本。我已使用属性> Java Build Path>库>添加外部Jar将'mysql-connector-java-8.0.11'添加到动态Web项目的库中。但是在运行程序时出现“ java.lang.ClassNotFoundException:com.mysql.jdbc.driver”错误。我是java的新手,我该如何解决**

1 个答案:

答案 0 :(得分:0)

将'mysql-connector-java-8.0.11'放入lib文件夹yourProject\WebContent\WEB-INF\lib