❮ Previous Next ❯

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");
}
}
❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved