Java调用oracle存储过程的示例总结如下。
一、无返回值的存储过程
存储过程为:
create or replace procedure adddept
(deptno number,dname varchar2,loc varchar2)
as
begin
insert into dept values(deptno,dname,loc);
end;
Java调用代码:
public class TestProcedure {
Connection conn=null ;
CallableStatement cstmt=null ;
String url="jdbc:oracle:thin:@localhost:1521:mydb";
String driver="oracle.jdbc.driver.OracleDriver";
String name="";
public TestProcedure() {
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,"scott","tiger");
cstmt=conn.prepareCall("{call adddept(?,?,?)}");
cstmt.setInt(1,13);
cstmt.setString(2,"间谍部2");
cstmt.setString(3,"ningbo2");
cstmt.executeUpdate();
System.out.println("success");
}
catch (Exception e){
e.printStackTrace();
}
finally{
cstmt.close();
conn.close();
}
}
}
注:dept表为oracle数据库方案scott中的一个表
二、有返回值的存储过程(返回非列表类型值)
存储过程为:
create or replace procedure testb
(para1 in varchar2,para2 out varchar2)
as
begin
select into para2 from testtb where i_id= para1;
end;
Java调用代码:
public class TestProcedureTWO {
public TestProcedureTWO() {
}
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";
ResultSet rs = null;
Connection conn = null;
CallableStatement proc = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, " hyq ", " hyq ");
proc = conn.prepareCall("{call HYQ.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(proc!=null){
proc.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (SQLException ex1) {
ex1..printStackTrace();
}
}
}
}
注意,这里的proc.getString(2)中的数值2并非任意的,而是和存储过程中的out列对应的,如果out是在第一个位置,那就是proc.getString(1),如果是第三个位置,就是proc.getString(3),当然也可以同时有多个返回值,那就是再多加几个out参数了。
三、有返回值的存储过程(返回列表类型值)
由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.所以要分两部分:
1.建一个程序包的包头(声明)。如下:
create or replace package testpackage
is
type test_cursor is ref cursor;
end testpackage;
2.建一个程序包的包体(实现)。如下:
create or replace procedure testc
(p_cursor out testpackage.test_cursor)
is
begin
open p_cursor for select * from hyq.testtb;
end testc;
可以看到,它是把游标(可以理解为一个指针),作为一个out 参数来返回值的。
Java调用代码:
import java.sql.*;
import java.io.OutputStream;
import java.io.Writer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import oracle.jdbc.driver.*;
public class TestProcedureTHREE {
public TestProcedureTHREE() {
}
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";
ResultSet rs = null;
Connection conn = null;
CallableStatement proc = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, "hyq", "hyq");
proc = conn.prepareCall("{ call hyq.testc(?) }");
proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);
proc.execute();
rs = (ResultSet)proc.getObject(1);
while(rs.next()){
System.out.println("<tr><td>" + rs.getString(1) + "</td><td>"+rs.getString(2)+"</td></tr>");
}
}
catch (SQLException ex2) {
ex2.printStackTrace();
}
catch (Exception ex1) {
ex1.printStackTrace();
}
finally{
try {
if(rs != null){
rs.close();
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (SQLException ex0) {
}
}
}
}
Java调用存储过程举例
最新推荐文章于 2025-05-22 10:07:53 发布