The statement interface is used to create SQL basic statements in Java it provides methods to execute queries with the database. There are different types of statements that are used in JDBC as follows:
- Create Statement
- Prepared Statement
- Callable Statement
1. Create a Statement: From the connection interface, you can create the object for this interface. It is generally used for general–purpose access to databases and is useful while using static SQL statements at runtime.
Syntax:
Statement statement = connection.createStatement();
Implementation: Once the Statement object is created, there are three ways to execute it.
- boolean execute(String SQL): If the ResultSet object is retrieved, then it returns true else false is returned. Is used to execute SQL DDL statements or for dynamic SQL.
- int executeUpdate(String SQL): Returns number of rows that are affected by the execution of the statement, used when you need a number for INSERT, DELETE or UPDATE statements.
- ResultSet executeQuery(String SQL): Returns a ResultSet object. Used similarly as SELECT is used in SQL.
Example:
Java
import java.sql.*;
class GFG {
public static void main(String[] args)
{
try {
Class.forName( "com.mysql.cj.jdbc.Driver" );
Connection con = DriverManager.getConnection(
Statement statement = con.createStatement();
String sql = "select * from people" ;
ResultSet result = statement.executeQuery(sql);
while (result.next()) {
System.out.println(
"Name: " + result.getString( "name" ));
System.out.println(
"Age:" + result.getString( "age" ));
}
}
catch (SQLException e) {
System.out.println(e);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
|
Output: Name and age are as shown for random inputs

2. Prepared Statement represents a recompiled SQL statement, that can be executed many times. This accepts parameterized SQL queries. In this, “?” is used instead of the parameter, one can pass the parameter dynamically by using the methods of PREPARED STATEMENT at run time.
Illustration:
Considering in the people database if there is a need to INSERT some values, SQL statements such as these are used:
INSERT INTO people VALUES ("Ayan",25);
INSERT INTO people VALUES("Kriya",32);
To do the same in Java, one may use Prepared Statements and set the values in the ? holders, setXXX() of a prepared statement is used as shown:
String query = "INSERT INTO people(name, age)VALUES(?, ?)";
Statement pstmt = con.prepareStatement(query);
pstmt.setString(1,"Ayan");
ptstmt.setInt(2,25);
// where pstmt is an object name
Implementation: Once the PreparedStatement object is created, there are three ways to execute it:
- execute(): This returns a boolean value and executes a static SQL statement that is present in the prepared statement object.
- executeQuery(): Returns a ResultSet from the current prepared statement.
- executeUpdate(): Returns the number of rows affected by the DML statements such as INSERT, DELETE, and more that is present in the current Prepared Statement.
Example:
Java
import java.sql.*;
import java.util.Scanner;
class GFG {
public static void main(String[] args)
{
try {
Class.forName( "com.mysql.cj.jdbc.Driver" );
Scanner sc = new Scanner(System.in);
System.out.println(
"What age do you want to search?? " );
int age = sc.nextInt();
Connection con = DriverManager.getConnection(
PreparedStatement ps = con.prepareStatement(
"select name from world.people where age = ?" );
ps.setInt( 1 , age);
ResultSet result = ps.executeQuery();
while (result.next()) {
System.out.println( "Name : "
+ result.getString( 1 ));
}
}
catch (SQLException e) {
System.out.println(e);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
|
Output:

3. Callable Statement are stored procedures which are a group of statements that we compile in the database for some task, they are beneficial when we are dealing with multiple tables with complex scenario & rather than sending multiple queries to the database, we can send the required data to the stored procedure & lower the logic executed in the database server itself. The Callable Statement interface provided by JDBC API helps in executing stored procedures.
Syntax: To prepare a CallableStatement
CallableStatement cstmt = con.prepareCall("{call Procedure_name(?, ?}");
Implementation: Once the callable statement object is created
- execute() is used to perform the execution of the statement.
Example:
Java
import java.sql.*;
class GFG {
public static void main(String[] args)
{
try {
Class.forName( "com.mysql.cj.jdbc.Driver" );
Connection con = DriverManager.getConnection(
Statement s = con.createStatement();
CallableStatement cs
= con.prepareCall( "{call peopleinfo(?,?)}" );
cs.setString( 1 , "Bob" );
cs.setInt( 2 , 64 );
cs.execute();
ResultSet result
= s.executeQuery( "select * from people" );
while (result.next()) {
System.out.println( "Name : "
+ result.getString( 1 ));
System.out.println( "Age : "
+ result.getInt( 2 ));
}
}
catch (SQLException e) {
System.out.println(e);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
|
Output:
