spring 调用存储过程 包括返回值,输出参数 多个结果集

//	ALTER proc [dbo].[mytestproc]
//	(
//	 @name varchar(60) ,
//	 @id int output
//	)
//	as
//	begin 
//	select top 6 * from clients
//	 select top 6 * from products where name like '%'+@name+'%'
//
//	 set @id=5 
//	 return 6
//	 end
	@GetMapping("/querybyproc3")
	@ResponseBody
	public String querybyproc3() {
		JSONArray ja = new JSONArray();
		JSONObject jo1 = new JSONObject();
		jo1.put("xuhao", 1);
		jo1.put("type", java.sql.Types.INTEGER);
		jo1.put("direction", 1);
		jo1.put("value", 0);
		ja.add(jo1);
		JSONObject jo2 = new JSONObject();
		jo2.put("xuhao", 2);
		jo2.put("type", java.sql.Types.VARCHAR);
		jo2.put("direction", 0);
		jo2.put("value", 0);
		ja.add(jo2);

		JSONObject jo3 = new JSONObject();
		jo3.put("xuhao", 3);
		jo3.put("type", java.sql.Types.INTEGER);
		jo3.put("direction", 1);
		jo3.put("value", 0);
		ja.add(jo3);

		String s = execproc("?=call mytestproc(?,?)", ja);
		return s;
	}
	public String execproc(String proc, JSONArray jain) {
		JSONObject joresult = new JSONObject();
		JSONArray jaresult = new JSONArray();
		jdbcTemplate.execute("{" + proc + "}", new CallableStatementCallback() {
			public String doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
				ArrayList outp = new ArrayList();//输出参数列表
				for (int i = 0; i < jain.size(); i++) {
					int parameterno = i + 1;
					JSONObject joitem = JSONObject.parseObject(jain.get(i).toString());
					if (joitem.getInteger("direction") == 1) {
						cs.registerOutParameter(parameterno, joitem.getIntValue("type"));
						outp.add(joitem);
					}

					if (joitem.getIntValue("type") == java.sql.Types.INTEGER) {
						cs.setInt(parameterno, joitem.getIntValue("value"));
					}

					if (joitem.getIntValue("type") == java.sql.Types.VARCHAR) {
						cs.setString(parameterno, joitem.getString("value"));
					}
					if (joitem.getIntValue("type") == java.sql.Types.NUMERIC) {
						cs.setDouble(parameterno, joitem.getDoubleValue("value"));
					}

				}

				boolean bool = cs.execute(); // true if the first result is a ResultSet object;

				while (bool) {

					ResultSet rs = cs.getResultSet();
					logger.info("cs---------------" + rs.toString());
					logger.info("cs---------------" + rs.toString());
					// logger.info("cs---------------"+resultSetToJsonArry(rs).toString());
					jaresult.add(RsToJson.resultSetToJsonArry(rs).toString());
					bool = cs.getMoreResults();
				}
				// 必须到最后一个记录集了才能取参数

				// logger.info("cs.getInt(1)---------" + cs.getInt(1));
				// logger.info("cs.getInt(3)---------" + cs.getInt(3));

				// cs.getResultSet();

				JSONArray returnvalues = new JSONArray();

				for (int n = 0; n < outp.size(); n++) {
					int xuhao = outp.get(n).getIntValue("xuhao");
					int type = outp.get(n).getIntValue("type");
					if (type == java.sql.Types.INTEGER) {

						returnvalues.add(cs.getInt(xuhao));
					}

					if (type == java.sql.Types.VARCHAR) {

						returnvalues.add(cs.getString(xuhao));
					}
					if (type == java.sql.Types.NUMERIC) {
						;
						returnvalues.add(cs.getDouble(xuhao));
					}

				}

				joresult.put("returnvalues", returnvalues);
				// jaresult.add(joreturnvalues);
				joresult.put("resultset", jaresult);

				return cs.getString(1);
			}
		});

		return joresult.toJSONString();
	}

 

你可能感兴趣的:(spring 调用存储过程 包括返回值,输出参数 多个结果集)