调用oracle中的过程,详解

--动态执行DDL
/*declare
  sqlType varchar2(500);
  begin
      sqlType:='create table houseType(tid number(11),typeName varchar2(50))';
      execute immediate sqlType;   
  end;*/
 
  --创建包头
 /* create or replace package p_house
         as
        
         --增加
        procedure p_insert(sid  number,tname varchar2);
         --更新
        procedure p_update(sid number,tname varchar2);
         --删除
         procedure p_delete(sid number);
         --查询所有
         procedure p_selectAll(r_result out sys_refcursor);
    end p_house;*/
   
   
--创建包体

  create or replace package body p_house
         as
        
         --增加
         procedure p_insert(sid  number,tname varchar2)
         is  
         begin     
           execute immediate
            'insert into houseType values(:sid,:tname)'
             using sid,tname;  
             commit;
         
         end p_insert;
        
         --更新
         procedure p_update(sid number,tname varchar2)
                   is
                   begin
                      
                      execute immediate
                              'update houseType set typeName=:tname where tid=:tid'
                              using tname,sid;
                            commit; 
                         
                   end p_update;
        
        
         --删除
        procedure p_delete(sid number)
         is
         begin
                  execute immediate
                          'delete from houseType where tid=:tid'
                          using sid;
                          commit;
         end p_delete;
        
         --查询所有
         procedure p_selectAll(r_result out sys_refcursor)
                   as
                   select_all varchar2(200):='select *from houseType';
                   begin
                          open r_result for select_all;
                   end;
                  
    end p_house;
   
   
  
   
        

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//实体类

 

package org.entity;

import java.io.Serializable;

public class HouseType implements Serializable {

 private Integer tid;
 private String typeName;
 public Integer getTid() {
  return tid;
 }
 public void setTid(Integer tid) {
  this.tid = tid;
 }
 public String getTypeName() {
  return typeName;
 }
 public void setTypeName(String typeName) {
  this.typeName = typeName;
 }
 
 public HouseType() {
  // TODO Auto-generated constructor stub
 }
 public HouseType(Integer tid, String typeName) {
  super();
  this.tid = tid;
  this.typeName = typeName;
 }
}

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

package org.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.entity.HouseType;

import oracle.jdbc.driver.OracleCallableStatement;
import oracle.jdbc.driver.OracleTypes;

//以下方法均是调用oracle中的过程方法
public class HouseDao {
 // 获得链接
 private Connection getConnection() throws ClassNotFoundException,
   SQLException {
  Class.forName("oracle.jdbc.driver.OracleDriver");
  return DriverManager.getConnection(
    "jdbc:oracle:thin:@localhost:1521:ORCL",
    "system",
    "accp");
 }

 // 更新
 public void update(int tid, String typeName) throws ClassNotFoundException,
   SQLException {

  Connection con = getConnection();
  //调用oracle中的过程 
  CallableStatement cs = con.prepareCall("{call p_house.p_update(?,?)}");
  cs.setInt(1, tid);
  cs.setString(2, typeName);
  cs.executeUpdate();
 }

 // 添加
 public void insert(int tid, String tname) throws ClassNotFoundException,
   SQLException {
  Connection con = getConnection();
  CallableStatement cs = con.prepareCall("{call p_house.p_insert(?,?)}");
  cs.setInt(1, tid);
  cs.setString(2, tname);
  cs.executeUpdate();
 }

 // 删除
 public void delete(int tid) throws ClassNotFoundException, SQLException {
  Connection con = getConnection();
  CallableStatement cs = con.prepareCall("{call p_house.p_delete(?)}");
  cs.setInt(1, tid);
  cs.executeUpdate();
 }

 // 查询所有
 public List<HouseType> selectAll() throws ClassNotFoundException,
   SQLException {
  Connection con = getConnection();
  //调用过oracle程中的方法
  CallableStatement cs = con.prepareCall("{call p_house.p_selectAll(?)}");
  
  /*
   * 替换预编译的参数,OracleTypes是oracle自带的
  * registerOutParameter是设置oralce过程中参数是输出参数OUT
  *
  */
  cs.registerOutParameter(1, OracleTypes.CURSOR);
  //执行查询
  cs.executeQuery();
  /**
   * OracleCallableStatement是oracle中自带的包
   * 需要将其强制转换
   * .getCursor(1)获得对应参数 这里指的是游标相当于resultSet结果集
   */
  ResultSet rs = ((OracleCallableStatement) cs).getCursor(1);
  List<HouseType> list = new ArrayList<HouseType>();
  //封装结果集
  while (rs.next()) {
   HouseType t = new HouseType(rs.getInt("tid"), rs
     .getString("typeName"));
   list.add(t);
  }
  return list;

 }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值