2013-04-19 104 views
0

我想通過查詢數據庫來創建UserInfo對象構造函數,並且我一直收到UserInfo thisUserInfo = new UserInfo()部分的cannot find symbol錯誤。我試圖採取ResultSet rs並填寫構造函數爲登錄的用戶創建「會話」。如何通過查詢Java中的數據庫填充對象構造函數

我在這裏做了什麼錯?這裏是我的代碼:

private UserInfo getUserInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException { 
    String userName = request.getParameter("userName"); 
    String nullString = null; 
    char nullChar = ' '; 

    ResultSet rs = null; 

    stmt = conn.createStatement(); 
    String getInfoSQL = "SELECT * FROM " + studentsTable + " WHERE USERNAME = '" + userName + "'"; 
    rs = stmt.executeQuery(getInfoSQL); 
    if(rs.next()) { 
     UserInfo thisUserInfo = new UserInfo(rs.getString(userName), rs.getString(passWord), rs.getString(lastName), rs.getString(firstName), rs.getString(age), rs.getString(sex), rs.getString(email)); 
     rs.close(); 
     stmt.close(); 
    } else { 
     UserInfo thisUserInfo = new UserInfo(nullString, nullString, nullString, nullString, nullString, nullChar, nullString); 
     rs.close(); 
     stmt.close(); 
    } 
    return thisUserInfo; 
} 

這裏是我的UserInfo類:

public class UserInfo { 
private final String userName; 
private final String passWord; 
private final String lastName; 
private final String firstName; 
private final String age; 
private final char sex; 
private final String email; 

public UserInfo(String userName, String passWord, String lastName, String firstName, String age, char sex, String email) { 
    this.userName = userName; 
    this.passWord = passWord; 
    this.lastName = lastName; 
    this.firstName = firstName; 
    this.age = age; 
    this.sex = sex; 
    this.email = email; 
} 

public String getUserName() { 
    return this.userName; 
} 
public String getPassWord() { 
    return this.passWord;     
} 
public String getLastName() { 
    return this.lastName; 
} 
public String getFirstName() { 
    return this.firstName; 
} 
public String getAge() { 
    return this.age;  
} 
public char getSex() { 
    return this.sex; 
} 
public String getEmail() { 
    return this.email; 
} 

}

回答

1

我不能左右一切100%,但是這看起來我錯了......

if(rs.next()) { 
    // UserInfo is declared within a local context 
    UserInfo thisUserInfo = new UserInfo(rs.getString(userName), rs.getString(passWord), rs.getString(lastName), rs.getString(firstName), rs.getString(age), rs.getString(sex), rs.getString(email)); 
    rs.close(); 
    stmt.close(); 
} else { 
    // UserInfo is declared within a local context 
    UserInfo thisUserInfo = new UserInfo(nullString, nullString, nullString, nullString, nullString, nullChar, nullString); 
    rs.close(); 
    stmt.close(); 
} 
// thisUserInfo does not exist 
return thisUserInfo; 

它應該更像......

UserInfo thisUserInfo = null; 
if(rs.next()) { 
    thisUserInfo = new UserInfo(rs.getString(userName), rs.getString(passWord), rs.getString(lastName), rs.getString(firstName), rs.getString(age), rs.getString(sex), rs.getString(email)); 
    rs.close(); 
    stmt.close(); 
} else { 
    thisUserInfo = new UserInfo(nullString, nullString, nullString, nullString, nullString, nullChar, nullString); 
    rs.close(); 
    stmt.close(); 
} 
return thisUserInfo; 

你要知道,我會親自做到這一點更像....

try { 
    stmt = conn.createStatement(); 
    String getInfoSQL = "SELECT * FROM " + studentsTable + " WHERE USERNAME = '" + userName + "'"; 
    rs = stmt.executeQuery(getInfoSQL); 
    UserInfo thisUserInfo = null; 
    if(rs.next()) { 
     thisUserInfo = new UserInfo(rs.getString(userName), rs.getString(passWord), rs.getString(lastName), rs.getString(firstName), rs.getString(age), rs.getString(sex), rs.getString(email)); 
    } else { 
     thisUserInfo = new UserInfo(nullString, nullString, nullString, nullString, nullString, nullChar, nullString); 
    } 
} finally { 
    try { 
     rs.close(); 
    } catch (Exception exp) { 
    } 
    try { 
     rs.close(); 
    } catch (Exception exp) { 
     stmt.close(); 
    } 
} 
return thisUserInfo; 

正如(現在)已經指出... UserInfo需要一個charsex參數,但你傳遞一個String

你應該嘗試提取使用類似String sexValue = rs.getString(sex)數據庫中的值,如果它不爲空,則使用sexValue.getCharAt(0)

我也有不知道在哪裏userNamepassWordlastNamefirstNameagesexemail定義。這讓我有點擔心......

+0

謝謝,MadProgrammer。那麼我應該在哪裏定義'userName','passWord'等等?在'getUserInfo()'方法中? – dinky

+1

對不起,我的不好,不斷被拖離電腦。你用來從結果集中提取結果的變量,即'rs.getString(userName)'沒有在任何地方定義(我可以看到),這只是讓我擔心一點;) – MadProgrammer

+0

我看到了...所以我剛剛改變了構造函數參數,這樣我只需要這樣的第n個數據:'UserInfo thisUserInfo = new UserInfo(rs.getString(0),rs.getString(1),rs.getString(2),rs.getString(3 ),rs.getString(4),rs.getString(5).atChar(0),rs.getString(6))''。這會工作得很好嗎? – dinky

2

您需要發佈的代碼爲您UserInfo類起動器。我敢打賭,你沒有定義一個構造函數來接受這些參數。

編輯你試圖暗中從String轉換爲char我懷疑你不能這樣做。而不是rs.getString(sex)嘗試rs.getChar(sex)。或者,你可以添加一個額外的構造函數,接受性別參數String

編輯哦,我現在看到了這個問題。你從來沒有定義任何其他變量,如passWord等。這些需要是字符串列索引的字符串或整數。

此外,這是不相關的,但從來沒有通過類似的查詢通過unsanitized輸入。你打開自己的SQL Injection。您應該使用prepared statements instead 或確保至少避開輸入。

+0

剛剛編輯了這個問題,克里斯。請看一下。而且我知道我應該使用'PreparedStatements',但以後我會擔心。謝謝。 – dinky

+1

@Dinky查看我的編輯 –

+0

感謝您的編輯。因爲我有'String userName = request.getParameter('userName')'頂部,因爲這是我需要從我的jsp頁面的唯一參數,錯誤發生在所有其他'rs。getString()'方法說'找不到符號'。有什麼問題? – dinky

相關問題