Understanding PreparedStatement Interface

The PreparedStatement interface belongs to the java.sql package and is used to execute parameterized SQL statements. It extends the Statement interface and allows SQL statements to be precompiled by the database. It uses the ? placeholder to represent values that are supplied later. The setXXX() methods are used to assign values to these placeholders. PreparedStatement improves performance when the same SQL statement is executed multiple times and helps protect applications against SQL injection. It is commonly used for INSERT, UPDATE, DELETE, and parameterized SELECT operations.

Methods of PreparedStatement Interface

Methods

public ResultSet executeQuery() throws SQLException

Executes the prepared SELECT statement and returns the result as a ResultSet.

public int executeUpdate() throws SQLException

Executes the prepared INSERT, UPDATE, or DELETE statement and returns the number of affected rows.

public boolean execute() throws SQLException

Executes the prepared SQL statement and returns true if the result is a ResultSet; otherwise, it returns false.

public void setInt(int parameterIndex, int x) throws SQLException

Sets an integer value for the specified parameter position.

public void setString(int parameterIndex, String x) throws SQLException

Sets a String value for the specified parameter position.

public void setDouble(int parameterIndex, double x) throws SQLException

Sets a double value for the specified parameter position.

public void setFloat(int parameterIndex, float x) throws SQLException

Sets a float value for the specified parameter position.

public void setLong(int parameterIndex, long x) throws SQLException

Sets a long value for the specified parameter position.

public void setBoolean(int parameterIndex, boolean x) throws SQLException

Sets a boolean value for the specified parameter position.

public void setDate(int parameterIndex, Date x) throws SQLExceptionS

ets a SQL Date value for the specified parameter position.

public void setTime(int parameterIndex, Time x) throws SQLException

Sets a SQL Time value for the specified parameter position.

public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException

Sets a SQL Timestamp value for the specified parameter position.

public void setNull(int parameterIndex, int sqlType) throws SQLException

Sets a SQL NULL value for the specified parameter.

public void setObject(int parameterIndex, Object x) throws SQLException

Sets a Java object as the value of the specified SQL parameter.

public void clearParameters() throws SQLException

Clears all parameter values that were previously set.

public void addBatch() throws SQLException

Adds the current set of parameter values to the batch of commands.

public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException

Sets a character stream as the value of the specified parameter.

public void setBinaryStream(int parameterIndex, InputStream inputStream) throws SQLException

Sets a binary input stream as the value of the specified parameter.

public void close() throws SQLException

Closes the PreparedStatement and releases its database resources.

Scroll to Top