Working with JDBC Statement
- Wednesday, October 12, 2011, 18:29
- JDBC Oracle, JDBC Tutorial
- 1 comment
The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enable you to send commands and receive data from your database. They also define methods that help bridge data type differences between Java and SQL data types used in a database.
Although they are used for different tasks, the three statement interfaces have a lot of similarities. Examine below Figure.

Notice that Statement is the parent, that PreparedStatement extends Statement, and that CallableStatement extends PreparedStatement. Driver vendors provide classes that implement these interfaces. Without a JDBC driver you cannot create objects based on these interfaces.
Introducing Statement Object: The Statement object provides you with basic database−interaction capabilities. However, it still gives you significant capabilities. It enables you to use all types of DML, DDL and other database specific commands. In addition, it supports batch updating, which enables you to implement transaction management.
Creating Statement Object: Instantiating a Statement object is a straightforward procedure. You create the object from a valid Connection object, as shown in the following example:
Connection conn = DriverManager.getConnection(url, "scott", "tiger"); Statement stmt = conn.createStatement();
Using Statement Object: A Statement object provides three methods — execute(), executeUpdate(), and executeQuery() that act as conduits to your database for sending database commands and retrieving results.
Let’s now look at an example to see the use of Statement Object using executeUpdate().
import java.sql.*; public class JDBCStatementDemo { public static void main(String s[]) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection con= DriverManager.getConnection ( "jdbc:oracle:thin:@mysys:1521:khan","scott","tiger"); String query="insert into department "+ " values (1001,'PRODUCTION','123,Metro Tower ,Pune')"; //Step1: Get PreparedStatement Statement stmt=con.createStatement(); int update = stmt.executeUpdate(query); System.out.println("No. of Record Inserted : "+update); con.close(); }//main }//class


One Comment on “Working with JDBC Statement”
Trackbacks