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

JDBC CallableStatement Interface

CallableStatement is an inbuilt interface present in java.sql package. It is used to call stored procedure.

Let’s see an example for better understanding:

Now I have taken a product table like;
create table product
(
id varchar2(200),
name varchar2(200)
)
Now I will create a stored procedure named as insertProcedure like
create or replace procedure insertProcedure
(
id in varchar2,
name in varchar2
)
is
begin
insert into product values(id,name);
end
/

Now I will develop JDBC program:

JdbcExample5.java
packagecom.silan;
importjava.sql.*;

publicclass JdbcExample5 {

    publicstaticvoidmain(String[] args) throws Exception{
        
        //To Load and Register the Driver
        Class.forName("oracle.jdbc.driver.OracleDriver");
                
        //Establish the connection
        Connectioncon=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
                
            //Create the statement object
            CallableStatementcs=con.prepareCall("{call insertProcedure(?,?)}");
            cs.setInt(1, 102);
            cs.setString(2, "Fair & Lovely");
                
            //Execute the sql statement
            cs.execute();
                
            System.out.println("record inserted successfully");
                
            //Close the connection
            con.close();
    }

}

Output:
img

Now we will go to the database environment and check the product table by executing following sql statement:

select * from product

img

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