注:本文来源于 《 Java调用oracle存储过程通过游标返回临时表数据 》
Java调用oracle存储过程通过游标返回临时表数据
项目开发过程中,不可避免的会用到存储过程返回结果集。特别是选择Oracle做为数据库时,就更麻烦了,因为Oracle存储过程和SQLServer的有点不太一样,直接返回不了结果集,那么怎么才能在使用Oracle的情况下返回结果集,程序代码中怎么获取到那,请看下面几篇:
1.存储过程返回结果集
2.Java中执行存储过程以及多结果集处理
3.返回临时表中数据
package com.zjhcsoft.test.utl; import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet; import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.driver.OracleTypes; public class TestOracleProc3 { /**
* @param args
*/
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
} Connection conn = null;
String DBurl = "jdbc:oracle:thin:@134.98.8.168:1521:ora817";
try {
conn = DriverManager.getConnection(DBurl, "sfz2", "sfz2_123");
System.out.println("Getting Connection...");
conn.close();
} catch (Exception e) {
e.printStackTrace();
} try {
conn = DriverManager.getConnection(DBurl, "sfz2", "sfz2_123");
long start = System.currentTimeMillis();
//最关键一步
conn.setAutoCommit(false);
OracleCallableStatement cstmt = (OracleCallableStatement)conn.prepareCall("{call sfz_obj.proc_sfz_proc_test(?,?)}");
cstmt.setString(1, "");
cstmt.registerOutParameter(2,OracleTypes.CURSOR);
cstmt.execute();
long end = System.currentTimeMillis();
System.out.println("this procedure consumes "+((end-start)/1000)+" excute time.");
start = System.currentTimeMillis();
int i=0;
ResultSet rs = (ResultSet)cstmt.getObject(2);
while (rs.next()) {
System.out.println("column"+(i+1)+":"+rs.getString(1)+", "+rs.getString(2)+", "+rs.getString(3));
i++;
}
System.out.println("this procedure has "+(i-1)+" data.");
end = System.currentTimeMillis();
System.out.println("show this procedure data consumes "+((end-start)/1000)+" excute time.");
conn.commit();
cstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
————————————————————————————————————————————————————————————————————————————————————————————