Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

How to call the stored procedure using JDBC

Stored Procedures are generally a group of SQL statements that allows you to make a single call to a database. The SQL statements in a stored procedure are executed statically for better performance. A stored procedure contains input, output, or both the input and output parameters. A stored procedure returns a value through the OUT parameter after executing the SQL statements. A stored procedure can return multiple ResultSets.

Let's see an example to execute a stored procedure with IN parameters:

To call the stored procedure, you need to create it in the database. Here, we are assuming that stored procedure looks like this.

1.create or replace procedure INSERTDEMO
2. (
3. id IN NUMBER,
4. name IN VARCHAR2
5. )
6. is
7. begin
8. insert into user values(id, name);
9. end;
10. /
11. Procedure created.

The table structure is given below:

1.create table user(id number(15), name varchar2(45));

In this example, we are going to call the stored procedure INSERTDEMO that receives id and name as the parameter and inserts it into the table user. Note that you need to create the user table as well to run this application.

import java.sql.*; 
public class StoredProcedureDemo
{  
    public static void main(String[] args) throws Exception
    {        
        Class.forName("oracle.jdbc.driver.OracleDriver");   
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");      
        CallableStatement stmt=con.prepareCall("{call INSERTDEMO(?,?)}");        
        stmt.setInt(1,101);     
        stmt.setString(2,"Tapaswini");    
        stmt.execute();    
        System.out.println("success");
            
    }
}

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc







 PreviousNext