下面為您介紹的Java調用Oracle函數方法,共兩種情況,一種調用無入參的Oracle函數,一種則是調用有一個入參,一個輸出參數以及一個字符串返回值的Oracle函數。
調用無入參的函數
函數定義
- 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();