创建需要的测试表:createtableTest(tidvarchar2(10),tnamevarchar2(10));
第一种情况:无返回值.
createorreplaceproceduretest_a(param1invarchar2,param2invarchar2)as
begin
insertintotestvalue(param1,param2);
end;
- 1.
- 2.
- 3.
- 4.
Java调用代码:
packagecom.test;
importjava.sql.*;
importjava.io.*;
importjava.sql.*;
publicclassTestProcA
{
publicTestProcA(){
}
publicstaticvoidmain(String[]args)
{
ResultSetrs=null;
Connectionconn=null;
CallableStatementproc=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:test","test","test");
proc=conn.prepareCall("{calltest_a(?,?)}");
proc.setString(1,"1001");
proc.setString(2,"TestA");
proc.execute();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(null!=rs){
rs.close();
if(null!=proc){
proc.close();
}
if(null!=conn){
conn.close();
}
}
}catch(Exceptionex){
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
第二种情况:有返回值的存储过程(返回值非列表).
存储过程为:
createorreplaceproceduretest_b(param1invarchar2,param2outvarchar2)
as
begin
selecttnameintoparam2fromtestwheretid=param1;
end;
- 1.
- 2.
- 3.
- 4.
- 5.
Java调用代码:
packagecom.test;
importjava.sql.*;
importjava.io.*;
importjava.sql.*;
publicclassTestProcB
{
publicTestProcB(){
}
publicstaticvoidmain(String[]args)
{
Connectionconn=null;
CallableStatementproc=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:test","test","test");
proc=conn.prepareCall("{calltest_b(?,?)}");
proc.setString(1,"1001");
proc.registerOutParameter(2,Types.VARCHAR);
proc.execute();
System.out.println("Outputis:"+proc.getString(2));
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(null!=proc){
proc.close();
}
if(null!=conn){
conn.close();
}
}catch(Exceptionex){
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
第三种情况:返回列表.
由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.要分两部分来写:
createorreplacepackagetpackageas
typet_cursorisrefcursor;
proceduretest_c(c_refoutt_cursor);
end;
createorreplacepackagebodytpackageas
proceduretest_c(c_refoutt_cursor)is
begin
openc_refforselect*fromtest;
endtest_c;
endtpackage;
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
Java调用代码:
packagecom.test;
importjava.sql.*;
importjava.io.*;
importjava.sql.*;
publicclassTestProcB
{
publicTestProcB(){
}
publicstaticvoidmain(String[]args)
{
Connectionconn=null;
CallableStatementproc=null;
ResultSetrs=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:test","test","test");
proc=conn.prepareCall("{?=calltpackage.test_b(?)}");
proc.registerOutParameter(1,OracleTypes.CURSOR);
proc.execute();
while(rs.next()){
System.out.println(rs.getObject(1)+"\t"+rs.getObject(2));
}
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(null!=rs){
rs.close();
if(null!=proc){
proc.close();
}
if(null!=conn){
conn.close();
}
}
}catch(Exceptionex){
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
Hibernate调用oracle存储过程
this.pnumberManager.getHibernateTemplate().execute(
newHibernateCallback()...{
publicObjectdoInHibernate(Sessionsession)
throwsHibernateException,SQLException...{
CallableStatementcs=session.connection().prepareCall("{callmodifyapppnumber_remain(?)}");
cs.setString(1,foundationid);
cs.execute();
returnnull;
}
});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
【编辑推荐】