refcursorexample.java
来自「比较实用的C++编程思想 有很多例子可以用」· Java 代码 · 共 71 行
JAVA
71 行
/* * This sample shows how to call a PL/SQL function that opens * a cursor and get the cursor back as a Java ResultSet. */import java.sql.*;import java.io.*;// Importing the Oracle Jdbc driver package makes the code more readableimport oracle.jdbc.driver.*;class RefCursorExample{ public static void main (String args []) throws SQLException { // Load the driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // Connect to the database // You can put a database name after the @ sign in the connection URL. Connection conn = DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger"); // Create the stored procedure init (conn); // Prepare a PL/SQL call CallableStatement call = conn.prepareCall ("{ ? = call java_refcursor.job_listing (?)}"); // Find out all the SALES person call.registerOutParameter (1, OracleTypes.CURSOR); call.setString (2, "SALESMAN"); call.execute (); ResultSet rset = (ResultSet)call.getObject (1); // Dump the cursor while (rset.next ()) System.out.println (rset.getString ("ENAME")); // Close all the resources rset.close(); call.close(); conn.close(); } // Utility function to create the stored procedure static void init (Connection conn) throws SQLException { Statement stmt = conn.createStatement (); stmt.execute ("create or replace package java_refcursor as " + " type myrctype is ref cursor return EMP%ROWTYPE; " + " function job_listing (j varchar2) return myrctype; " + "end java_refcursor;"); stmt.execute ("create or replace package body java_refcursor as " + " function job_listing (j varchar2) return myrctype is " + " rc myrctype; " + " begin " + " open rc for select * from emp where job = j; " + " return rc; " + " end; " + "end java_refcursor;"); stmt.close(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?