Understanding Statement Interface

The Statement interface belongs to the java.sql package and is used to execute SQL statements from a Java application. A Statement object is created using the createStatement() method of the Connection interface. It can execute SQL commands such as SELECT, INSERT, UPDATE, and DELETE. The executeQuery() method is generally used for SELECT statements, while executeUpdate() is used for INSERT, UPDATE, and DELETE operations. The execute() method can be used when the type of SQL statement or its result is not known in advance. After completing database operations, the Statement should be closed to release resources.

Methods of Statement Interface

Methods

public ResultSet executeQuery(String sql) throws SQLException

Executes a SELECT SQL statement and returns the resulting data as a ResultSet.

public int executeUpdate(String sql) throws SQLException

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

public boolean execute(String sql) throws SQLException

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

public void close() throws SQLException

Closes the Statement object and releases the database resources associated with it.

public int getMaxRows() throws SQLException

Returns the maximum number of rows that a ResultSet can contain.

public void setMaxRows(int max) throws SQLException

Sets the maximum number of rows that a ResultSet can contain.

public int getQueryTimeout() throws SQLException

Returns the maximum time in seconds allowed for executing a SQL statement.

public void setQueryTimeout(int seconds) throws SQLException

Sets the maximum time in seconds allowed for executing a SQL statement.

public void cancel() throws SQLException

Cancels the current SQL statement if the JDBC driver supports cancellation.

public void clearWarnings() throws SQLException

Clears all warnings associated with the Statement object.

public SQLWarning getWarnings() throws SQLException

Returns the first warning reported by the Statement object.

public Connection getConnection() throws SQLException

Returns the Connection object that created the Statement.

public boolean isClosed() throws SQLException

Checks whether the Statement object has been closed.

public void addBatch(String sql) throws SQLException

Adds an SQL statement to the current batch of commands.

public void clearBatch() throws SQLException

Removes all SQL statements currently stored in the batch.

public int[] executeBatch() throws SQLException

Executes all SQL statements stored in the batch and returns an array containing the update counts.

public ResultSet getResultSet() throws SQLExceptionR

Retrieves the current result as a ResultSet object.

public int getUpdateCount() throws SQLException

Returns the current result as an update count, or -1 if the result is a ResultSet or there is no result.

public boolean getMoreResults() throws SQLException

Moves to the next result produced by the statement.

Scroll to Top