11.batch Updations
11.batch Updations
1
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
Batch Updations
In general in Jdbc applications, it is required to provide number of SQL queries according to
application requirement.
With the above, if we execute the Jdbc application then JVM will send all the SQL queries to the
database in a sequential manner.
If we use the above convention to execute SQL queries in Jdbc application we have to spend a lot of
time only to carry or transfer SQL queries from Java application to database,this approach will
reduce the performance of Jdbc application.
In the above context, to improve the performance of the Jdbc applications we have to use Batch
updations.
In batch updations, we will gather or collect all the updation group SQL queries as a single unit called
as Batch and we will send batch of updation group SQL queries at a time from Java application to
database.
At database, Database Engine may execute all the SQL queries and generate respective row count
values in the form of an array to Java application.
To add an SQL query to batch we have to use the following method from Statement.
To send batch of updation group SQL queries at a time from Java application to database and to
make the Database Engine to execute all the batch of updation group SQL queries we have to use
the following method from Statement.
Where int[] will represent all the row count values generated from the updation group SQL queries.
JdbcApp30:
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
JdbcApp31:
3
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
pst.setInt(1,777);
pst.setString(2, "GGG");
pst.setFloat(3, 7000);
pst.setString(4, "Hyd");
pst.addBatch();
pst.setInt(1,888);
pst.setString(2, "HHH");
pst.setFloat(3, 8000);
pst.setString(4, "Hyd");
pst.addBatch();
int[] rowCounts=pst.executeBatch();
for(int i=0;i<rowCounts.length;i++){
System.out.println("Records Manipulated :"+rowCounts[i]);
}
con.close();
Note: If we include selection group SQL query in a batch then JVM will raise an Exception like
ava.sql.BatchUpdateException: invalid batch command: invalid SELECT batch command.
4
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
5
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com