Java调用存储过程
Posted 倾我一生一世
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java调用存储过程相关的知识,希望对你有一定的参考价值。
Java调用存储过程
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl,
"hyq", "hyq");
CallableStatement proc = null;
proc = conn.prepareCall("{ call hyq.testc(?)
}");
proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);
proc.execute();
rs = (ResultSet)proc.getObject(1);
while(rs.next())
- //调用存储过程 删除流程相关记录
- String procdure = "{Call sp_deleteInstByRootID(?)}";
- CallableStatement cs = this.getHibernateTemplate().getSessionFactory().getCurrentSession().connection().prepareCall(procdure);
- //this.getSession().connection().prepareCall(procdure).setString(0, orgdefid);
- cs.setString(1, procinstid);
- cs.execute();
ibatis配置文件:
复制代码
<parameterMap id="parameterMapCesu" class="java.util.Map">
<!-- 参数 -->
<parameter property="cesuDate" jdbcType="VARCHAR2" javaType="java.lang.String" mode="IN" />
<parameter property="csCount" jdbcType="DOUBLE" javaType="java.lang.Long" mode="OUT"/>
</parameterMap>
<!-- 调用存储过程-->
<procedure id="cesu" parameterMap="parameterMapCesu">
{call handiwork(?,?)}
</procedure>
Java代码
public Long doTest(final String cesuDate) {
return (Long) this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
@SuppressWarnings("unchecked")
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
long csCount = 0;
try {
//设置存储过程参数
Map cesu = new HashMap();
cesu.put("cesuDate", cesuDate);//输入参数
cesu.put("csCount", 0);//输出参数
//调用存储过程
executor.queryForObject("test.cesu",cesu);//ibatis文件的namespace是test
csCount = (Long)cesu.get("csCount");//获取返回值
return csCount;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
});
}
- /**
- * 直接调用存储过程
- * @param procString
- * @author kongqz
- * @throws Exception
- * @date 2009-03-03
- * **/
- public void callProcedure(String procString,List<Object> params) throws Exception {
- CallableStatement stmt = null;
- try {
- stmt = this.getSession().connection().prepareCall(procString);
- if (params != null){
- int idx = 1;
- for (Object obj : params) {
- if (obj != null) {
- stmt.setObject(idx, obj);
- } else {
- stmt.setNull(idx, Types.NULL);
- }
- idx++;
- }
- }
- stmt.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- throw new Exception("调用存储过程的时候发生错误[sql = " + procString + "]", e);
- }
- }
或
tx = session.beginTransaction();
Connection con=session.connection();
String procedure = "{call batchUpdateStudent(?) }";
CallableStatement cstmt = con.prepareCall(procedure);
cstmt.setInt(1,0); //把年龄参数设为0
cstmt.executeUpdate();
tx.commit();
spring调用存储过程
1.继承StoredProcedure
org.springframework.jdbc.object.StoredProcedure是对应存储过程调用的操作对象,它通过其父类
org.springframework.jdbc.object.SqlCall获得相应的底层API支持(CallableStatementCreator), 然
后在此基础之上构建了调用存储过程的执行方法。
2、重写父类的execute()方法。将存储过程的参数封装成Map类型的传入该方法
.
3、写一个方法来封装存储过程的方法及把参数放到Map里面.
如:
Map paraMap = new HashMap();
paraMap.put(IN_PARAMETER_NAME, tableName);
paraMap.put(INOUT_PARAMETER_NAME, v);
注意:key值一定要与前面构造函数里面声明的参数一致。
4、execute()返回的map值要取到里面的value值,可以用前面构造函数声明时候用到的key值去取。
如:(String)resultMap.get(OUT_PARAMETER_NAME);就得到了存储过程的返回值。
示例如下:
public class xxxxProcedure extends
StoredProcedure {
private static final String PROCEDURE_NAME =
"xxxx";
protected xxxxProcedure() {
/* empty */
}
public xxxxProcedure(JdbcTemplate jdbcTemplate) {
super(jdbcTemplate, PRO_NAME);
declareParameter(new
SqlParameter("xxxx", Types.VARCHAR));
declareParameter(new
SqlParameter("xxxx", Types.VARCHAR));
declareParameter(new
SqlParameter("xxxx", Types.NUMERIC));
}
public void execute(String xxxx,String xxxx,int xxxx) {
Map<String, Object> paramsIn = new
HashMap<String, Object>();
paramsIn.put("xxxx", xxxx);
paramsIn.put("xxxx", xxxx);
paramsIn.put("xxxx", xxxx);
super.execute(paramsIn);
}
}
在mybatis中调用存储过程,然后获取该结果集:
1、xml配置文件
<?xml version="1.0"
encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ifeng.iis.bean.iis.Report" >
<resultMap type="java.util.HashMap"
id="resultMap">
<result column="id" property="id"
javaType="java.lang.Integer" jdbcType="INTEGER"/>
<result column="name" property="name"
javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="repDate"
property="repDate" javaType="java.lang.String"
jdbcType="VARCHAR"/>
<result column="summ"
property="summ" javaType="java.lang.Long"
jdbcType="BIGINT"/>
</resultMap>
<select id="test123"
parameterType="java.util.Map" resultMap="resultMap"
statementType="CALLABLE" >
{call pro_sql_data(
#{obj,jdbcType=VARCHAR,mode=IN}
)
}
</select>
</mapper>
Java代码
public String query(String param)
throws Exception {
logger.info(param);
Map queryMap = new HashMap();
queryMap.put("obj", param);
//List<Map> listIis1 = reportDao.select4MapParam(queryMap,
"currentSql");
List<Map> listIis2 =reportDao.select4MapParam(queryMap,"test123");
return JSONArray.fromObject(listIis2).toString();
}
以上是关于Java调用存储过程的主要内容,如果未能解决你的问题,请参考以下文章
我如何用JAVA调用存储过程取得 serveroutput?