我在mysql中有一個存儲過程,它返回多行。Java/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_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"]...]
東西你知道如何解決這一問題?
問題就在這裏:'obj.put( 「ps_allergies」,ALIST);'obj是地圖,你把你所有的線路中的地圖同樣的鑰匙。每個結果都會覆蓋前一個結果。這就是爲什麼你只看到最後一行。 – StephaneM
@StephaneM是的,我明白。在json的新列表中可以有單獨的行嗎? – zinon