Reputation: 63
These are my codes inside the .entity directory.
public boolean createUser() {
boolean success = false;
DBController db = new DBController();
db.setUp("IT Innovation Project");
String sql = "INSERT INTO forumUsers(users_userName,users_password,users_NRIC)";
sql += "VALUES ('" + userName + "','" + pwd + "','" + nric + "')";
System.out.println(sql);
if (db.updateRequest(sql) == 1)
success = true;
db.terminate();
return success;
}
public boolean retrieveUser() {
boolean success = false;
ResultSet rs = null;
DBController db = new DBController();
db.setUp("IT Innovation Project");
String dbQuery = "SELECT * FROM forumUsers WHERE users_userName = '";
dbQuery += userName + "'";
rs = db.readRequest(dbQuery);
try {
if (rs.next()) {
pwd = rs.getString("password");
success = true;
}
} catch (Exception e) {
e.printStackTrace();
}
db.terminate();
return success;
}
These are my codes in the user interface class.
if (userName.equals("")||pwd.equals("")||nric.equals(""))
JOptionPane.showMessageDialog(null,"Please enter all text field");
else{
eForumUser user = new eForumUser (userName,pwd,nric);
if (user.retrieveUser())
JOptionPane.showMessageDialog(null,"User already exists! Cannot create.");
else{
if (user.createUser()==true);
JOptionPane.showMessageDialog(null,"User record created successfull!");
}
My database table got users_userName, users_password, users_NRIC, users_securityQuestion, users_answer column.
I can create a data into the database. However, when I try to check whether the username is exist or not by using the retrieveUser method, the console showed me "column not found" error message. So what am I supposed to do to fix this problem? Is it something wrong with my retrieveUser method?
I editted my retriveUser method to:
public boolean retrieveUser() {
boolean success = false;
ResultSet rs = null;
DBController db = new DBController();
db.setUp("IT Innovation Project");
String dbQuery = "SELECT * FROM forumUsers WHERE users_NRIC AND users_securityQuestion AND users_answer = '";
dbQuery += userName + securityQuestion + answer + "'";
rs = db.readRequest(dbQuery);
try {
if (rs.next()) {
pwd = rs.getString("users_password");
success = true;
}
} catch (Exception e) {
e.printStackTrace();
}
db.terminate();
return success;
}
And I try to retrieve password by entering nric, security question and answer. Here is the code for user interface.
public void submitForgotPwd(ActionEvent e){
String nric = jTextField.getText();
Object securityQuestion = jComboBox.getSelectedItem();
String answer = jTextField1.getText();
if (nric.equals("")||securityQuestion.equals("")||answer.equals("")){
JOptionPane.showMessageDialog(null, "Please enter nric,security question and answer.");
}else{
eForumUser user = new eForumUser(nric,securityQuestion,answer);
if (user.retrieveUser()==true){
jTextField2.setText(user.getPwd());
}else{
JOptionPane.showMessageDialog(null,"Nric do not exist or wrong security question and answer");
}
}
}
However, my retrieve does not works. Can somebody help me with it?
Upvotes: 0
Views: 358
Reputation: 5183
I think your problem is here
pwd = rs.getString("password");
the column name is users_password
so change it to
pwd = rs.getString("users_password");
Upvotes: 1