JDBC batch insert using Java bean class

In this tutorial, you will learn about JDBC batch insert using java bean / model class.

Tutorials   

Ads

Ads

JDBC batch insert using Java bean class

In this tutorial, you will learn about JDBC batch insert using java bean / model class.

EXAMPLE

The database table(employee) in which we will insert batch of records, can be created as follows :

CREATE TABLE 'employee' ( 
	'employee_id' int(10) DEFAULT NULL, 
	'name' varchar(25) DEFAULT NULL, 
	'phone' varchar(25) DEFAULT NULL, 
	'city' varchar(25) DEFAULT NULL, 
	'state' varchar(25) DEFAULT NULL, 
	'country' varchar(25) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 

The bean class or model class 'Employee' is given below :

package jdbcBatchInsert;

import java.util.Iterator;

public class Employee {
private int employee_id;

private String name;

private String phone;

private String city;

private String state;

private String country;

/**
* @param employeeId
* @param name
* @param phone
* @param city
* @param state
* @param country
*/
public Employee(int employeeId, String name, String phone, String city,
String state, String country) {
this.employee_id = employeeId;
this.name = name;
this.phone = phone;
this.city = city;
this.state = state;
this.country = country;
}

public int getEmployee_id() {
return employee_id;
}

public void setEmployee_id(int employeeId) {
employee_id = employeeId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public Iterator<Employee> iterator() {
// TODO Auto-generated method stub
return null;
}

}

The code for batch insert using Java model or bean class is given below :

package jdbcBatchInsert;

import java.sql.*;
import java.util.ArrayList;

import jdbcBatchInsert.Employee;

public class JdbcBatchInsertUsingBeanClass {
public static void main(String args[]) {

String url = "jdbc:mysql://localhost:3306/";
String db = "jdbc_insert";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";

try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url + db, user,
pass);

Employee e1 = new Employee(1201, "Nina", "9595959595", "Chennai",
"TamilNadu", "India");
Employee e2 = new Employee(1202, "Samuel", "9090909090",
"Banglore", "Karnataka", "India");
Employee e3 = new Employee(1203, "Samrat", "9595959595",
"New Delhi", "Delhi", "India");
Employee e4 = new Employee(1204, "Rajju", "9090909090", "Bhopal",
"Madhya Pradesh", "India");
Employee e5 = new Employee(1205, "Fareed", "9595959595", "Indore",
"Madhya Pradesh", "India");
Employee e6 = new Employee(1206, "Rukmesh", "9090909090",
"Ahmdabaad", "Gujraat", "India");
Employee e7 = new Employee(1207, "Rajesh", "9595959595", "Lucknow",
"Uttar Pradesh", "India");
Employee e8 = new Employee(1208, "Jwala", "9090909090", "Agra",
"Uttar Pradesh", "India");
Employee e9 = new Employee(1209, "Raju", "9595959595", "Noida",
"Uttar Pradesh", "India");
Employee e10 = new Employee(1210, "Gaurav", "9090909090",
"Aligarh", "Uttar Pradesh", "India");
Employee e11 = new Employee(1211, "Nitika", "9595959595",
"Kolkata", "West Bengaal", "India");
ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(e1);
employees.add(e2);
employees.add(e3);
employees.add(e4);
employees.add(e5);
employees.add(e6);
employees.add(e7);
employees.add(e8);
employees.add(e9);
employees.add(e10);
employees.add(e11);

String sql = "insert into employee (employee_id,name,phone,city,state,country) values (?, ?, ?, ?, ?, ?)";
PreparedStatement ps = connection.prepareStatement(sql);

int count = 0;
for (Employee employee : employees) {
ps.setInt(1, employee.getEmployee_id());
ps.setString(2, employee.getName());
ps.setString(3, employee.getPhone());
ps.setString(4, employee.getCity());
ps.setString(5, employee.getState());
ps.setString(6, employee.getCountry());
ps.addBatch();
count++;
}
ps.executeBatch();
System.out.println(count + " records inserted successfully");
ps.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

OUTPUT

In console, you will get the following message :


11 records inserted successfully

After batch insertion, you will get the following records in database table "employee" :

Download Source Code

Advertisements

Ads
Share on Google+Share on Google+

JDBC batch insert using Java bean class

Posted on: December 6, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC batch insert using Java bean class  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads