本文主要是介绍Java调用Oracle存储过程一,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、通过PL/SQL工具连接上Oracle数据库,创建表zx_test_procedure
二、创建存储过程
①无返回值的存储过程
存储过程为:
create or replace procedure testa
(para1 in varchar2,para2 in varchar2) as
begin
insert into zx_test_procedure(i_id,i_name) values(para1,para2);
end testa;
Java调用的代码:
import java.sql.*;public class TestProcedureOne {public TestProcedureOne() {}public static void main(String[] args ){String driver = "oracle.jdbc.driver.OracleDriver";String strUrl = "jdbc:oracle:thin:@10.10.1.29:1521:testdb";Statement stmt = null;ResultSet rs = null;Connection conn = null;//CallableStatement cstmt = null;try {Class.forName(driver);conn = DriverManager.getConnection(strUrl, "neb", "testneb");CallableStatement proc = null; //创建执行存储过程的对象proc = conn.prepareCall("{call TESTA(?,?) }"); //设置存储过程 call为关键字.proc.setString(1, "400"); //设置第一个输入参数proc.setString(2, "TestFour");//设置第二个输入参数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 i_name INTO PARA2 FROM zx_test_procedure WHERE I_ID= PARA1;
END TESTB;
Java调用代码如下:
import java.sql.*;public class TestProcedureTwo {public TestProcedureTwo() {}public static void main(String[] args ){String driver = "oracle.jdbc.driver.OracleDriver";String strUrl = "jdbc:oracle:thin:@10.10.1.29:1521:testdb";Statement stmt = null;ResultSet rs = null;Connection conn = null;//CallableStatement cstmt = null;try {Class.forName(driver);conn = DriverManager.getConnection(strUrl, "neb", "testneb");CallableStatement proc = null; //创建执行存储过程的对象proc = conn.prepareCall("{call TESTB(?,?) }"); //设置存储过程 call为关键字.proc.setString(1, "300"); //设置第一个输入参数proc.registerOutParameter(2, Types.VARCHAR); //第二个参数输出参数,是varchar类型的proc.execute();//执行String test = proc.getString(2);//获得输出参数System.out.println(test);}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) {}}}
}
结果如下:
TestThree
注意,这里的proc.getString(2)中的数值2并非任意的,而是和存储过程中的out列对应的,如果out是在第一个位置,那就是proc.getString(1),如果是第三个位置,就是proc.getString(3),当然也可以同时有多个返回值,那就是再多加几个out参数了。
③返回列表
type test_cursor is ref cursor;
end testpackage;
begin
open p_cursor for select * from zx_test_procedure;
end testc;
import java.sql.*;public class TestProcedureThree {public TestProcedureThree() {}public static void main(String[] args ){String driver = "oracle.jdbc.driver.OracleDriver";String strUrl = "jdbc:oracle:thin:@10.10.1.29:1521:testdb";Statement stmt = null;ResultSet rs = null;Connection conn = null;//CallableStatement cstmt = null;try {Class.forName(driver);conn = DriverManager.getConnection(strUrl, "neb", "testneb");CallableStatement proc = null; //创建执行存储过程的对象proc = conn.prepareCall("{call TESTC(?) }"); //设置存储过程 call为关键字.//设置输出参数是一个游标.第一个参数,游标类型proc.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);proc.execute();rs = (ResultSet) proc.getObject(1);while(rs.next()) {System.out.println(rs.getString(1) + ",,," + rs.getString(2));}}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) {}}}
}
结果如下:
200,,,TestTwo
300,,,TestThree
400,,,TestFour
参考资料:
SELECT INTO STATEMENT
将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条
记录,否则抛出异常(如果没有记录抛出NO_DATA_FOUND)
例子:
BEGIN
SELECT col1,col2 into 变量1,变量2 FROM typestruct where xxx;
EXCEPTION
WHEN NO_DATA_FOUND THEN
xxxx;
END;
...
关于oracle存储过程的若干问题备忘
select a.appname from appinfo as a; -- 错误
select af.keynode from APPFOUNDATION af where af.appid =aid and af.foundationid =fid; -- 没有into,编译报错,提示:Compilation
Error: PLS - 00428: an INTO clause is expected in this SELECT statement
select af.keynode into kn from APPFOUNDATION af where af.appid = appid and af.foundationid = foundationid; -- 运行阶段报错,提示
ORA - 01422:exact fetch returns more than requested number of rows
id varchar2( 50) primary key not null,
vcount number( 8) not null,
bid varchar2( 50) not null -- 外键
);
fcount:=0;
end if;
new HibernateCallback() ... {
public Object doInHibernate(Session session)
throws HibernateException, SQLException ...{
CallableStatement cs = session
.connection()
.prepareCall("{call modifyapppnumber_remain(?)}");
cs.setString(1, foundationid);
cs.execute();
return null;
}
});
这篇关于Java调用Oracle存储过程一的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!