What is Statement in JDBC?


The Statement interface represents the static SQL statement, it is used to create and execute general purpose SQL statements using Java programs.

Creating a statement

You can create an object of this interface using the createStatement() method of the connection interface. Create a statement by invoking this method as shown below.

Statement stmt = null;
try {
   stmt = conn.createStatement( );
   . . .
}
catch (SQLException e) {
   . . .
}
finally {
   . . .
}

Executing the Statement object

Once you have created the statement object you can execute it using one of the execute methods namely, execute(), executeUpdate() and, executeQuery().

  • execute(): This method is used to execute SQL DDL statements, it returns a boolean value specifying weather the ResultSet object can be retrieved.

  • executeUpdate(): This method is used to execute statements such as insert, update, delete. It returns an integer value representing the number of rows affected.

  • executeQuery(): This method is used to execute statements that returns tabular data (example SELECT statement). It returns an object of the class ResultSet.

Example

Following JDBC application demonstrates how to create and, execute a statement.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class StatementExample {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());

      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/testdb";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");

      //Creating the Statement
      Statement stmt = con.createStatement();

      //Executing the statement
      String createTable = "CREATE TABLE Employee(“
         + "Name VARCHAR(255), "
         + "Salary INT NOT NULL, "
         + "Location VARCHAR(255))";
      boolean bool = stmt.execute(createTable);

      String insertData = "INSERT INTO Employee("
         + "Name, Salary, Location) VALUES "
         + "('Amit', 30000, 'Hyderabad'), "
         + "('Kalyan', 40000, 'Vishakhapatnam'), "
         + "('Renuka', 50000, 'Delhi'), "
         + "('Archana', 15000, 'Mumbai')";
      int i = stmt.executeUpdate(insertData);
      System.out.println("Rows inserted: "+i);
      ResultSet rs = stmt.executeQuery("Select *from Employee");

      while(rs.next()) {
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Salary: "+rs.getInt("Salary")+", ");
         System.out.print("City: "+rs.getString("Location"));
         System.out.println();
      }
   }
}

Output

Connection established......
Rows inserted: 4
Name: Amit, Salary: 30000, City: Hyderabad
Name: Kalyan, Salary: 40000, City: Vishakhapatnam
Name: Renuka, Salary: 50000, City: Delhi
Name: Archana, Salary: 15000, City: Mumbai

Updated on: 30-Jul-2019

490 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements