以下开发
例子参见两个附件的图片:
JAVA调用
ORACLE的存储过程、函数的返回结果集的JAVA开发界面.JPG
JAVA调用ORACLE的存储过程、函数的返回结果集的PL-SQLDEPVELOPER界面.JPG
具体过程如下:
--例子开发工具:
oralce数据库
版本:oralce9i ,10G
oralce的JDBC驱动:
class12.zip ,主要下载该驱动包后,才可以连接oracle
oralce数据库
脚本执行工具: PL/SQL
Developer Version 7.1.4.1390
JAVA的IDE工具: ECLIPSE 3.1以上
--开始准备:
-- 用SYSTEM或者SYS用户 登陆到oralce中
-- 建立ZZH_TEST用户,该用户的密码为ZZH_TEST
--
--用ZZH_TEST用户进入到 PL/SQL Develope中,以下表、存储过程、函数均建立在ZZH_TEST用户中
--(一).建立一张测试表
--建立表:
CREATE TABLE STOCK_PRICES(
RIC VARCHAR(6) PRIMARY
KEY,
PRICE NUMBER(7,2),
UPDATED DATE );
/
--对表插入数据
INSERT INTO stock_prices
SELECT '1110',1.0,SYSDATE FROM DUAL;
INSERT INTO stock_prices
SELECT '1111',2.0,SYSDATE FROM DUAL;
INSERT INTO stock_prices
SELECT '1112',3.0,SYSDATE FROM DUAL;
INSERT INTO stock_prices
SELECT '1113',4.0,SYSDATE FROM DUAL;
--检查数据是否插入
SELECT * FROM stock_prices
--(二).建立一个返回游标
CREATE OR REPLACE PACKAGE ZZH_TEST.PKG_PUB_UTILS IS
--动态游标
TYPE REFCURSOR IS REF CURSOR;
END PKG_PUB_UTILS;
/
--(三).创建一个测试用的存储过程
CREATE OR REPLACE PROCEDURE ZZH_TEST.P_GET_PRICE(AN_O_RET_CODE OUT NUMBER,
AC_O_RET_MSG OUT VARCHAR2,
CUR_RET OUT PKG_PUB_UTILS.REFCURSOR,
AN_I_PRICE IN NUMBER
) IS
BEGIN
AN_O_RET_CODE := 0;
AC_O_RET_MSG := '操作成功';
OPEN CUR_RET FOR
SELECT * FROM STOCK_PRICES WHERE PRICE<AN_I_PRICE;
EXCEPTION
WHEN OTHERS THEN
AN_O_RET_CODE := -1;
AC_O_RET_MSG := '
错误代码:' || SQLCODE || CHR(13) || '错误信息:' || SQLERRM;
END P_GET_PRICE;
--(四).创建一个测试用的函数:
CREATE OR REPLACE FUNCTION ZZH_TEST.F_GET_PRICE(v_price IN NUMBER)
RETURN PKG_PUB_UTILS.REFCURSOR
AS
stock_cursor PKG_PUB_UTILS.REFCURSOR;
BEGIN
OPEN stock_cursor FOR
SELECT ric,price,updated FROM stock_prices WHERE price < v_price;
RETURN stock_cursor;
END;
--(五).开发JAVA调用存储过程返回结果集的例子:JDBCoracle10G_INVOKEPROCEDURE
--(六).开发JAVA调用
函数返回结果集的例子:JDBCoracle10G_INVOKEFUNCTION
-----------------------------------------------------------------------------------------------------------------------------------
--=======================================================================
-----------------------------------------------------------------------------------------------------------------------------------
--2份JAVA源代码:
-- (五).开发JAVA调用存储过程返回结果集的例子:JDBCoracle10G_INVOKEPROCEDURE
import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
/* 本例是通过调用oracle的存储过程来返回结果集:
* oracle 9i、10G 的jdbc由1个jar包组成:classes12.zip
*/
public class JDBCoracle10G_INVOKEPROCEDURE {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
CallableStatement stmt = null;
String driver;
String url;
String user;
String pwd;
String sql;
String in_price ;
public JDBCoracle10G_INVOKEPROCEDURE()
{
driver = "oracle.jdbc.driver.OracleDriver";;
url = "jdbc:oracle:thin:@10.168.1.193:1521:orcl";
//oracle 用户
user = "ZZH_TEST";
//oracle 密码
pwd = "ZZH_TEST";
init();
//mysid:必须为要连接机器的sid名称,否则会包以下错:
// java.sql.SQLException: Io
异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
//参考连接方式:
// Class.forName( "oracle.jdbc.driver.OracleDriver" );
// cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
}
public void init()
{
System.out.println("oracle jdbc test");
try{
Class.forName(driver);
System.out.println("driver is ok");
conn = DriverManager.getConnection(url,user,pwd);
System.out.println("conection is ok");
statement = conn.createStatement();
// conn.setAutoCommit(false);
//输入参数
in_price="3.0";
//调用函数
stmt = conn.prepareCall( "call P_GET_PRICE(?,?,?,?)");
stmt.registerOutParameter(1, java.sql.Types.FLOAT);
stmt.registerOutParameter(2, java.sql.Types.CHAR);
stmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
stmt.setString(4, in_price);
stmt.executeUpdate();
int retCode = stmt.getInt(1);
String retMsg = stmt.getString(2);
if (retCode == -1) { //如果出错时,返回错误信息
System.out.println("报错!");
} else {
//取的结果集的方式一:
rs = ((OracleCallableStatement)stmt).getCursor(3);
//取的结果集的方式二:
// rs = (ResultSet) stmt.getObject(3);
String ric ;
String price ;
String updated ;
//对结果进行输出
while(rs.next()){
ric = rs.getString(1);
price = rs.getString(2);
updated = rs.getString(3);
System.out.println("ric:"+ric+";-- price:"+price+"; --"+updated+"; ");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
System.out.println("close ");
}
}
public static void main(String args [])//自己替换[]
{
new JDBCoracle10G_INVOKEPROCEDURE();
}
}
--=======================================================================
--源代码:
--(六).开发JAVA调用函数返回结果集的例子:JDBCoracle10G_INVOKEFUNCTION
import java.sql.*;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
/*
/* 本例是通过调用oracle的函数来返回结果集:
* oracle 9i、10G 的jdbc由1个jar包组成:classes12.zip
*/
public class JDBCoracle10G_INVOKEFUNCTION {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
CallableStatement stmt = null;
String driver;
String url;
String user;
String pwd;
String sql;
String in_price ;
public JDBCoracle10G_INVOKEFUNCTION()
{
driver = "oracle.jdbc.driver.OracleDriver";;
url = "jdbc:oracle:thin:@10.168.1.193:1521:orcl";
//oracle 用户
user = "ZZH_TEST";
//oracle 密码
pwd = "ZZH_TEST";
init();
//mysid:必须为要连接机器的sid名称,否则会包以下错:
// java.sql.SQLException: Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=169870080)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
//参考连接方式:
// Class.forName( "oracle.jdbc.driver.OracleDriver" );
// cn = DriverManager.getConnection( "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", sUsr, sPwd );
}
public void init()
{
System.out.println("oracle jdbc test");
try{
Class.forName(driver);
System.out.println("driver is ok");
conn = DriverManager.getConnection(url,user,pwd);
System.out.println("conection is ok");
statement = conn.createStatement();
//conn.setAutoCommit(false);
//输入参数
in_price="5.0";
//调用函数
stmt = conn.prepareCall("{? = call F_GET_PRICE(?)}");
// stmt.registerOutParameter(1, java.sql.Types.FLOAT);
// stmt.registerOutParameter(2, java.sql.Types.CHAR);
stmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
stmt.setString(2, in_price);
stmt.executeUpdate();
//取的结果集的方式一:
rs = ((OracleCallableStatement)stmt).getCursor(1);
//取的结果集的方式二:
// rs = (ResultSet) stmt.getObject(1);
String ric ;
String price ;
String updated ;
while(rs.next()){
ric = rs.getString(1);
price = rs.getString(2);
updated = rs.getString(3);
System.out.println("ric:"+ric+";-- price:"+price+"; --"+updated+"; ");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
System.out.println("close ");
}
}
public static void main(String args [])//自己替换[]
{
new JDBCoracle10G_INVOKEFUNCTION();
}
}