这些是.entity目录中的代码。
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;
}
这些是我在用户界面类中的代码。
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!");
}
我的数据库表有users_userName,users_password,users_NRIC,users_securityQuestion,users_answer列。
我可以在数据库中创建数据。但是,当我尝试使用retrieveUser方法检查用户名是否存在时,控制台显示“找不到列”错误消息。那我该怎么做才能解决这个问题呢?我的retrieveUser方法有问题吗?
我将retriveUser方法编辑为:
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;
}
我尝试通过输入nric,安全问题和答案来检索密码。这是用户界面的代码。
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");
}
}
}
但是,我的检索不起作用。有人可以帮我吗?
答案 0 :(得分:1)
我认为你的问题在这里
pwd = rs.getString("password");
列名是users_password
所以将其改为
pwd = rs.getString("users_password");