java中使用引用遊標

atlantisholic發表於2011-04-26
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章