package com.rxtc.bi.app.crm.rep.dao.impl;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
*
* <pre>
* Title: Using stored procedures in hibernate
* It just needs to bypass hibernate and call the jdbc API. Of course, the transaction is still managed by hibernate:
Suppose we create the following stored procedure:
create or replace procedure batchUpdateCustomer(p_age in number) as
begin
update CUSTOMERS set AGE=AGE+1 where AGE> p_age;
end;
There is a parameter p_age in the stored procedure, which represents the age of the customer. The application can call the stored procedure in the following way:
* Description: Description of program functions
* </pre>
* @author lichunmei [email protected]
* @version 1.00.00
* <pre>
* Modify records
* Modified version: Modifier: Modification date: Modification content:
* </pre>
*/
public class Text extends HibernateDaoSupport {
public void procedure(){
try {
DataSource ds= SessionFactoryUtils.getDataSource(getSessionFactory());
Connection conn=ds.getConnection();
String sql = "{call batchUpdateCustomer(?)}";
CallableStatement cstmt = conn.prepareCall(sql);
cstmt.setInt(1, 0);//Set the age parameter to 0;
cstmt.executeUpdate();
//ResultSet rs = cstmt.executeQuery(sql); if querying
conn.commit();
//rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/xixi_666/archive/2009/12/18/5029768.aspx
-