java程序调用oracle存储过程

1、直接在java程序中用jdbc connection的方式调用:

 

private ResultSet callProcedure(Connection conn, String proName, List<Object> values) throws Exception {
		CallableStatement cstmt = null;
		ResultSet rs = null;
		try {
			StringBuffer function = new StringBuffer("{call "+proName+"(");
			
			if (values != null && values.size() > 0) {
				for (int i=0; i<values.size(); i++) {
					function.append("?,");
				}
			}
			function.append("?");
			
			function.append(")}");
			cstmt = conn.prepareCall(function.toString());
			
			for (int i=0; i<values.size(); i++) {
				cstmt.setObject(i+1, values.get(i));
			} 
			cstmt.registerOutParameter(values.size()+1, oracle.jdbc.driver.OracleTypes.CURSOR);
			cstmt.execute();
			rs = (ResultSet)cstmt.getObject(values.size()+1);
			return rs;
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally {
			try {
				cstmt.close();
			}catch (Exception e) {
				e.printStackTrace();
				throw e;
			}
		}
		
	}

 

 

2、java程序用ibatis调用存储过程:

ibatis配置:

 

<resultMap id="corp-map" class="com.procedure.domain.IbatisProDomain">
    	<result property="dm" column="DM" /> 
    	<result property="mc" column="MC" /> 
    	<result property="bz" column="BZ" />
    	.....
	</resultMap>
	<parameterMap id="curMemuParameters" class="java.util.HashMap" > 	    
	    <parameter property="dm"  javaType="java.lang.String" mode="IN"/>	        
	    <parameter property="curMemuList" jdbcType="ORACLECURSOR" javaType="java.sql.ResultSet" mode="OUT" resultMap="corp-map" />
	</parameterMap>
	<procedure id="getProcedureExample" parameterMap="curMemuParameters"> 
	    {call P_EXAMPLE_PROCEDURE(?,?)} 
	</procedure>

 

 java程序:

 

List<IbatisProDomain> returnList = new ArrayList<IbatisProDomain>();
		Map<String,Object> parameterMap = new HashMap<String,Object>();   
		if(domain != null){			
			parameterMap.put("dm","01");
			parameterMap.put("curMemuList",null);
			sqlMapClientTemplate.queryForObjectByCurr("getProcedureExample","curMemuList",parameterMap); 
			returnList = (List<IbatisProDomain>)parameterMap.get("curMemuList");	
		}

你可能感兴趣的:(java,oracle,存储过程,ibatis,procedure)