0

我的课上有这个方法,

 public static AccountHolder getAccountHolder(String username, String password) {

        try {
            String verifyQuery = "SELECT COUNT(username) FROM accountholder WHERE username=? AND password=?)";
            connection = getConnection();
            ptmt = connection.prepareStatement(verifyQuery);

            ptmt.setString(4, username);
            ptmt.setString(5, password);

            ResultSet verified = ptmt.executeQuery();

            if (verified.next()) {
                // Only one Result
                if (verified.getInt(1) == 1) {
                    connection.close();
                    return 1;
                } else {
                    connection.close();
                    return null;
                }

            }
        } catch (Exception ex) {
        }
    }

我知道我应该返回 AccountHolder 类型的对象,它是一个类。AccountHolder 类代码如下:

public class AccountHolder {
   private String firstname;
   private String lastname;
   private String username;
   private String password;
   private String address;
   private String phonenumber;

  // get & set methods();
}

我应该使用该方法从数据库返回匹配的用户名和密码。我怎样才能做到这一点?

4

4 回答 4

0

您需要将ResultSet作为答案获得的元素转换为的对象AccountHolder并返回它而不是 line return 1;

于 2013-06-18T06:24:40.173 回答
0

尝试更改您的代码

PreparedStatement stmt = conn.prepareStatement(
        "SELECT USERNAME FROM accountholder 
                             WHERE USERNAME = ? AND PASSWORD = ?");
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
    // valid credentials
} else {
    // invalid credentials
}
于 2013-06-18T06:26:55.400 回答
0

您可以采用一种方法,如果根据输入的用户名和密码找到匹配的记录,则返回一个具体的 AccountHolder 对象,否则返回 null。

尝试这样的事情,根据您的架构更改列名:

公共静态AccountHolder getAccountHolder(字符串用户名,字符串密码){

    AccountHolder accountHolder = null;
    try {
        String verifyQuery = "SELECT * FROM accountholder WHERE username=? AND password=?)";
        connection = getConnection();
        ptmt = connection.prepareStatement(verifyQuery);

        ptmt.setString(1, username);
        ptmt.setString(2, password);

        ResultSet verified = ptmt.executeQuery();

        if (verified.next()) {
           accountHolder = new AccountHolder();
           accountHolder.setFirstName(rs.getString("FIRSTNAME"));
           accountHolder.setLastName(rs.getString("LASTNAME"));
           accountHolder.setUsername(rs.getString("USERNAME"));
           accountHolder.setPassword(rs.getString("PASSWORD"));
           accountHolder.setAddress(rs.getString("ADDRESS"));
           accountHolder.setPhoneNumber(rs.getString("PHONENUMBER"));

        }
    } catch (Exception ex) {
    } finally {
                connection.close();
    }
    return accountHolder ;
}
于 2013-06-18T06:29:53.430 回答
0
Change that prepared statement query like this

pm.setString(1,username);
pm.setString(2,password);

and

assign the setters like this
accountholder.setUserName(verified.getString(1));
accountholder.setpassword(verified.getString(2));


get those values in array or any collection for retrieval.
于 2013-06-18T06:41:18.143 回答