Understanding CallableStatement

CallableStatement is a JDBC interface used to execute stored procedures and stored functions in a database. It extends the PreparedStatement interface and allows Java applications to call database routines that may contain input parameters, output parameters, or both. The prepareCall() method of the Connection interface is used to create a CallableStatement object. Input values are supplied using methods such as setInt() and setString(), while output parameters are registered using registerOutParameter(). After executing the stored procedure, methods such as getInt() and getString() are used to retrieve the output values.

Methods of CallableStatement

Method Signature and Short Description

public void registerOutParameter(int parameterIndex, int sqlType)

Registers an OUT parameter with the specified SQL type.

public void registerOutParameter(String parameterName, int sqlType)

Registers a named OUT parameter with the specified SQL type.

public void setInt(int parameterIndex, int value)

Sets an integer value for the specified input parameter.

public void setString(int parameterIndex, String value)

Sets a String value for the specified input parameter.

public void setDouble(int parameterIndex, double value)

Sets a double value for the specified input parameter.

public void setFloat(int parameterIndex, float value)

Sets a float value for the specified input parameter.

public void setBoolean(int parameterIndex, boolean value)

Sets a boolean value for the specified input parameter.

public void setObject(int parameterIndex, Object value)

Sets an object value for the specified input parameter.

public int getInt(int parameterIndex)

Retrieves the value of an OUT parameter as an integer.

public String getString(int parameterIndex)

Retrieves the value of an OUT parameter as a String.

public double getDouble(int parameterIndex)

Retrieves the value of an OUT parameter as a double.

public Object getObject(int parameterIndex)

Retrieves the value of an OUT parameter as an Object.

public int getInt(String parameterName)

Retrieves a named OUT parameter as an integer.

public String getString(String parameterName)

Retrieves a named OUT parameter as a String.

public Object getObject(String parameterName)

Retrieves a named OUT parameter as an Object.

public boolean execute()

Executes the stored procedure or function associated with the CallableStatement.

public ResultSet getResultSet()

Retrieves the current result as a ResultSet object.

public int executeUpdate()

Executes the callable SQL statement and returns the number of affected rows.

public void setNull(int parameterIndex, int sqlType)

Sets the specified input parameter to SQL NULL.

public void setDate(int parameterIndex, Date value)

Sets a SQL date value for the specified input parameter.

public void setTime(int parameterIndex, Time value)

Sets a SQL time value for the specified input parameter.

public void setTimestamp(int parameterIndex, Timestamp value)

Sets a timestamp value for the specified input parameter.

public void close()

Releases the resources held by the CallableStatement.

Scroll to Top