Friday, 17 May 2013

Call Stored Procedure In Jdbc

CallableStatement – used for executing stored procedures on the database. The interface used to execute SQL stored procedures. The JDBC API provides a stored procedure SQL escape syntax that allows stored procedures to be called in a standard way for all RDBMS.


A CallableStatement can return one ResultSet object or multiple ResultSet objects.

public interface CallableStatement
extends PreparedStatement

following is syntax to call procedure :-

CallableStatement cs=null;

            cs=conn.prepareCall("{call showName(?)}"); // Pass your procedure Name

            cs.setInt("userid_", userId); // Set run time parameter 
            cs.execute();
            ResultSet rs = cs.getResultSet();

while(rs.next){
System.out.println(rs.getString("Name")); // Name Column Name in Table
}