java调用存储过程


一下代码是在grails框架中的Service包中书写的,所以各位在参照的时候,不能照搬 只能作为借鉴 

第一种情况 存储过程没有输出:类似于

procedure   XX(  

                          p_operator_id  in  varchar2,--操作人员ID
                          p_num                in VARCHAR2 --日常作业计划数
                        )  。。。。。。。。。。。。。。。。。

1。导Oracle驱动包(不多说了。。)

2.。获得数据库连接

 def dbDriver = "oracle.jdbc.driver.OracleDriver";
  def url =  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  def username = "xx";
  def password = "xx";
  Connection conn = null;
  Class.forName(dbDriver);
  DriverManager.setLoginTimeout(30);
3 。得到参数
  CallableStatement proc=null;
  conn = DriverManager.getConnection(url, username, password);

4。调用存储过程

                                                        //包名.存储名(参数)注意:有几个参数(输入,输出都包括),写几个问号

   proc=conn.prepareCall("{call PKG_SQM.P_MESSAGE_SEND(?,?,?,?,?,?)}");
   proc.setString(1, p_type);
   proc.setString(2, p_flag);
   proc.setString(3, p_person_flag);
   proc.setString(4, p_user_id) ;
   proc.setString(5, p_operator_id) ;
   proc.setString(6, p_num);
   proc.execute(); 

5.关闭 

     proc.close();
     conn.close();

第一种情况 存储过程有输出:类似于

 procedure  YY(

                               p_start_date   in varchar2,--开始时间 2012-12-01
                               p_end_date     in varchar2,--结束时间 2012-12-18
                               p_cursor       out t_cursor)。。。。

1。导Oracle驱动包(不多说了。。)

2.。获得数据库连接

 def dbDriver = "oracle.jdbc.driver.OracleDriver";
  def url =  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  def username = "xx";
  def password = "xx";
  Connection conn = null;
  Class.forName(dbDriver);
  DriverManager.setLoginTimeout(30);
3 。得到参数
  CallableStatement proc=null;
  conn = DriverManager.getConnection(url, username, password);

4。调用存储过程

 proc = conn.prepareCall("{call PKG_XYYYCP.p_xyyycp_stat(?,?,?,?,?)}");
   proc.setString(1, start_d);
   proc.setString(2, end_d);
   proc.setString(3, flag);
   proc.setString(4, areaCode);
   proc.registerOutParameter(5, oracle.jdbc.OracleTypes.CURSOR);
   proc.execute();

  rs = (ResultSet)proc.getObject(5);

 def num_xyyycp_price="";
    if(null != rs){
    while(rs.next()){
           num_xyyycp_price=num_xyyycp_price+","+rs.getObject("num_xyyycp_price")  //存储过程要返回的字段名。如果要返回多个字段,照这样写多个就行。
     }
   }

5。关闭连接。

  proc.close();
   conn.close();

 

有输出的话 可以使用一个公共方法来调用。这是公共方法。                                                                                               public static List<List<String>> getList(String call,String[] params){
		List<List<String>>  list = new ArrayList<List<String>>();
		Connection con = SqlCon.getCon();//写一个公共方法获得数据库的连接。
		CallableStatement proc = null;
		ResultSet rs = null;
		try{
			proc = con.prepareCall(call);
			int index = 0;
			if(params != null && params.length > 0){
				for (int i = 1; i <= params.length; i++) {
					proc.setString(i, params[index]);
					index +=1;
				}
			}
			index = index+1;
			proc.registerOutParameter(index, oracle.jdbc.OracleTypes.CURSOR);
			proc.execute();
			rs = (ResultSet)proc.getObject(index);
			ResultSetMetaData meta = rs.getMetaData();
			int columnCount = meta.getColumnCount();
			List<String> temp ;
			String str;
			while (rs.next()){
				temp = new ArrayList<String>();
				for(int k=1;k<=columnCount;k++){
					if(rs.getObject(k)==null){
						str = "";
					}else{
						str = rs.getObject(k).toString();
					}
					temp.add(str);
					str = null;
				}
				list.add(temp);
				temp = null;
			}
		}catch (Exception e) {
			System.out.println("exec procedure exception... ...");
			e.printStackTrace();
		}finally{
			SqlCon.closeCstmt(proc);
			SqlCon.closeRst(rs);
			SqlCon.closeConn(con);
		}
		return list;
	}                                                                                                                         在Controller中调用 String[] p = new String[6];
  p[0] = p_flag;
  p[1] = p_lan_code;
  p[2] = p_task_type;
  p[3] = p_user_type;
  p[4] = p_start_date;
  p[5] = p_end_date;
 def list  = ProcedureUtil.getList("{call PKG_REPORT.p_report_rcxj(?,?,?,?,?,?,?)}",p);
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值