我在mysql中有一个返回多行的存储过程。
我执行它的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\u 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"]...]
你知道怎么修吗?
2条答案
按热度按时间prdp8dxp1#
不知道你用的是什么库,但可能和这行有关:
obj.put("ps_allergies", alist);
put方法通常将指定值与Map中的指定键相关联。由于您不断覆盖循环中的键“ps\u allergies”,因此它将只保留最后一个值。您可能需要将列表/数组与ps\ U过敏相关,然后添加
alist
对象。uidvcgyl2#
我找到了解决办法。我使用append方法而不是put。
结果现在是: