一、先在oracle中编写测试过程
1、首选建一个测试表
[code]-- Create table
create table BOOK
(
BOOKID VARCHAR2(50) not null,
BOOKNAME VARCHAR2(50) not null,
PUBLISHER VARCHAR2(50) not null,
PRICE VARCHAR2(50) null
)[/code]
2、编写ORACLE测试过程
[code]CREATE OR REPLACE PACKAGE pkg_test
AS
TYPE myrctype IS REF CURSOR;
PROCEDURE get (p_id VARCHAR2, p_str OUT VARCHAR2, p_rc OUT myrctype);
END pkg_test;
/
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE get (p_id VARCHAR2, p_str OUT VARCHAR2,p_rc OUT myrctype)
IS
sqlstr VARCHAR2 (500);
BEGIN
p_str := ''''Hello,may~'''';
IF p_id = ''''all'''' THEN
OPEN p_rc FOR
SELECT *
FROM BOOK;
ELSE
sqlstr :=
''''select *
from BOOK where BOOKID=:w_id'''';
OPEN p_rc FOR sqlstr USING p_id;
END IF;
END get;
END pkg_test;
/[/code]
在这里我们建立了一个输入参数,为普通类型,两个输出参数,其中一个为普通VARCHAR2型,另一个为特殊的记录集类型。
(注:在数据库端测试一下这个过程,以确保没有问题,开始下面的操作~:)
二、编写JAVA代码测试过程
[code]/*
* 创建日期: 2006-8-8
*/
package JDBC;
/**
* 作者:may
* 时间:15:09:23
*/
import java.sql.*;
import oracle.jdbc.driver.*;
public class proctest {
public static void main(String[] args) {
proctest pc = new proctest();
pc.ShowContent();
}
String sDBDriver="oracle.jdbc.driver.OracleDriver";
String sConnStr="jdbc:oracle:thin:@10.3.8.48:1521:ORADB";
Connection connect=null;
ResultSet rs = null;
public proctest(){
try{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
public ResultSet ShowContent()
{
try{
connect = DriverManager.getConnection(sConnStr,"SHUIBJ","SHUIBJ");
CallableStatement stmt = connect.prepareCall("{call PKG_TEST.GET(?,?,?)}");
stmt.setString(1,"all"); //输入参数
stmt.registerOutParameter(2,Types.CHAR); //输出参数为普通参数
stmt.registerOutParameter(3,OracleTypes.CURSOR); //输出参数为结果集参数
stmt.executeQuery();
rs = ((OracleCallableStatement) stmt).getCursor(3); //得到输出结果集参数
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
String str = stmt.getString(2);
System.out.println("第二个参数为:"+str);
System.out.println("结果集列数"+numberOfColumns);
//列出结果集中的记录
ResultSetMetaData md = rs.getMetaData();
int nColumns = md.getColumnCount();
for (int i=1;i<=nColumns;i++){
System.out.print(md.getColumnName(i)+((i==nColumns)?"\n":"\t"));
if(i==2) System.out.print("\t");
}
while (rs.next()){
for(int i=1;i<=nColumns;i++){
System.out.print(rs.getString(i)+((i==nColumns)?"\n":"\t"));
}
}
}
catch(SQLException ex){
System.err.println(ex.getMessage()+"连数据库有问题!");
}
return rs;
}
}[/code]
输出结果为:
第二个参数为:Hello,may~
结果集列数4
[code]BOOKID BOOKNAME PUBLISHER PRICE[/code]