在Hibernate中用jdbc做存储过程
packagecom.rxtc.bi.app.crm.rep.dao.impl;
importjava.sql.CallableStatement;
importjava.sql.Connection;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjavax.sql.DataSource;
importorg.springframework.orm.hibernate3.SessionFactoryUtils;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
*
*
* Title:在hibernate中用存储过程
* 只是要绕过hibernate 调用jdbc API,当然事务还是由hibernate业管理:
假如我们创建了以下存储过程:
createorreplaceprocedurebatchUpdateCustomer(p_ageinnumber)as
begin
updateCUSTOMERSsetAGE=AGE+1whereAGEp_age;
end;
存储过程中有一个参数p_age,代表客户的年龄,应用程序可按照以下方式调用存储过程:
*Description:程序功能的描述
*
*@version1.00.00
*
*修改记录
*修改后版本:修改人:修改日期:修改内容:
*
*/
publicclassTextextendsHibernateDaoSupport{
publicvoidproceduce(){
try{
DataSourceds=SessionFactoryUtils.getDataSource(getSessionFactory());
Connectionconn=ds.getConnection();
Stringsql="{callbatchUpdateCustomer(?)}";
CallableStatementcstmt=conn.prepareCall(sql);
cstmt.setInt(1,0);//把年龄参数设置为0;
cstmt.executeUpdate();
//ResultSetrs=cstmt.executeQuery(sql);若是查询的时候
conn.commit();
//rs.close();
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
| 广告合作:400-664-0084 全国热线:400-664-0084 Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号 珠峰网 版权所有 All Rights Reserved
|