【发布时间】:2020-09-11 22:10:20
【问题描述】:
我最近正在制作一个 Java Web 应用程序,它必须专注于 CRUD(你知道)。但我坚持阅读、更新和删除操作(三个操作)。只有创建操作才能正常工作。详细地说,我正在开发的 Java Web 应用程序到目前为止还没有完成。在我的代码中,名为“findUsers”的函数用于实现读取操作。顺便说一句,我已经检查了很长时间的代码。我猜问题可能出在 findUsers 函数中(不确定,只是一个假设)。每次,我尝试键入 R 来调用该函数,Netbeans 返回“用户不存在”。我不知道为什么。而且,数据库连接成功。 enter image description here这张照片是我的数据库结构。
DBManager.java
//read operation
public User findUsers(String email, String password) throws SQLException {
String sqll = "SELECT * FROM XWB.USERS WHERE EMAIL = ' " + email + " ' AND PASSWORD = ' " + password + " ' ";
// " SELECT * FROM XWB.USERS WHERE EMAIL = ' " + email + " ' AND PASSWORD = ' " + password + " ' ";
//select * from XWB.Users where EMAIL = 'TargetEmail' and PASSWORD = 'TargetPassword';
ResultSet rs = st.executeQuery(sqll);
while (rs.next()) {
String UserEmail = rs.getString("EMAIL");
String UserPassword = rs.getString("PASSWORD");
if (UserEmail.equals(email) && UserPassword.equals(password)) {
String UserName = rs.getString("NAME");
String UserGender = rs.getString("GENDER");
String UserColor = rs.getString("FAVOURITECOLOR");
return new User(UserEmail, UserName, UserPassword, UserGender, UserColor);
}
}
return null;
}
TestDB.java(我用这个类来测试DBManager)
// findUsers()
private void testRead() throws SQLException {
System.out.print("User email: ");
String email = in.nextLine();
System.out.print("User password: ");
String password = in.nextLine();
User user = db.findUsers(email, password); // returns nothing
//System.out.println(user);
if( user != null) {
System.out.println("User " + user.getName() + " exists in the database.");
}else { //user == null
System.out.println("User does not exit.");
}
}
这是我从 Netbeans 得到的结果。它总是告诉我“用户不存在。” enter image description here
【问题讨论】:
标签: java sql jsp model-view-controller crud