在Java语言中访问游标类型详解


public class Testclass {
 private  static Connection conn;
 static{
  //加载驱动
  try {
   Class.forName("Oracle.jdbc.driver.OracleDriver");
  
   conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","scott");
  
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
 }

public static void testProcedure() throws SQLException{
   CallableStatement cas=conn.prepareCall("{call emppackage.selectEmps(?)}");
   int index=1;
   cas.registerOutParameter(index++,oracle.jdbc.OracleTypes.CURSOR);
   boolean flag=cas.execute();
   System.out.println(flag);
   //OracleCallableStatement 可使强制造型为oracle中的存储过程的对象
   //调用里面的getCursor方法获取返回的ResultSet的结果集
   ResultSet rs=((OracleCallableStatement)cas).getCursor(1);
   //遍历输出
   while(rs.next()){
    System.out.println(rs.getInt(1));
   
   
   }

public static void functionTest2() throws SQLException{
    CallableStatement cas =conn.prepareCall("{?=call mypackage.queryEmps}");
   int index=1;
   cas.registerOutParameter(index++, oracle.jdbc.OracleTypes.CURSOR);
   boolean flag=cas.execute();
   System.out.println(flag);
   ResultSet rs=((OracleCallableStatement)cas).getCursor(1);
   while(rs.next()){
    System.out.println(rs.getInt(1));
   
   }

}

相关内容