Statement Interface

  • Represents object that is used for executing a static SQL statement and returning the results it produces
  • Connection.createStement() creates a Statement object for sending SQL statements to the database
  • SQL Statements without parameters are normally executed using Statement Objects
  • In case if same SQL statement is executed many times, it may be more efficient to use PreparedStatement object
  • Statement.execute, Statement.executeUpdate and Statement.executeQuery methods used to execute static SQL statement
  • ResultSet object is returned as result which static SQL statement produces while execution
  • By default, only one ResultSet object per Statement object can be open at the same time
  • All execution methods in the Statement interface implicitly close a current ResultSet object of the statement if an open one exists
  • Reading one ResultSet object long with another, each must have been generate by different Statement objects

Commonly used methods of Statement interface

  • boolean execute(String sql)
    - Executes the given SQL statement, which may return multiple results.
  • ResultSet executeQuery(String sql)
    - Executes the given SQL statement, which returns a single ResultSet object.
  • int executeUpdate(String sql)
    - Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
  • int[] executeBatch()
    - Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.
package com.atuldwivedi.jee.jdbc.basic;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.atuldwivedi.jee.jdbc.util.ReleaseResources;
import com.atuldwivedi.jee.jdbc.util.connection.ConnectionFactory;

 * This class contains very basic example of JDBC program for using {@link Statement} interface,
 * it creates a table, inserts a record and fetch the same.
 * This example uses h2 database as data source.
 * @author Atul Dwivedi 2016
public class LearnStatement {

	public static void main(String[] args) {
		Connection con = null;
		Statement stmt = null;
		String insertIntoLoginTable = "INSERT INTO LOGIN VALUES('Atul', '1234')";
		String selectFromLoginTable = "SELECT * FROM LOGIN";
		int rowUpdatedRowCount = 0;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnecction();
			stmt = con.createStatement();

			rowUpdatedRowCount = stmt.executeUpdate(createLoginTable);
			if(rowUpdatedRowCount >= 0){
				System.out.println("Table created successfully.");
				rowUpdatedRowCount = stmt.executeUpdate(insertIntoLoginTable);
				if(rowUpdatedRowCount >= 0){
					System.out.println("Row iserted successfully.");
					rs = stmt.executeQuery(selectFromLoginTable);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
		} catch (SQLException e) {
			// TODO Auto-generated catch block
Footer with Map