java中使用引用游标

1.创建使用引用游标的存储过程。
create or replace procedure getCursor(cur out sys_refcursor)
as
begin
open cur for select * from user_objects;
end;
 
2.使用java调用该存储过程。
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;
public class Test {
 /**
  * @param args
  * @throws SQLException
  */
 public static void main(String[] args) throws SQLException {
         DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
         Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xxxholic","infogrid","infogrid");
         String sql="{call getCursor(?)}";
         CallableStatement ps=con.prepareCall(sql);
         ps.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);
         ps.execute();
         ResultSet rs = ((OracleCallableStatement)ps).getCursor(1);
         while(rs.next()){
              System.out.println(rs.getObject(1));
         }
   }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23071790/viewspace-693818/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23071790/viewspace-693818/

你可能感兴趣的:(java中使用引用游标)