gpt4 book ai didi

java.sql.SQLException : No suitable driver found for jdbc:mysql://localhost:3306/db 异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:02:51 25 4
gpt4 key购买 nike

<分区>

页面总是转发到“custLogin”,但数据是正确的。

下面的代码:

类(class)登录:

private CustomerDB db;

public void init() {
String dbUrl = "jdbc:mysql://localhost:3306/fyp";
String dbUser = "root";
String dbPassword = "";
db = new CustomerDB(dbUrl, dbUser, dbPassword);
}

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

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");

if (!isAuthenticated(request) && !("authenticate".equals(action))) {
doLogin(request, response);
return;
}
if ("authenticate".equals(action)) {
doAuthenticate(request, response);
} else if ("logout".equals(action)) {
doLogout(request, response);
} else {
response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
}
}

private void doAuthenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = request.getParameter("custID");
String password = request.getParameter("custPW");
String targetURL;
init();
boolean isValid = db.isValidUser("chan123", "123456");

if (isValid) {
targetURL = "/index.jsp";
} else {
targetURL = "/custLogin.jsp";
}
RequestDispatcher rd;
rd = getServletContext().getRequestDispatcher("/" + targetURL);
rd.forward(request, response);
}

public boolean isAuthenticated(HttpServletRequest request) {
boolean result = false;
HttpSession session = request.getSession();
if (session.getAttribute("userInfo") != null) {
result = true;
}
return result;
}

private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String targetURL = "login.jsp";
RequestDispatcher rd;
rd = getServletContext().getRequestDispatcher("/" + targetURL);
rd.forward(request, response);
}

private void doLogout(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
HttpSession session = request.getSession(false);
if (session != null) {
session.removeAttribute("userInfo");
session.invalidate();
}
doLogin(request, response);
}

chan123 是 custID。123456 是客户密码。

类 CustomerDB:

private String dbUrl;
private String dbUser;
private String dbPassword;

public CustomerDB() {
}

public CustomerDB(String dburl, String dbUser, String dbPassword) {
this.dbUrl = dbUrl;
this.dbUser = dbUser;
this.dbPassword = dbPassword;
}

public Connection getConnection() throws SQLException, IOException {
System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/fyp", "root", "");
return conn;
}

public boolean isValidUser(String custID, String custPW) {
boolean isValid = false;
Connection cnnct = null;
PreparedStatement pStmnt = null;

try {
cnnct = getConnection();
String preQueryStatement = "SELECT * FROM customer WHERE custID=? and custPW=?";
pStmnt = cnnct.prepareStatement(preQueryStatement);
pStmnt.setString(1, custID);
pStmnt.setString(2, custPW);
ResultSet rs = null;
rs = pStmnt.executeQuery();
if (rs.next()) {
isValid = true;
}
pStmnt.close();
cnnct.close();
} catch (SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return isValid;
}

我创建了测试 java。它可以加载正确的结果。

public static void main(String[] arg) {
String dbUrl = "jdbc:mysql://localhost:3306/fyp";
String dbUser = "root";
String dbPassword = "";
CustomerDB db = new CustomerDB(dbUrl, dbUser,dbPassword);
boolean n = db.isValidUser("chan123", "123456");
if (n) {
System.out.println("TTTTTT");
}else
System.out.println("f");
}

它显示“TTTTTT”。

但页面转到“custLogin”。输出:

SEVERE:   java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/fyp
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at fyp.db.CustomerDB.getConnection(CustomerDB.java:29)
at fyp.db.CustomerDB.isValidUser(CustomerDB.java:39)
at fyp.servlet.Login.doAuthenticate(Login.java:52)
at fyp.servlet.Login.doPost(Login.java:39)
...

请帮我解决这个问题。谢谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com