CodesJava

Easy learning with example program codes

JDBC Statement batch update example


The JDBC statement is used to execute queries against the database. Let us study JDBC Statement by batch update example.

Example:

JDBCTest.java

import java.sql.Connection;
import java.sql.Statement;
import com.codesjava.util.JDBCUtil;
 
/**
 * This class is used to batch update in DB table.
 * @author codesjava
 */
public class JDBCTest {
	public static void main(String args[]){
		Connection conn = null;
		Statement statement = null;
 
		String query1 = "insert into EMPLOYEE " +
				"(EMPLOYEE_ID, NAME, SALARY) " +
				"values (1, 'Harish Yadav', 50000)";
 
		String query2 = "insert into EMPLOYEE " +
				"(EMPLOYEE_ID, NAME, SALARY) " +
				"values (5, 'Abhishek Rathor', 50000)";
 
		try{			
			//get connection
			conn = JDBCUtil.getConnection();
 
			//create statement
			statement = conn.createStatement();
 
			//set auto commit to false
			conn.setAutoCommit(false);
 
			//add queries to batch
			statement.addBatch(query1);
			statement.addBatch(query2);
 
			//execute batch
			statement.executeBatch();
 
			//commit
			conn.commit();
 
			//close connection
			statement.close();
			conn.close();
 
		     System.out.println("Records inserted successfully.");
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

JDBCUtil.java

import java.sql.Connection;
import java.sql.DriverManager;
 
/**
 * This is a utility class for JDBC connection.
 * @author codesjava
 */
public class JDBCUtil {
	//JDBC and database properties.
	private static final String DB_DRIVER = 
		           "oracle.jdbc.driver.OracleDriver";
	private static final String DB_URL = 
		        "jdbc:oracle:thin:@localhost:1521:XE";
	private static final String DB_USERNAME = "system";
	private static final String DB_PASSWORD = "oracle";
 
	public static Connection getConnection(){
		Connection conn = null;
		try{
			//Register the JDBC driver
			Class.forName(DB_DRIVER);
 
			//Open the connection
			conn = DriverManager.
			getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
 
			if(conn != null){
			   System.out.println("Successfully connected.");
			}else{
			   System.out.println("Failed to connect.");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}	
}

Output:

Successfully connected.
Record inserted successfully.

Download this example.
 
Next Topic: JDBC PreparedStatement interface.
Previous Topic: JDBC Statement deletes a record example.

Related Topics:

JDBC Statement creates a table example.
JDBC Statement inserts a record example.
JDBC Statement updates a record example.
JDBC Statement select records example.
JDBC Statement deletes a record example.
JDBC Statement batch update example.

 

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki