The Statement interface in JDBC is used to execute static SQL queries against a database. It is created using the createStatement() method of the Connection interface. The Statement interface can execute SQL commands such as SELECT, INSERT, UPDATE, and DELETE. It is mainly suitable when the SQL query does not require dynamic parameters.
Common Methods
| Method | Description |
|---|---|
executeQuery() |
Executes a SELECT query and returns a ResultSet. |
executeUpdate() |
Executes INSERT, UPDATE, or DELETE and returns the number of affected rows. |
execute() |
Executes any SQL statement and returns true if the result is a ResultSet. |
close() |
Closes the Statement object. |
Program 1: Executing a SELECT Query using Statement
This program retrieves employee records from a MySQL database using the Statement interface. The executeQuery() method is used to execute the SELECT query and retrieve the results.
import java.sql.*;
public class StatementSelectExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "MySql_password";
try {
Connection con = DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement();
String sql = "SELECT employee_id, employee_name, salary FROM employee";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(
rs.getInt("employee_id") + " " +
rs.getString("employee_name") + " " +
rs.getDouble("salary")
);
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" StatementSelectExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" StatementSelectExample
101 Ravi 45000.0
102 Priya 42000.0
103 Arjun 50000.0Code language: CSS (css)
Program 2: Executing a INSERT Query using Statement
This program inserts a new employee record into the database using the Statement interface. The executeUpdate() method is used because an INSERT query modifies database records.
import java.sql.*;
public class StatementInsertExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "Mysql_password";
try {
Connection con = DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement();
String sql =
"INSERT INTO employee (employee_id, employee_name, salary) " +
"VALUES (4, 'Arun', 38000)";
int rows = stmt.executeUpdate(sql);
System.out.println(rows + " record inserted successfully.");
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" StatementInsertExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" StatementInsertExample
1 record inserted successfully.Code language: CSS (css)
Program 3: Executing a UPDATE Query using Statement
This program updates the salary of an existing employee using the Statement interface. The executeUpdate() method returns the number of records affected by the UPDATE query.
import java.sql.*;
public class StatementUpdateExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "Mysql_password";
try {
Connection con = DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement();
String sql =
"UPDATE employee SET salary = 50000 WHERE employee_id = 101";
int rows = stmt.executeUpdate(sql);
System.out.println(rows + " record updated successfully.");
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" StatementUpdateExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" StatementUpdateExample
1 record updated successfully.Code language: CSS (css)
