0% found this document useful (0 votes)
172 views

11.batch Updations

This document discusses batch updates in Java database applications. It explains that batching SQL queries can improve performance by sending multiple queries to the database at once rather than sequentially. The Statement and PreparedStatement objects in JDBC allow collecting SQL queries into a batch using addBatch() and executing the entire batch together with executeBatch(), which returns an array of row counts. Select queries should not be included in batches as this will cause an error.

Uploaded by

Harman Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

11.batch Updations

This document discusses batch updates in Java database applications. It explains that batching SQL queries can improve performance by sending multiple queries to the database at once rather than sequentially. The Statement and PreparedStatement objects in JDBC allow collecting SQL queries into a batch using addBatch() and executing the entire batch together with executeBatch(), which returns an array of row counts. Select queries should not be included in batches as this will cause an error.

Uploaded by

Harman Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA Means DURGASOFT

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.

public void addBatch(String query)

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.

public int[] executeBatch()

Where int[] will represent all the row count values generated from the updation group SQL queries.

Batch updations with Statement:


2
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

JdbcApp30:
package com.durgasoft;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JdbcApp30 {


public static void main(String[] args)throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "durga");
Statement st=con.createStatement();
st.addBatch("insert into emp1 values(666,'FFF',9000,'Hyd')");
st.addBatch("update emp1 set esal=esal-500 where esal<10000");
st.addBatch("delete from emp1 where eno=555");
// st.addBatch("select * from emp1");--> java.sql.BatchUpdateException
int[] rowCounts=st.executeBatch();
for(int i=0;i<rowCounts.length;i++){
System.out.println("Records Manipulated :"+rowCounts[i]);
}
st.close();
con.close();
}

Batch Updations with PreparedStatement:

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;

public class JdbcApp31 {


public static void main(String[] args)throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/durgadb",
"root","root");
PreparedStatement pst=con.prepareStatement("insert into emp1 values(?,?,?,?)");
pst.setInt(1,666);
pst.setString(2, "FFF");
pst.setFloat(3, 6000);
pst.setString(4, "Hyd");
pst.addBatch();

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy