2017-09-04 80 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

相关问题