Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

JDBC batch insert

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


JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout

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 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 ) - 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.

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 {
  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')");
  String sql = "select * from person";
  rs = st.executeQuery(sql);
  System.out.println("No  \tName");
  while ( {
  System.out.print(rs.getString(1) + " \t");
  catch (Exception e) {


No  	Name
4   	Komal
5   	Ajay
6   	Santosh

Download code


Share on Google+Share on Google+

JDBC batch insert

Posted on: November 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JDBC batch insert   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
November 6, 2011

I had to add con.commit() after st.executeBatch() in order for the code to work.