JDBC PreparedStatement - Java @ Desk

Friday, May 31, 2013

JDBC PreparedStatement

JDBC Prepared Statement/PreparedStatement in java is used for execution of SQL statements. If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead.

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

Advantage : The advantage to this is that in most cases, this SQL statement is sent to the DBMS right away, where it is compiled. As a result, the PreparedStatement object contains not just a SQL statement, but a SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first.

Sample Implementation

import java.sql.*;

public class JdbcPreparedStatementImpl {
   public static void main(String[] args) throws Exception{
      //Register a driver

      // Load a database driver
      Connection con = DriverManager.getConnection
      PreparedStatement updateemp = con.prepareStatement
      ("insert into emp values(?,?,?)");
      updateemp.setString(3, "CEO");

      // Execute the SQL statement

No comments:

Post a Comment