JDBC Statement - Java @ Desk

Friday, May 31, 2013

JDBC Statement

JDBC Statement is used for execution of SQL statements. Statement interface provides basic method for SELECT, INSERT, UPDATE, DELETE operations in the database.

SQL query execution involves :
1) Parsing of SQL query string
2) Compilation
3) Execute the SQL query



Disadvantage : For each and every incoming SQL query, statement will follow all the three steps. It parse & compiles the SQL query for every request. Hence it is a time consuming process.

Sample implementation:

import java.sql.*;

public class JdbcStatementImpl {
   public static void main(String[] args) throws Exception{
      //Register a driver
      Class.forName("<DRIVER_NAME>");

      // Load a database driver
      Connection con = DriverManager.getConnection
      (<DB_URL>,<USERNAME>,<PASSWORD>);
 
      Statement stmt = con.createStatement();
      String query = ("insert into emp values(2,'name','job');
      con.setAutoCommit(false);

      // Execute the SQL statement
      stmt.executeUpdate(query);
      con.commit();
   }
}    






No comments:

Post a Comment