JDBC Statement Interface

The Statement interface provides methods to execute queries with the database.

Statement Interface methods are:
  1. 1. public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the object of ResultSet.
  2. 2. public int executeUpdate(String sql): is used to execute specified query, it may be create, drop, insert, update, delete etc.
  3. 3. public boolean execute(String sql): is used to execute queries that may return multiple results.
  4. 4. public int[] executeBatch(): is used to execute batch of commands.
Example Program

                      package com.java.session.ninteen;

                      import java.sql.Connection;
                      import java.sql.DriverManager;
                      import java.sql.SQLException;
                      import java.sql.Statement;
                      
                      public class StatementInterfaceExample {
                          public static void main(String[] args) {
                              Connection con = null;
                              Statement stmt = null;
                              try {
                                  Class.forName("com.mysql.cj.jdbc.Driver");
                                  con = DriverManager.getConnection("jdbc:mysql://localhost/emp","root","PASSWORD");
                                  stmt = con.createStatement();
                                  int result = stmt.executeUpdate("delete from emp.employee where id = 11");
                                  System.out.println(result+" records affected");
                              } catch (ClassNotFoundException ce) {
                                  ce.printStackTrace();
                              } catch (SQLException se) {
                                  se.printStackTrace();
                              } finally {
                                  try {
                                      stmt.close();
                                      con.close();
                                  } catch (SQLException se) {
                                      se.printStackTrace();
                                  }
                              }
                          }
                      }