JDBC Statement | Java @ Desk
Home » » JDBC Statement

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();
   }
}    




0 comments:

Post a Comment