2016-08-12 26 views
-1

我有一個調用SQL查詢的方法。它返回一個ArrayList,所以我將它設置爲一個變量temp。我正在更改使用ResultSet不使用它的舊代碼。 我把這個:Java Arraylist <String>調用SQL查詢

if (rs.next()) { 
     Password = rs.getString("EncryptedPassword"); 

到:

for (String x : temp) { 
    Password = //something 

我要檢索的字符串ArrayList中,並檢查是否等於「EncryptedPassword」 你能解釋如何RS。 getString的工作原理和我如何操作新代碼? 我可以使用if語句而不是foreach語句。

回答

0

這是什麼的Java的API說,對ResultSet和它的方法:

next() 
      Moves the cursor froward one row from its current position. returns boolean value 

    getString(String columnLabel) 
     Retrieves the value of the designated column in the current row of this 
     ResultSet object as a String in the Java programming language. 

因此,基於我們可以閱讀只是在那裏「舊代碼」通過調用列名檢索某些單元格的值。當光標向前移動時,方法next()可能會移動,它正在遍歷每個覆蓋範圍。 所以你的新代碼會是這樣的,我爲你寫了僞代碼:

array_of_passwords = getData into arraylist; 
for(String password: array_of_passwords){ 
    Password = // process data here; 
}