Java Call stored procedure (with return value)

Source: Internet
Author: User

1: Create a stored procedure

This stored procedure returns a parameter (address) through the passed in parameter (Name).

Create or Replace procedure Demo_procedure (Namedemo in Varchar2,addressdemo out varchar2)
As
Begin
Select Address into Addressdemo from System.demo where Name=namedemo;
End

2:java Code:

Importjava.sql.CallableStatement; Importjava.sql.Connection; ImportJava.sql.DriverManager; ImportJava.sql.ResultSet; Importjava.sql.SQLException; Importjava.sql.Statement; Importjava.sql.Types;  Public classTestproceduretwo { PublicTestproceduretwo () {} Public Static voidMain (string[] args) {String driver= "Oracle.jdbc.driver.OracleDriver"; String strURL= "Jdbc:oracle:thin: @localhost: 1521:myoracle"; Statement stmt=NULL; ResultSet RS=NULL; Connection Conn=NULL; Try{class.forname (driver); Conn= Drivermanager.getconnection (strURL, "System", "admin"); callablestatement proc=NULL; Proc= Conn.preparecall ("{Call Dem_procedure (?,?)}"); Proc.setstring (1, "Kalision"); Proc.registeroutparameter (2, Types.varchar);        Proc.execute (); String Testprint= Proc.getstring (2); System.out.println ("The value returned by the stored procedure is:" +testprint); }      Catch(SQLException ex2) {ex2.printstacktrace (); }      Catch(Exception ex2) {ex2.printstacktrace (); }      finally{        Try {          if(rs! =NULL) {rs.close (); if(stmt!=NULL) {stmt.close (); }            if(conn!=NULL) {conn.close (); }          }        }        Catch(SQLException ex1) {}}} }  

Note: Remember to import the JDBC-driven jar package.

Java Call stored procedure (with return value)

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.