首页 > 代码库 > Java 返回结果集的存储过程

Java 返回结果集的存储过程

--返回结果集的存储过程--1.创建一个包create package myTestPackage astype test_cursor is ref cursor;end myTestPackage;--2.创建存储过程create procedure sp_pro9(       spNo in number,       p_cursor out myTestPackage.test_cursor) isbegin  open p_cursor for        select * from student where classId=spNo; end;--3.在Java中调用
 1 package oracle; 2  3 import java.sql.CallableStatement; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8  9 import oracle.jdbc.OracleTypes;10 11 12 public class Test02 {13 14     public static void main(String[] args) {15         // TODO Auto-generated method stub16                 Connection conn = null;17                 CallableStatement cs = null;18                 try {19                     //1.加载驱动20                     Class.forName("oracle.jdbc.driver.OracleDriver");21                     22                     //2.连接23                     String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORACLE12C";24                     String userName = "sys as SYSDBA";25                     String password = "fairy6280";26                     conn = DriverManager.getConnection(url,userName,password);27                     28                     //3.创建CallableStatement29                     String procedure = "{call sp_pro9(?,?)}";30                     cs = conn.prepareCall(procedure);31                     cs.setInt(1, 301);32                     cs.registerOutParameter(2, OracleTypes.CURSOR);33                     cs.execute();34                     35                     //得到结果集36                     ResultSet rSet = (ResultSet)cs.getObject(2);37                     while(rSet.next())38                     {39                         String sidStr = "学号:"+ rSet.getInt(1);40                         String sNameStr = ",姓名" + rSet.getString(2);41                         System.out.println(sidStr+sNameStr);42                     }43                     44                 } catch (Exception e) {45                     // TODO Auto-generated catch block46                     e.printStackTrace();47                 }48                 finally{49                     //关闭50                     try {51                         cs.close();52                         conn.close();53                     } catch (SQLException e) {54                         // TODO Auto-generated catch block55                         e.printStackTrace();56                     }57                 }58     }59 60 }

Java 返回结果集的存储过程