在Hibernate中用jdbc做存储过程的实现
来源:优易学  2011-12-22 18:05:08   【优易学:中国教育考试门户网】   资料下载   IT书店
    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;
  /**
  *
  *

    * Title:在hibernate中用存储过程

  * 只是要绕过hibernate 调用jdbc API,当然事务还是由hibernate业管理:

 

  假如我们创建了以下存储过程:


 create or replace procedure batchUpdateCustomer(p_age in number) as
  begin
  update CUSTOMERS set AGE=AGE+1 where AGE> p_age;
  end;

  存储过程中有一个参数p_age,代表客户的年龄,应用程序可按照以下方式调用存储过程:

 * Description: 程序功能的描述
  *
  * @version 1.00.00
  *
  * 修改记录
  * 修改后版本: 修改人: 修改日期: 修改内容:
  *
  */
  public class Text extends HibernateDaoSupport {
  public void proceduce(){
  try {
  DataSource ds= SessionFactoryUtils.getDataSource(getSessionFactory());
  Connection conn=ds.getConnection();
  String sql = "{call batchUpdateCustomer(?)}";
  CallableStatement cstmt = conn.prepareCall(sql);
  cstmt.setInt(1, 0);//把年龄参数设置为0;
  cstmt.executeUpdate();
  //ResultSet rs = cstmt.executeQuery(sql);若是查询的时候
  conn.commit();
  //rs.close();
  conn.close();
  } catch (SQLException e) {
  e.printStackTrace();
  }
  }
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训