我正在尝试将servlet连接到netbeans8、jdk8和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");
}
}
}
2条答案
按热度按时间im9ewurl1#
尝试将jar文件读入java构建路径并清除,而不是重新编译。
你的代码看起来不错。
qaxu7uf22#
检查类路径上的mysql驱动程序jar,并验证mysql驱动程序jar是否正确。