JDBC batch insert

In this Tutorial we want to describe you a code that helps you in understand Jdbc batch insert.

JDBC batch insert

JDBC batch insert

     

JDBC Batch Insert is a set of SQL Statements sent to the database and executed as a single unit. The Statement object performs the batch update.

Understand with Example

The Tutorial depicts you an example from JDBC Batch Insert, An empty array object is linked, whenever a statement object is created. The multiple SQL statements is added to the empty array. This  statements is executed as a batch. The con.setAutoCommit( ) is used to disable the auto commit.The batch contain three SQL INSERT statement added to a batch using the addBatch ( ).The code show you the list of method required to carry out JDBC Batch Insert are as follows - 

  Driver loading -  Once the sql package is imported, the second step is the loading of a driver class that call a forclass.name with driver name passed as argument. Once your driver is loaded you connect your front end java application to the backend database. If there is an exception in try block, the catch block subsequently handle the exception.

  DriverManager.getconnection ( ) : This is used to establish a connection  between  database and respective url.

. con.createStatement ( ) : This is used  to provide you a Sql object. On establishing a connection ,your application can interact with backend  database. An connection object is used to send and execute Sql statement to a backend database.

   st.addbatch( ) : This method is used to add the sql parameter to the batch of command.

   executeQuery ( ) : This method is used to return the value obtained from select command and is stored in result set object.rs.next( ) - This method is used to retrieve the element from a database in successively sequence.

  The print ln print the element from rs.getString ( ),which return you the value of elements from  the database.

JdbcBatchInsert.java

import java.sql.*;
public class JdbcBatchInsert {
  public static void main(String args[]) {
  Connection con = null;
  Statement st = null;
  ResultSet rs = null;
  String url = "jdbc:mysql://localhost:3306/";
  String db = "komal";
  String driver = "com.mysql.jdbc.Driver";
  String user = "root";
  String pass = "root";
  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);// Disables auto-commit.
  st = con.createStatement();
  st.addBatch("INSERT INTO person VALUES('4','Komal')");
  st.addBatch("INSERT INTO person VALUES('5','Ajay')");
  st.addBatch("INSERT INTO person VALUES('6','Santosh')");
  st.executeBatch();
  String sql = "select * from person";
  rs = st.executeQuery(sql);
  System.out.println("No  \tName");
  while (rs.next()) {
  System.out.print(rs.getString(1) + " \t");
  System.out.println(rs.getString(2));
  }
  rs.close();
  st.close();
  con.close();
  catch (Exception e) {
  e.printStackTrace();
  }
  }
}

Output

No  	Name
4   	Komal
5   	Ajay
6   	Santosh

Download code