随笔 - 6  文章 - 129  trackbacks - 0
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 822229
  • 排名 - 49

最新评论

阅读排行榜

评论排行榜

 

调用无入参的函数

函数定义

CREATE OR REPLACE Function f_getstring Return Varchar2 Is
Begin
 Return ''String value'';
End f_getstring;

调用函数的Java片断

   CallableStatement cstmt = con.prepareCall("{?=call f_getstring}");
   cstmt.registerOutParameter(1, Types.VARCHAR);
   cstmt.execute();
   String strValue = cstmt.getString(1);
   System.out.println("The return value is:" + strValue);
   cstmt.close();

调用有一个入参,一个输出参数以及一个字符串返回值的函数

函数定义

CREATE OR REPLACE Function f_Getinfo(Id Integer, Age Out Integer) Return Varchar2 Is
Begin
 Age := 10;
 Return ''The age is:'' || Id;
End f_Getinfo;

调用函数的Java代码片断

   CallableStatement cstmt = con
     .prepareCall("{?=call f_getinfo(?,?)}");
   cstmt.registerOutParameter(1, Types.VARCHAR);
   cstmt.setInt(2, 11);
   cstmt.registerOutParameter(3, Types.INTEGER);
   cstmt.execute();
   String strValue = cstmt.getString(1);
   int age = cstmt.getInt(3);
   System.out.println("The return value is:" + strValue
     + " and age is:" + age);
   cstmt.close();



posted on 2009-05-12 10:27 Ke 阅读(6361) 评论(2)  编辑  收藏 所属分类: oraclejava

FeedBack:
# re: Java调用Oracle函数 2013-05-12 15:02 wms
package com;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

public class TestProcedureOne {

public TestProcedureOne() {

}

public static void main(String[] args) {

String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn = null;
CallableStatement cstmt = null;

try {

Class.forName(driver);
conn = DriverManager.getConnection(strUrl, "wms", "wms");

cstmt = conn.prepareCall("{?=call add_three_numbers(?,?,?)}");

cstmt.registerOutParameter(1, Types.INTEGER);

cstmt.setInt(2,4);
cstmt.setInt(3,5);
cstmt.setInt(4,6);

cstmt.execute();
System.out.println("调用函数add_three_numbers:"+cstmt.getInt(1));

} catch (SQLException ex2) {
ex2.printStackTrace();
} catch (Exception ex2) {
ex2.printStackTrace();
}

finally {

try {

if (cstmt != null) {
cstmt.close();
if (conn != null) {
conn.close();
}
}
} catch (SQLException e) {
System.out.println("SQL state" + e.getSQLState());
System.out.println("错误消息" + e.getMessage());
System.out.println("错误代码" + e.getErrorCode());
e.printStackTrace();
}

}

}

}
  回复  更多评论
  
# re: Java调用Oracle函数 2013-05-12 15:04 wms
补充:上例使用的函数(oracle 11g中编写)
代码:

create or replace FUNCTION add_three_numbers ( a NUMBER:=0, b NUMBER:=0, c NUMBER:=0 )
RETURN NUMBER
AS
Result1 NUMBER:=0;
BEGIN
Result1:=(a+b+c);
RETURN Result1;
END add_three_numbers;

  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: