What is Callable Statement
-
CallableStatement interface is used to call the stored procedures.
-
We can have business logic on the database by the use of stored procedures that will make the performance better as they are precompiled.
-
Three types of parameters exist: IN, OUT, and INOUT. The PreparedStatement object only uses the IN parameter. The CallableStatement object can use all the three.
Sr. |
Parameter |
Description |
1 |
IN |
A parameter whose value is unknown when the SQL statement is created. You bind values to IN parameters with the setXXX() methods. |
2 |
OUT
|
A parameter whose value is supplied by the SQL statement it returns. You retrieve values from the OUT parameters with the getXXX() methods.
|
3 |
INOUT |
A parameter that provides both input and output values. You bind variables with the setXXX() methods and retrieve values with the getXXX() methods.
|
Stored Procedure
(Figure: Stored Procedure)
Program To Use Stored Procedure
- import java.sql.*;
- public class CallableDemo {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn= DriverManager.getConnection
- ("jdbc:mysql://localhost:3306/gtu", "root",“pwd");
-
- CallableStatement cs=conn.prepareCall("{call gettitle(?,?)}");
- cs.setInt(1,1201);
- cs.registerOutParameter(2,Types.VARCHAR);
- cs.execute();
- System.out.println(cs.getString(2));
-
- cs.close();
- conn.close();
- }catch(Exception e){System.out.println(e.toString());}
- }//PSVM
- }//class