Java connects to the database and calls oracle stored procedures with returned values

Source: Internet
Author: User

How can I call an oracle stored procedure with a returned value when connecting to an oracle database using java? The following describes how to use java to connect to a database to call an oracle stored procedure with returned values.

Oracle stored procedures with returned values are not listed)
The stored procedure is:

 
 
  1. CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2)   AS  
  2. BEGIN   
  3.    SELECT INTO PARA2 FROM TESTTB WHERE I_ID= PARA1;   
  4. END TESTB; 

Use the following code when calling in java:

 
 
  1. package com.hyq.src;  
  2.  
  3. public class TestProcedureTWO {  
  4.    public TestProcedureTWO() {  
  5.    }  
  6.    public static void main(String[] args ){  
  7.      String driver = "oracle.jdbc.driver.OracleDriver";  
  8.      String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";  
  9.      Statement stmt = null;  
  10.      ResultSet rs = null;  
  11.      Connection conn = null;  
  12.      try {  
  13.        Class.forName(driver);  
  14.        conn =   DriverManager.getConnection(strUrl, " hyq ", " hyq ");  
  15.        CallableStatement proc = null;  
  16.        proc = conn.prepareCall("{ call HYQ.TESTB(?,?) }");  
  17.        proc.setString(1, "100");  
  18.        proc.registerOutParameter(2, Types.VARCHAR);  
  19.        proc.execute();  
  20.        String testPrint = proc.getString(2);  
  21.        System.out.println("=testPrint=is="+testPrint);  
  22.      }  
  23.      catch (SQLException ex2) {  
  24.        ex2.printStackTrace();  
  25.      }  
  26.      catch (Exception ex2) {  
  27.        ex2.printStackTrace();  
  28.      }  
  29.      finally{  
  30.        try {  
  31.          if(rs != null){  
  32.            rs.close();  
  33.            if(stmt!=null){  
  34.              stmt.close();  
  35.            }  
  36.            if(conn!=null){  
  37.              conn.close();  
  38.            }  
  39.          }  
  40.        }  
  41.        catch (SQLException ex1) {  
  42.        }  
  43.      }  
  44.    }  
  45. }  
  46.  
  47. }  

Note that the proc. the value 2 in getString (2) is not arbitrary, but corresponds to the out column in the stored procedure. If the out column is in the first position, it is proc. getString (1). If it is the third position, it is proc. getString (3), of course, you can also have multiple return values at the same time, that is, add a few more out parameters.

The preceding describes how to call oracle stored procedures with returned values when connecting to the database.

Oracle RMAN backup Optimization

Oracle backup using RMAN

Oracle EXP/IMP backup Overview

Importance of Oracle Database Backup

Oracle control file recovery

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.