When you want to send a bunch of SQL queries to database in one go(one call) to execute that’s what batch concept comes.
This reduces network traffic and improves performance of your application
In this post we will learn about JDBC batch update with java.sql.Statement.
As you know java.sql.Statement always deals with Literal or hard coded SQL.
So here we have some hard coded SQL queries in ClientTest.java and all these SQL queries will execute in single database call using Jdbc batch. I mean to say we are not going to make separate database call for every SQL query.
DB Script to create employee_table(database schema jdbcdb)
1 2 3 4 5 6 7 8 9 |
CREATE TABLE `employee_table` ( `employee_id` int(11) NOT NULL auto_increment, `employee_name` varchar(60) NOT NULL, `email` varchar(45) NOT NULL, `salary` double default NULL, `date_of_joining` datetime default NULL, `bonus` decimal(10,0) default NULL, PRIMARY KEY (`employee_id`) ) ; |
DBUtil.java class which is responsible to connect with MySQL database.
this util class uses getConnection() method of java.sql.DriverManager class
public static Connection getConnection(String url,String user, String password) throws SQLException
This method takes three parameters database URL,username and password.
Here database username =”root”,password = “root” what password had supplied during MYSQL database installation time and finally database URL =“jdbc:mysql://localhost:3306/jdbcdb” Where jdbc is the API, mysql is the database, localhost is the server name on which mysql database server is running, we may also use IP address instead machine name, 3306 is the port number and jdbcdb(Make you you have created this schema in MySQL database) is the database name. You may use any database name, in that case, You need to replace the jdbcdb with your database name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.infotech.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { private static final String DB_DRIVER_CLASS="com.mysql.jdbc.Driver"; private static final String DB_USERNAME="root"; private static final String DB_PASSWORD="root"; private static final String DB_URL ="jdbc:mysql://localhost:3306/jdbcdb"; private static Connection connection = null; static{ try { Class.forName(DB_DRIVER_CLASS); connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } public static Connection getConnection(){ return connection; } } |
Client program
In this client program we have three SQL queries which we have added in jdbc statement object by calling addBatch(String sqlQuery) method and finally when we call executeBatch() method all these SQL queries send to database in one go.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.infotech.client; import java.sql.Connection; import java.sql.Statement; import com.infotech.util.DBUtil; public class ClientTest { public static void main(String[] args) { try(Connection connection = DBUtil.getConnection();Statement statement = connection.createStatement()) { String SQLINSERT = "INSERT INTO employee_table(employee_name,email,salary,date_of_joining,bonus) " String DELETESQL="DELETE FROM employee_table WHERE employee_id=20"; statement.addBatch(SQLINSERT); statement.addBatch(UPDATESQL); statement.addBatch(DELETESQL); int[] executeBatch = statement.executeBatch(); for (int i : executeBatch) { System.out.println(i); } } catch (Exception e) { e.printStackTrace(); } } } |
After running client program you will get below output on IDE console:
1 2 3 |
1 1 0 |
Here value :1 says query executed successfully and value:0 says there is no employee with id:20
That’s all for this topic JDBC batch update with Statement.
You May Also Like:
Calling StoredProcedure Using CallableStatement
Get ResultSet By Calling StoredProcedure Using CallableStatement
Calling database custom Function Using CallableStatement
JDBC batch update with PreparedStatement
JDBC batch update with CallableStatement
How to retrieve data from JDBC resultset in java
How to update a Row in a Database Table Using an updatable ResultSet
How to insert a Row in a Database Table Using an updatable ResultSet
JDBC ResultSet navigation methods example in java
If you have any feedback or suggestion please feel free to drop in below comment box.