2013-06-18 74 views
0

我有我的類此方法,不兼容的類型。必需的類名。找到int。

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) { 
     } 
    } 

我知道我應該返回類型賬戶持有它是一類對象。賬戶持有類代碼如下:

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

    // get & set methods(); 
} 

我應該回到使用該方法從數據庫匹配用戶名密碼&。我怎樣才能做到這一點?

回答

0

你可以有,如果匹配的記錄,發現針對輸入的用戶名和密碼,然後返回一個具體的賬戶持有的對象,否則無效的方法。

嘗試這樣的事情,改變列名稱按您的模式:

公共靜態賬戶持有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 ; 
} 
0

您需要將ResultSet中的元素作爲答案轉換爲AccountHolder的對象,並返回該元素而不是行return 1;

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 
} 
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. 
相關問題