JDBC Tutorial - JDBC Statements

After a connection is obtained we can interact with the database with sql statement.

Usually the sql statement is executed by Statement interface returned from Connection.

JDBC Statement is for general-purpose access to your database. It is useful when using static SQL statements.

The Statement interface cannot accept parameters.

Statement object is created using the Connection object's createStatement() method as follows:

Statement stmt = conn.createStatement( );

Methods from Statement

We can use Statement to execute a SQL statement with one of its three execute methods.

  • boolean execute(String SQL)
    executes SQL statements such as create database, create table. It returns true value if a ResultSet object can be retrieved; otherwise, it returns false.
  • int executeUpdate(String SQL)
    executes SQL statements which will affect the data rows, for example, an INSERT, UPDATE, or DELETE statement. It returns the numbers of rows affected by the SQL statement.
  • ResultSet executeQuery(String SQL) executes a SELECT statement and get result back. Returns a ResultSet object.

Close Statement Obeject

We need to close a Statement object to clean up the allocated database resources.

If we close the Connection object first, it will also close the Statement object. 
Statement stmt = null;
try {
   stmt = conn.createStatement( );
   stmt execuate method();
catch (SQLException e) {

finally {