java云存储代码 java云服务

java如何调用存储过程 代码如下 , 最好能解释下相关函数的作用,不胜感激这是我以前的学习笔记java云存储代码,LZ凑合着看看吧,应该能看懂一些吧
===================================================
55 java跟oracle 调用(存储过程 , 函数等)
55.1 Java调用无参的函数
1:函数为:
create or replace function MyF1 return varchar2 is
Result varchar2(20);
begin
dbms_output.put_line('now in My F1');
Result := 'Now MyF1 return';
return(Result);
end MyF1;
2:Java程序
/**
* 演示调用有一个没有参数的函数
* @throws Exception
*/
private static void t1() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
try {
CallableStatement stmt = conn
.prepareCall("{?=call MyF1()}");
stmt.registerOutParameter(1, Types.VARCHAR);
stmt.execute();
System.out.println(stmt.getString(1));
} finally {
conn.close();
}
}
55.2 Java调用无参但有返回值的存储过程
1:存储过程
create or replace procedure MyP1(str outVarchar2) is
begin
dbms_output.put_line('Hello Procedure.');
str :='Haha,Hello Procedure';
end MyP1;
2:程序
/**
* 如何调用无参但有返回值的存储过程 测试的存储过程
* @throws Exception
*/
private static void t2() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
try {
CallableStatement stmt = conn.prepareCall("{call MyP1(?)}");
// 注意 , 这里的stmt.getInt(1)中的数值1并非任意的,而是和存储过程中的out列对应的,
// 如果out是在第一个位置,那就是 stmt.getInt(1),如果是第三个位置,就是getInt.getInt(3),
// 当然也可以同时有多个返回值,那就是再多加几个out 参数了 。
stmt.registerOutParameter(1, Types.VARCHAR);
stmt.execute();
System.out.println(stmt.getString(1));
} finally {
conn.close();
}
}
55.3 Java调用有参的(传入)函数
1:函数
create or replace function MyF2(a number,b varchar2) return varchar2 is
Result varchar2(50);
begin
dbms_output.put_line('a==='||a||',b=='||b);
Result := a||b;
return(Result);
end MyF2;
2:程序
/**
* 调用有参的函数
* @throws Exception
*/
private static void t3() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
try {
CallableStatement stmt = conn
.prepareCall("{?=call MyF2(?,?)}");
stmt.setInt(2, 15);
stmt.setString(3, "HelloF2");
stmt.registerOutParameter(1, Types.VARCHAR);
stmt.execute();
System.out.println(stmt.getString(1));
} finally {
conn.close();
}
}
55.4 Java调用有参的(传入传出)存储过程
1:存储过程
create or replace procedure MyP2(a in number,b in varchar2,c out varchar2) is
begin
dbms_output.put_line('a=='||a||',b=='||b);
c := 'ret=='||a||',b=='||b;
end MyP2;
2:程序
/**
* 调用有参数和返回值的存储过程
* @throws Exception
*/
private static void t4() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
try {
CallableStatement stmt = conn.prepareCall("{call MyP2(?,?,?)}");

推荐阅读