JDBC batch insert using Java bean class

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


 

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
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

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

Ads
 

Discuss: JDBC batch insert using Java bean class  

Post your Comment


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

Ads

 

Ads