Java调⽤Oracle存储过程返回结果集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)
f_cursor
AS
stock_f_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";
oracle手动调用存储过程/**
* 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 = Connection(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
// set the in param
stmt.setFloat(2, price);
// execute and retrieve the result set
ResultSet rs = (Object(1);
// print the results
while (rs.next()) {
System.out.String(1) + "t" +
}
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("acle.JDBCDemo " + " host db user password price");
}
/**
* Runs the class
*/
public static void main(String[] args) throws Exception {
if (args.length != 5) {
JDBCDemo.usage();
}
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); ute(price);
jdbc.cleanup();
}
catch (ClassNotFoundException ex) {
System.out.println("Demo failed");
}
catch (SQLException ex) {
System.out.println("Demo failed: " + ex.getMessage());
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论