注:本文来源于 《 Java调用oracle存储过程通过游标返回临时表数据 》
Java调用oracle存储过程通过游标返回临时表数据
项目开发过程中,不可避免的会用到存储过程返回结果集。特别是选择Oracle做为数据库时,就更麻烦了,因为Oracle存储过程和SQLServer的有点不太一样,直接返回不了结果集,那么怎么才能在使用Oracle的情况下返回结果集,程序代码中怎么获取到那,请看下面几篇:
1.存储过程返回结果集
2.Java中执行存储过程以及多结果集处理
3.返回临时表中数据
1 package com.zjhcsoft.test.utl; 2 3 import java.sql.CallableStatement; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 8 import oracle.jdbc.OracleCallableStatement; 9 import oracle.jdbc.driver.OracleTypes; 10 11 public class TestOracleProc3 { 12 13 /** 14 * @param args 15 */ 16 public static void main(String[] args) { 17 try { 18 Class.forName("oracle.jdbc.driver.OracleDriver"); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 23 Connection conn = null; 24 String DBurl = "jdbc:oracle:thin:@134.98.8.168:1521:ora817"; 25 try { 26 conn = DriverManager.getConnection(DBurl, "sfz2", "sfz2_123"); 27 System.out.println("Getting Connection..."); 28 conn.close(); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 33 try { 34 conn = DriverManager.getConnection(DBurl, "sfz2", "sfz2_123"); 35 long start = System.currentTimeMillis(); 36 //最关键一步 37 conn.setAutoCommit(false); 38 OracleCallableStatement cstmt = (OracleCallableStatement)conn.prepareCall("{call sfz_obj.proc_sfz_proc_test(?,?)}"); 39 cstmt.setString(1, ""); 40 cstmt.registerOutParameter(2,OracleTypes.CURSOR); 41 cstmt.execute(); 42 long end = System.currentTimeMillis(); 43 System.out.println("this procedure consumes "+((end-start)/1000)+" excute time."); 44 start = System.currentTimeMillis(); 45 int i=0; 46 ResultSet rs = (ResultSet)cstmt.getObject(2); 47 while (rs.next()) { 48 System.out.println("column"+(i+1)+":"+rs.getString(1)+", "+rs.getString(2)+", "+rs.getString(3)); 49 i++; 50 } 51 System.out.println("this procedure has "+(i-1)+" data."); 52 end = System.currentTimeMillis(); 53 System.out.println("show this procedure data consumes "+((end-start)/1000)+" excute time."); 54 conn.commit(); 55 cstmt.close(); 56 conn.close(); 57 } catch (Exception e) { 58 e.printStackTrace(); 59 } 60 } 61 62 } 63
————————————————————————————————————————————————————————————————————————————————————————————