一、先在oracle中编写测试过程
1、首选建一个测试表
-- Create table create table BOOK ( BOOKID VARCHAR2(50) not null, BOOKNAME VARCHAR2(50) not null, PUBLISHER VARCHAR2(50) not null, PRICE VARCHAR2(50) null )
2、编写ORACLE测试过程
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;
/
在这里我们建立了一个输入参数,为普通类型,两个输出参数,其中一个为普通VARCHAR2型,另一个为特殊的记录集类型。
(注:在数据库端测试一下这个过程,以确保没有问题,开始下面的操作~:)
二、编写JAVA代码测试过程
/* * 创建日期: 2003-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.p
原文:http://canyang.blog.ccidnet.com/blog-htm-itemid-108482-do-showone-type-blog-uid-38235.html |