java/mysql:从存储过程中获取所有结果行,而不仅仅是最后一行

y0u0uwnf  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(308)

我在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"]...]

你知道怎么修吗?

prdp8dxp

prdp8dxp1#

不知道你用的是什么库,但可能和这行有关: obj.put("ps_allergies", alist); put方法通常将指定值与Map中的指定键相关联。由于您不断覆盖循环中的键“ps\u allergies”,因此它将只保留最后一个值。
您可能需要将列表/数组与ps\ U过敏相关,然后添加 alist 对象。

uidvcgyl

uidvcgyl2#

我找到了解决办法。我使用append方法而不是put。

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"]]

相关问题