我正在尝试将servlet连接到netbeans 8和jdk 8以及glassfish服务器中的mysql数据库。我还在库中包含了mysqlconnector jar文件但是我得到了
Class.forName中未找到类异常(“com.mysql.jdbc.Driver”)
我的代码
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class sqltest1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String user = request.getParameter("user");
String pass = request.getParameter("pass");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "root");
PreparedStatement pst = conn.prepareStatement("Select user,pass from login where user=? and pass=?");
pst.setString(1, user);
pst.setString(2, pass);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
out.println("Correct login credentials");
}
else {
out.println("Incorrect login credentials");
}
}
}
答案 0 :(得分:1)
尝试将jar文件重新添加到java 构建路径并清理而不是重新编译。
您的代码看起来很好。
答案 1 :(得分:0)
检查类路径上的mysql驱动程序jar,并验证正确的mysql驱动程序jar。