一、无返回值的存储过程
测试表:
-- Create table
create table TESTTB
(
ID VARCHAR2(30),
NAME VARCHAR2(30)
)
tablespace BOM
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
例: 存储过程为(当然了,这就先要求要建张表TESTTB,里面两个字段(I_ID,I_NAME)。
):
CREATE OR REPLACE PROCEDURE TESTA(PARA1 IN VARCHAR2, PARA2 IN VARCHAR2) AS
BEGIN
INSERT INTO BOM.TESTTB(ID, NAME) VALUES (PARA1, PARA2);
END TESTA;
在Java里调用时就用下面的代码:
package com.yiming.procedure.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestProcedureDemo1 {
public TestProcedureDemo1() {
}
public static void main(String[] args) {
String driver = "Oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:Oracle:thin:@10.20.30.30:1521:vasms";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
CallableStatement proc = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, "bom", "bom");
proc = conn.prepareCall("{ call BOM.TESTA(?,?) }");
proc.setString(1, "100");
proc.setString(2, "TestOne");
proc.execute();
} catch (SQLException ex2) {
ex2.printStackTrace();
} catch (Exception ex2) {
ex2.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
} catch (SQLException ex1) {
}
}
}
}
二、有返回值的存储过程(非列表)
例:存储过程为:
CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2, PARA2 OUT VARCHAR2) AS
BEGIN
SELECT NAME INTO PARA2 FROM TESTTB WHERE ID = PARA1;
END TESTB;
在Java里调用时就用下面的代码:
package com.yiming.procedure.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
public class TestProcedureDemo2 {
public static void main(String[] args) {
String driver = "Oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:Oracle:thin:@10.20.30.30:1521:vasms";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
CallableStatement proc = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, "bom", "bom");
proc = conn.prepareCall("{ call BOM.TESTB(?,?) }");
proc.setString(1, "100");
proc.registerOutParameter(2, Types.VARCHAR);
proc.execute();
String testPrint = proc.getString(2);
System.out.println("=testPrint=is=" + testPrint);
} catch (SQLException ex2) {
ex2.printStackTrace();
} catch (Exception ex2) {
ex2.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
} catch (SQLException ex1) {
}
}
}
}
注意,这里的proc.getString(2)中的数值2并非任意的,而是和存储过程中的out列对应的,如果out是在第一个位置,那就是proc.getString(1),如果是第三个位置,就是proc.getString(3),当然也可以同时有多个返回值,那就是再多加几个out参数了。
(文章来自网络,如有侵权请来信告知,本站将在第一时间删除。)
总机:(010)-88589926,88589826,88587026 QQ讨论群:243729577 182441349 邮箱:cuug_bj@cuug.com
通信地址:北京市海淀区紫竹院路98号北京化工大学科技园609室(CUUG)邮政编码:100089
中国UNIX用户协会 Copyright 2010 ALL Rights Reserved 北京神脑资讯技术有限公司
京ICP备11008061号 京公网安备110108006275号