使用存储过程返回结果集

因业务复杂,SQL较为庞大,后改造为存储过程方式,传参,获取结果集,此处只显示存储过程的结构部分,也是最重要部分

CREATE OR REPLACE PACKAGE PKG_QUALITYCONTROL IS
  TYPE retcursor IS REF CURSOR;
  PROCEDURE GET_QUALITYCONTROL(v_encounterid in nvarchar2,
                                   v_patientid   in nvarchar2,
                                   outcurse      IN OUT retcursor);
END;
CREATE OR REPLACE PACKAGE BODY PKG_QUALITYCONTROL IS
  PROCEDURE GET_QUALITYCONTROL(v_encounterid in nvarchar2,
                                   v_patientid   in nvarchar2,
                                   outcurse      IN OUT retcursor) IS
  begin
    OPEN outcurse FOR
      select fm.formid,
             fm.formtype,
             fm.formname,
             fm.formurl,
             case
               when exists (select 1
                       from form f
                      where f.states = 'N'
                        and f.formtype = fm.formtype
                        and f.encid = v_encounterid
                        and f.patientid = v_patientid) then
                '暂存'
               when exists (select 1
                       from form f
                      where f.states = 'Y'
                        and f.formtype = fm.formtype
                        and f.encid = v_encounterid
                        and f.patientid = v_patientid) then
                '是'
               else
                '否'
             end formtemp
        from form_menu fm
       where fm.formtype in ('Form1',
                             'Form2',
                             'Form3',
                             'Form4',
                             'Form5',
                             'Form6',
                             'Form7');
    return;
  end;
END;

上面两个SQL需要分开执行,一个是定义包,一个是定义包内容

java代码部分

public List<String[]> getFormMenuList(String encid, String patientid) throws ControllerException {
		String me = getClass().getName() + ".getFormMenuList()";
		List<String[]> ls = new ArrayList<String[]>();
		Connection conn = null;
		CallableStatement cstmt = null;
		ResultSet rs = null;
		String sql = "{call PKG_MANAGEJOBS.GET_QUALITYCONTROL(?, ?, ?)}";
		try {
			conn = getNisJdbcConnection();
			cstmt = conn.prepareCall(sql);
		    cstmt.setString(1, encid);
		    cstmt.setString(2, patientid);
		    cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR); //outcurse
		    cstmt.execute();
		    rs = (ResultSet) cstmt.getObject(3);
			String[] array = null;
			while (rs.next()) {
				array = new String[7];
				array[0] = rs.getString("formid");
				array[1] = rs.getString("formtype");
				array[2] = rs.getString("formname");
				array[3] = rs.getString("formurl");
				array[4] = rs.getString("formtemp");
				ls.add(array);
			}
		} catch (Throwable e) {
			throw new ControllerException("{0}:获取出院病人记录列表", new String[] { me }, e);
		} finally {
			closeConnection(conn, cstmt, rs);
		}
		return ls;
	}

 

    原文作者:贝铃-Turing
    原文地址: https://blog.csdn.net/u011410116/article/details/84935938
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞