java.lang.ClassNotFoundException:Servlet中的com.mysql.jdbc.Driver

时间:2017-12-04 10:41:46

标签: mysql servlets jdbc classnotfoundexception

我正在编写servlet以使用mysql java连接mysqlconnector数据库但是它给出了错误

  

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

我尝试使用与mysql连接的相同驱动程序与核心java相同,并且查询也在运行但是使用servlet它不起作用。

我的代码是servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;





public class RegisterServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String n = request.getParameter("userName");
        String p = request.getParameter("password");
        String e = request.getParameter("email");
        String c = request.getParameter("course");

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = null;
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/phpmyadmin/saiiff","root", "");
            PreparedStatement ps =  conn
                    .prepareStatement("insert into eemeze values(?,?,?,?)");

            ps.setString(1, n);
            ps.setString(2, p);
            ps.setString(3, e);
            ps.setString(4, c);

            int i = ps.executeUpdate();
            if (i > 0)
                out.print("You are successfully registered...");

        } catch (Exception e2) {
            System.out.println(e2);
        }

        out.close();    
    }
}

1 个答案:

答案 0 :(得分:-1)