java
2017-01-16 56 views 0 likes 
0

我想要給hashmap添加值從DB.resulset中獲取有2行數據但它的覆蓋首先添加了行值。把數據寫入散列表而不覆蓋

這是我的代碼。

 Statement s1 = con.createStatement(); 
     ResultSet rs1 = s1.executeQuery("SELECT * FROM garantee WHERE loanapplicationid ='" + applicationid + "'"); 
     ResultSetMetaData garanteeMetaData = rs1.getMetaData(); 
     int garanteeColumns = garanteeMetaData.getColumnCount(); 
     HashMap<String, String> garanteeFull = new HashMap<String, String>(); 
     while (rs1.next()) { 
      int j = 1; 
      for (int i = 1; i < garanteeColumns; i++) { 
       garanteeFull.put(garanteeMetaData.getColumnLabel(i) + j, rs1.getString(i)); 
       System.out.println(rs1.getString(i)); 
      } 
      j++; 
     } 

我使用詮釋J改變關鍵value.but其無法正常工作corectly.please幫助me.how做

**對不起壞英語

+1

調試吧..最有可能的「garanteeMetaData.getColumnLabel(我)+ J」將有兩個運行的相同值.. –

+5

' j'總是1.最後的增量是多餘的。你的意思是把'int j = 1;'放在while循環之外嗎? –

+1

(Andy Turner的bug)會被調試器注意到,或者只是在循環中記錄日誌消息。記得先調試你的程序 – AxelH

回答

1

我認爲你應該使用跟隨外部while循環,因爲每當它進入循環時,j的值就變成1。

int j = 1;

所以,請與下面的代碼嘗試更換你的..

Statement s1 = con.createStatement(); 
    ResultSet rs1 = s1.executeQuery("SELECT * FROM garantee WHERE loanapplicationid ='" + applicationid + "'"); 
    ResultSetMetaData garanteeMetaData = rs1.getMetaData(); 
    int garanteeColumns = garanteeMetaData.getColumnCount(); 
    HashMap<String, String> garanteeFull = new HashMap<String, String>(); 
    int j = 1; 
    while (rs1.next()) { 

     for (int i = 1; i < garanteeColumns; i++) { 
      garanteeFull.put(garanteeMetaData.getColumnLabel(i) + j, rs1.getString(i)); 
      System.out.println(rs1.getString(i)); 
     } 
     j++; 
    } 
相關問題