JDBC- PreparedStatement example - Execute INSERT query(DML command) using PreparedStatement's executeUpdate method in java


In this tutorial we will learn how to Execute INSERT query(DML command) using PreparedStatement's executeUpdate method in java JDBC.

JDBC tutorial - What is java.sql.CallableStatement in java


java.sql.PreparedStatement's executeUpdate method can be used for executing INSERT queries and
executeUpdate method returns number of rows inserted



--Before executing java program execute these database scripts  >
create table EMPLOYEE(ID number(4), NAME varchar2(22));
commit;
--If table already exists then execute the DROP command >
drop table EMPLOYEE;



Before execution of below program EMPLOYEE table will look like this >

After execution of below program EMPLOYEE table will look like this >


Example/ Full Programs JDBC- Execute INSERT query using PreparedStatement's executeUpdate method in java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class PreparedStatementInsertExample {
   public static void main(String... arg) {
          Connection con = null;
          PreparedStatement prepStmt = null;
          try {
                 // registering Oracle driver class
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 // getting connection
                 con = DriverManager.getConnection(
                              "jdbc:oracle:thin:@localhost:1521:orcl",
                              "ankit", "Oracle123");
                 System.out.println("Connection established successfully!");
                
                
                 prepStmt = con.prepareStatement("INSERT into EMPLOYEE (ID, NAME) "
                                                                   + "values (?, ?) ");
                
                 //Executing PreparedStatement first time
                 prepStmt.setInt(1, 7); //substitute first occurrence of ? with 7
                 prepStmt.setString(2, "ankit"); //substitute second occurrence of ? with "ankit"
                
                 // execute insert query
                 int numberOfRowsInserted = prepStmt.executeUpdate();
                 System.out.println("numberOfRowsInserted=" + numberOfRowsInserted);
                 //Executing same PreparedStatement second time
                 prepStmt.setInt(1, 8); //substitute first occurrence of ? with 8
                 prepStmt.setString(2, "rohit"); //substitute second occurrence of ? with "rohit"
                
                 // execute insert query
                 numberOfRowsInserted = prepStmt.executeUpdate();
                 System.out.println("numberOfRowsInserted=" + numberOfRowsInserted);
                
          } catch (ClassNotFoundException e) {
                 e.printStackTrace();
          } catch (SQLException e) {
                 e.printStackTrace();
          }
          finally{
                 try {
                       if(prepStmt!=null) prepStmt.close(); //close PreparedStatement
                       if(con!=null) con.close(); // close connection
                 } catch (SQLException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/*OUTPUT
Connection established successfully!
numberOfRowsInserted=1
numberOfRowsInserted=1
*/

Must read : java.sql.PreparedStatement - using executeUpdate  and executeQuery methods - CREATE, SELECT, UPDATE and DELETE in java.


Must Know :

By default connection is auto-commit, so all insert queries will be committed automatically by java connection.
(Must know : insert is a DML(Data Manipulation Language) command, queries are not committed automatically in database)

We may stop connection from doing auto-commits by setting auto-commit to false using > con.setAutoCommit(false);
Well in that we may commit transactions any time using con.commit();

Also, We may set connection back to default behavior of doing auto-commit by using > con.setAutoCommit(true) or simply using con.setAutoCommit();

But what will be benefit of using con.setAutoCommit(false)?
If any transaction goes wrong than we may halt execution further related transactions before calling con.commit() and call con.rollback() in catch block.


So in this tutorial we learned how to Execute INSERT query(DML command) using PreparedStatement's executeUpdate method in java JDBC.

RELATED LINKS>

java.sql.PreparedStatement - using executeUpdate  and executeQuery methods - CREATE, SELECT, INSERT, UPDATE and DELETE in java

JDBC tutorial- Insert/Store/save FILE in database

JDBC example- Batch PreparedStatement example- Execute INSERT query(DML command) using PreparedStatement's addBatch() and executeBatch() methods in java


Execute database STORED PROCEDURE - IN parameter, OUT parameter and IN OUT parameter || call FUNCTION from java

Difference between CLOB and CLOB data type in Oracle


Labels: Core Java JDBC
eEdit
Must read for you :