Search code examples
javanetbeansresultsetuser-accounts

Check ResultSet values one by one


public void changeAccount(String X, String Y, String Z) {
    try {
        JDBC.DB.putData("UPDATE login SET pw='" + Y + "',type='" + Z + "' WHERE un='" + X + "' ");
        System.out.println("done");
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}

This my code to update user password according to the username given. These X, Y, Z values pw, usertype (admin or limit), username. Let's think there is user 'abc' & pw '123' in db. I want to check If 'abc' user exist in db. If not exist show message "no such user".. How to user resultset here to check..


Solution

  • First, execute a SELECT statement, something like:

    select un from login where un=<given username> and pw = <given password>
    

    If this returns >= 1 rows, this means such a user
    exists so proceed with your UPDATE statement.

    If the SELECT statement returns 0 rows, no such user exists.

    See some simple tutorial with samples on the web.

    For example, this one looks OK.

    http://www.tutorialspoint.com/jdbc/jdbc-select-records.htm

    See STEP 5 there.