摘-- 關於在函式或過程中 ref cursor 的用法
http://blog.itpub.net/post/16888/149516
Oracle 儲存過程返回結果集用 ref cursor 實現。試驗步驟如下:
1. 建立 ref cursor 型別和過程
CREATE OR REPLACE PACKAGE types
AS
TYPE ref_cursor IS REF CURSOR;
END;
/
CREATE TABLE STOCK_PRICES(
RIC VARCHAR(6) PRIMARY KEY,
PRICE NUMBER(7,2),
UPDATED DATE );
/
CREATE OR REPLACE FUNCTION sp_get_stocks(v_price IN NUMBER)
RETURN types.ref_cursor
AS
stock_cursor types.ref_cursor;
BEGIN
OPEN stock_cursor FOR
SELECT ric,price,updated FROM stock_prices WHERE price < v_price;
RETURN stock_cursor;
END;
2. 用 sqlplus 測試過程
SQL> var results refcursor
SQL> exec :results := sp_get_stocks(20.0)
SQL> print results
3. 從 Java 呼叫
import java.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;
public class JDBCDemo {
/**
* Compile-time flag for deciding which query to use
*/
private boolean useOracleQuery = true;
/**
* Class name of Oracle JDBC driver
*/
private String driver = "oracle.jdbc.driver.OracleDriver";
/**
* Initial url fragment
*/
private String url = "jdbc:oracle:thin:@";
/**
* Standard Oracle listener port
*/
private String port = "1521";
/**
* Oracle style of calling a stored procedure
*/
private String oracleQuery = "begin ? := sp_get_stocks(?); end;";
/**
* JDBC style of calling a stored procedure
*/
private String genericQuery = "{ call ? := sp_get_stocks(?) }";
/**
* Connection to database
*/
private Connection conn = null;
/**
* Constructor. Loads the JDBC driver and establishes a connection
*
* @param host the host the db is on
* @param db the database name
* @param user user's name
* @param password user's password
*/
public JDBCDemo(String host, String db, String user, String password)
throws ClassNotFoundException, SQLException {
// construct the url
url = url + host + ":" + port + ":" + db;
// load the Oracle driver and establish a connection
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
}
catch (ClassNotFoundException ex) {
System.out.println("Failed to find driver class: " + driver);
throw ex;
}
catch (SQLException ex) {
System.out.println("Failed to establish a connection to: " + url);
throw ex;
}
}
/**
* Execute the stored procedure
*
* @param price price parameter for stored procedure
*/
private void execute(float price)
throws SQLException {
String query = useOracleQuery ? oracleQuery : genericQuery;
System.out.println("Query: " + query + "n");
CallableStatement stmt = conn.prepareCall(query);
// register the type of the out param - an Oracle specific type
stmt.registerOutParameter(1, OracleTypes.CURSOR);
// set the in param
stmt.setFloat(2, price);
// execute and retrieve the result set
stmt.execute();
ResultSet rs = (ResultSet)stmt.getObject(1);
// print the results
while (rs.next()) {
System.out.println(rs.getString(1) + "t" +
rs.getFloat(2) + "t" +
rs.getDate(3).toString());
}
rs.close();
stmt.close();
}
/**
* Cleanup the connection
*/
private void cleanup() throws SQLException {
if (conn != null)
conn.close();
}
/**
* Prints usage statement on stdout
*/
static private void usage() {
System.out.println("java com.enterprisedt.demo.oracle.JDBCDemo " +
" host db user password price");
}
/**
* Runs the class
*/
public static void main(String[] args) throws Exception {
if (args.length != 5) {
JDBCDemo.usage();
System.exit(1);
}
else {
try {
// assign the args to sensible variables for clarity
String host = args[0];
String db = args[1];
String user = args[2];
String password = args[3];
float price = Float.valueOf(args[4]).floatValue();
// and execute the stored proc
JDBCDemo jdbc = new JDBCDemo(host, db, user, password);
jdbc.execute(price);
jdbc.cleanup();
}
catch (ClassNotFoundException ex) {
System.out.println("Demo failed");
}
catch (SQLException ex) {
System.out.println("Demo failed: " + ex.getMessage());
}
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7425608/viewspace-997525/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於lag函式的用法函式
- Lua中呼叫ref和out修飾引數的函式/過載函式函式
- mysql儲存過程procedure、函式function的用法MySql儲存過程函式Function
- 在Oracle中查詢儲存過程和函式Oracle儲存過程函式
- 關於React的refReact
- Oracle中關於函式的使用Oracle函式
- 關於qt中的tr()函式QT函式
- Rust 中 *、&、mut、&mut、ref、ref mut 的用法和區別Rust
- oracle 建立過程、函式中as, is區別 ?Oracle函式
- 關於cuda中的函式問題函式
- PHP 手冊中的匿名函式關聯用法分析PHP函式
- PL/SQL 中的儲存過程與函式SQL儲存過程函式
- mysql中replace函式的用法MySql函式
- 關於Python中函式過載問題的思考(原創)Python函式
- React ref的用法React
- 關於學習過程中走過的彎路
- mysql procedure 中 repeat &cursor 的用法。MySql
- 關於python建構函式的過載Python函式
- iOS中關於專案中打包ipa的過程iOS
- Oracle中過程/函式返回結果集Oracle函式
- 關於 js 中的回撥函式 callbackJS函式
- 關於scala中的主建構函式函式
- 關於C++當中的“模板函式”C++函式
- 關於SQL Server中儲存過程在C#中呼叫的簡單示例SQLServer儲存過程C#
- 關於 lambda 在 WebDriverWait () 中的用法實在不明白WebAI
- [轉貼於菜鳥之Oracle & Java]在sqlplus中呼叫函式和儲存過程OracleJavaSQL函式儲存過程
- 【圖文】函式呼叫過程中棧的變化函式
- SQL中儲存過程和函式的區別SQL儲存過程函式
- Oracle 遷移到 OB 過程中的函式改造案例Oracle函式
- PL/SQL在執行過程中,對其依賴的表,函式,過程都加lock和pin嗎?SQL函式
- C++中函式呼叫的用法C++函式
- python中zip()函式的用法Python函式
- Matlab中erf函式的用法Matlab函式
- Java中Split函式的用法技巧Java函式
- matlab中sort函式的用法Matlab函式
- vue --ref用法Vue
- 儲存過程 函式儲存過程函式
- C++通過occi執行select語句、儲存過程、函式,取cursor值示例C++儲存過程函式