调用带有Out返回值的Oracle存储过程

在EJB 3.0中,传统的调用Oracle带Out返回值的存储过程是不被允许的。在项目中计算业务遇到了需要调用以上形式的存储过程,经过苦恼-纠结-痛苦直至变通。通过EntityManager获取到Hibernate的Session,再通过Session获取到Connection然后进行余下操作。

@Override
	public Map executeToCollectPersonnel(Long formId, Long periodId,
			String areaCode) {
		Map<String, String> result = new HashMap<String, String>();
		Session session = (Session) this.em.getDelegate();
		Connection conn = session.connection();
		try {
			CallableStatement call = conn
					.prepareCall("{CALL COLLECT_PERSONNEL(?,?,?,?)}");
			call.setLong(1, formId);
			call.setLong(2, periodId);
			call.setString(3, areaCode);
			call.registerOutParameter(4, oracle.jdbc.OracleTypes.VARCHAR);
			call.execute();

			String str = call.getString(4);
			for(String temp : str.split(",")){
				result.put(temp.split("#")[1], temp.split("#")[0]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

猜你喜欢

转载自beamofsoul.iteye.com/blog/1066515