Two cases of Java calling Oracle Functions

Source: Internet
Author: User
Tags db2 functions

The following describes how to call Oracle functions in Java. In two cases, one is to call an Oracle function without an input parameter, and the other is to call an input parameter, an Oracle function that outputs parameters and returns a string.

Call a function without an input parameter

Function Definition

 
 
  1. CREATE OR REPLACE Function f_getstring Return Varchar2 Is  
  2. Begin  
  3. Return ''String value'';  
  4. End f_getstring; 

Java snippets for calling Functions

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

Calls a function that has an input parameter, an output parameter, and a string return value.

Function Definition

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

Java code snippet for calling a function

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



Edit recommendations]

Implementation of oracle function return table

How to uninstall an Oracle database in Windows

Multiple table Connection Methods in Oracle

Example of using SQL recursive statements in oracle

Comparison Between Common DB2 functions and Oracle Functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.