时间:2021-07-01 10:21:17 帮助过:15人阅读
2.包体如下:
1 CREATE OR REPLACE PACKAGE BODY JAVALINKTEST IS 2 3 PROCEDURE TEST_CURSOR(INPUT STRING, CURSOR_BACK OUT CURSOR_TYPE) 4 IS 5 BEGIN 6 IF INPUT = ‘物料‘ THEN 7 OPEN CURSOR_BACK FOR SELECT * FROM T_BD_MATERIAL WHERE CFISSYNC = 1; 8 ELSE 9 OPEN CURSOR_BACK FOR SELECT * FROM T_BD_CUSTOMER WHERE CFISSYNC = 1; 10 11 END IF; 12 13 END TEST_CURSOR; 14 END JAVALINKTEST;
3.java环境,用的时eclispe,oracle 11g,java代码如下,需要的小伙伴自己引一下jdbc包吧
1 package cursorTest; 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 import java.sql.Statement; 9 import oracle.jdbc.OracleTypes; 10 import oracle.jdbc.oracore.OracleType; 11 12 public class cursorUse { 13 static ResultSet rs = null; 14 static Statement stmt = null; 15 static Connection conn = null; 16 static CallableStatement proc = null; 17 static int i; 18 19 public static void main(String[] args) { 20 21 try { 22 // 加载驱动 23 Class.forName("oracle.jdbc.driver.OracleDriver"); 24 // 与数据库建立物理连接 25 conn = DriverManager.getConnection( 26 "jdbc:oracle:thin:@127.0.0.1:1521:orcl", "easdb", "easdb"); 27 // 调用 游标 28 proc = conn.prepareCall("call JAVALINKTEST.TEST_CURSOR(?,?)"); 29 proc.setString(1, "hhe"); 30 proc.registerOutParameter(2, OracleTypes.CURSOR); 31 proc.execute(); 32 rs = (ResultSet) proc.getObject(2); 33 34 while (rs.next()) { 35 i+=1; 36 System.out.println("fid:" + rs.getString(1)+ 37 " 编码:"+rs.getString("fnumber")+" 名称:"+rs.getString("fname_l2")); 38 39 } 40 System.out.println("返回结果集:共"+i+"行"); 41 } catch (ClassNotFoundException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } catch (SQLException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } finally { 48 if (rs != null) { 49 try { 50 rs.close(); 51 } catch (SQLException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 } 56 if (stmt != null) { 57 try { 58 stmt.close(); 59 } catch (SQLException e) { 60 // TODO Auto-generated catch block 61 e.printStackTrace(); 62 } 63 } 64 if (conn != null) { 65 try { 66 conn.close(); 67 } catch (SQLException e) { 68 // TODO Auto-generated catch block 69 e.printStackTrace(); 70 } 71 } 72 } 73 74 } 75 76 }
运行结果如下:
java实现调用ORACLE中的游标和包
标签:lock ora next 建立 auto cat material import prepare