2017-09-04 83 views
0

我在mysql中有一個存儲過程,它返回多行。Java/Mysql:獲取存儲過程中的所有結果行,而不僅僅是最後一個

enter image description here

我的Java代碼得到執行,那就是:

preparedStmt = conn.prepareCall(queryString); 
      preparedStmt.setString(1, String.valueOf(patient_id)); 
      //System.out.print("select patient data java file 1 "); 

      boolean results = preparedStmt.execute(); 

      int rowsAffected = 0; 
      // Protects against lack of SET NOCOUNT in stored procedure 
      while (results || rowsAffected != -1) { 
       if (results) { 
        rs = preparedStmt.getResultSet(); 
        break; 
       } else { 
        rowsAffected = preparedStmt.getUpdateCount(); 
       } 
       results = preparedStmt.getMoreResults(); 
      } 
      int i = 0; 
      obj = new JSONObject(); 
      while (rs.next()) { 
       JSONArray alist = new JSONArray(); 
       alist.put(rs.getString("patient_id")); 
       alist.put(rs.getString("allergy")); 
       alist.put(rs.getString("allergy_description")); 
       alist.put(rs.getString("allergy_onset_date")); 
       alist.put(rs.getString("agent_description")); 
       alist.put(rs.getString("agent")); 
       alist.put(rs.getString("severity")); 
       obj.put("ps_allergies", alist); 
       i++; 
      } 
      conn.close(); 

最後,ps_allergies JSON對象僅包含查詢的最後一行。這是打印輸出:

["1","week",null,"2017-07-07","vacation home","test2","mobile contact"] 

我想ps_allergies包含類似於

[["1","hydrogen peroxide","Nuts","2017-07-04","Nursing profressionals","43","Paramedical practinioners"],["1","week",null,"2017-07-07","vacation home","test2","mobile contact"]...] 

東西你知道如何解決這一問題?

+0

問題就在這裏:'obj.put( 「ps_allergies」,ALIST);'obj是地圖,你把你所有的線路中的地圖同樣的鑰匙。每個結果都會覆蓋前一個結果。這就是爲什麼你只看到最後一行。 – StephaneM

+0

@StephaneM是的,我明白。在json的新列表中可以有單獨的行嗎? – zinon

回答

0

我找到了解決方案。而不是我用追加方法。

obj.append("ps_allergies", alist); 

所得輸出是現在:

[["1","hydrogen peroxide","Nuts","2017-07-04","Nursing professionals","43","Paramedical practitioners"],["1","chlorhexidine","test123","2017-07-15","mobile contact","test232","pager"],["1","Resistance to unspecified antibiotic","Feb3","2017-03-02","mobile contact","test232","pager"],["1","week",null,"2017-07-07","vacation home","test2","mobile contact"]] 
0

不完全知道你用什麼庫,但它可能有一些做這一行: obj.put("ps_allergies", alist);

看跌方法一般同夥與地圖中的指定鍵指定的值。由於您不斷覆蓋您在循環中鍵入的「ps_allergies」,它只會保留最後一個值。

您可能希望將列表/數組關聯到ps_allergies,然後在此列表/數組中添加每個alist對象。

+0

我想'ps_allergies'到包含類似於'[[ 「1」, 「過氧化氫」, 「堅果」, 「2017年7月4日」, 「護理profressionals」, 「43」, 「輔助醫療practinioners」]的東西, [「1」,「星期」,null,「2017-07-07」,「度假屋」,「test2」,「移動聯繫人」] ...] – zinon

相關問題