JDBC batch insert using Java bean class

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

JDBC batch insert using Java bean class

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

Tutorials

  1. Assertion in java
  2. Anonymous Inner Classes - Anonymous Inner Classes tutorial
  3. Appending Strings - Java Tutorials
  4. Assertion in Java
  5. Autoboxing unboxing in Java - Java Tutorials
  6. Thread Deadlocks - Java Tutorials
  7. BASIC Java - Java Tutorials
  8. Interthread Communication in Java
  9. boolean comparisons - tutorial
  10. Catching Exceptions in GUI Code - Java Tutorials
  11. Exception in Java - Java Tutorials
  12. Causing Deadlocks in Swing Code
  13. Class names don't identify a class - Java Tutorials
  14. Commenting out your code - Java Tutorials
  15. Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java
  16. Disassembling Java Classes - Java Tutorials
  17. Double-checked locking,java tutorials,java tutorial
  18. Exceptional Constructors - Java Tutorials
  19. Final Methods - Java Tutorials
  20. garbage collection in java
  21. Java - JDK Tutorials
  22. J2EE Singleton Pattern - Design Pattern Tutorials
  23. Java Comments - Java Tutorials
  24. Java Field Initialisation - Java Tutorials
  25. Java HashSet  - Java Tutorials
  26. Java Multi Dimensions Array - Java Tutorials
  27. java awt package tutorial
  28. Java GC
  29. Java HashMap - Java Tutorials
  30. JDK 1.4 the NullPointerException - Java Tutorials
  31. HashMap and HashCode
  32. LinkedHashMap - Java Tutorials
  33. Which is Faster - LinkedList or ArrayList?
  34. Making Enumerations Iterable - JDK 5 Example
  35. Making Exceptions Unchecked - java tutorial,java tutorials
  36. Creation Time Comparison of Multi Dimensional Array- Java Tutorials
  37. Multicasting in Java - java tutorials,tutorial
  38. Non-virtual Methods in Java - java tutorials
  39. Orientating Components Right to Left,java newsletter,java,tutorial
  40. The link to the outer class,java tutorial,java tutorials