oracle java存储过程返回值类型_Java 调用 Oracle 存储过程返回结果集

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 title

System.out.println("ric   price    updated");

System.out.println("---   -----    -------");

// print the results

while (rs.next()) {

System.out.println(rs.getString(1) + "     " +

rs.getFloat(2) + "     " +

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());

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值