0

这些是我在 .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");
        }
    }
}

但是,我的检索不起作用。有人可以帮我吗?

4

1 回答 1

1

我认为你的问题在这里

pwd = rs.getString("password");

列名是 users_password

所以把它改成

 pwd = rs.getString("users_password");
于 2012-12-05T06:46:21.123 回答