问题
I am recently working on making a Java web application, which must focus on CRUD(You know). But I am stuck with the read & update & delete operations(Three operations). Only Create Operation works fine. In detailed, the Java web application I am working on is not completed so far. Inside my code, the function called "findUsers" is used to implement read operation. By the way, I already checked code for a long period of time. I guess the problem may be in findUsers function(not sure, just an assumption). Every time, I try to type R to invoke that function, Netbeans returns "User does not exist". I don't know why. Moreover, the database is connected successfully. enter image description here This photo is my database structure.
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(I use this class to test the 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.");
}
}
This is the result I got from Netbeans. It always tells me "User does not exist. " enter image description here
回答1:
I think you should remove space after and before ['] character if your data value are correct with email and password. Your code:
String sqll = "SELECT * FROM XWB.USERS WHERE EMAIL = ' " + email + " ' AND PASSWORD = ' " + password + " ' ";
Replace:
String sqll = "SELECT * FROM XWB.USERS WHERE EMAIL = '" + email + "' AND PASSWORD = '" + password + "' ";
回答2:
In your code you have return null
so when this get check at your calling class if( user != null)
will return false and the else
part of your if-statement
will get executed.So to overcome do below changes :
Your method will look like below:
User user; //create class object
String sqll = "SELECT * FROM XWB.USERS WHERE EMAIL = ? AND PASSWORD = ? ";
PreparedStatement ps = con.prepareStatement(
sqll);
//setting value for "?"
ps.setString(1, email);
ps.setString(2, password);
//execute query
ResultSet rs = ps.executeQuery();
//if found
if (rs.next()) {
//fetch
String UserEmail = rs.getString("EMAIL");
String UserPassword = rs.getString("PASSWORD");
String UserName = rs.getString("NAME");
String UserGender = rs.getString("GENDER");
String UserColor = rs.getString("FAVOURITECOLOR");
//pass in constructor
user = new User(UserEmail, UserName, UserPassword, UserGender, UserColor);
} else {
//set the object to null (no match)
user = null;
}
//send object back
return user;
来源:https://stackoverflow.com/questions/61999571/questions-about-jsp-sql-java-web-application