Tuesday, November 1, 2016

JDBC batch Update Example

In this article we will learn about JDBC Batch Update  and understand you an Example from JDBC batch update. JDBC batch update is a collectively called when a group of SQL statements are executes simultaneously to a database as a single unit. That means the batch sent to the database in a single request using connection object.The main advantage of batch is to reduce the network calls between the front end application and its database rather than executing single SQL statement.

Now, we will understand this concept  with example:

We have a class JDBC Batch Update. Before defining a class we need to import a java.sql package. This package contains the definition for all classes which provides network interface to communicate between front end application and back end.
The Next Step is load a driver,that calls a Class.forName(). This help you in establishing a connection between the front end and back end of application. 
The Next Step is DriverManager.getConnection(),this method returns you a connection object,that is used to build connection between URL and database.

con.setAutoCommit(false)//this method returns you the disable auto commit

con.createStatement()// This method returns a SQL object.Once your connection is built between front end and back end , you can retrieve,update  the record in the database. A connection object is used to send and execute SQL statement to a back end database.

st.addBatches(update)//This method returns you the modify value in the database using update query in the SQL command. This method also shows the number of rows affected in a table.

executeQuery()// this method is used to retrieve records from the database using select query.

rs.next()// this method returns you the next successive value in the element series.

Example:

import java.sql.*;
public class JDBCBatchUpdate
{
public static void main(String args[])
{

Connection con=null;
Statement st=null;
ResultSet rs=null;
String url="jdbc:oracle:thin:@localhost:1521";
String db="xe";
String driver="oracle.jdbc.driver.OracleDriver";
String username="system";
String pass="manager";
try{
Class.forName(driver);
con=DriverManager.getConnection(url +db,user,pass);
con.setAutoCommit(false);//Disable auto commit 
st=con.createStatement();
st.addBatch(("update person set cname="mohan" where id='101'");
st.addBatch(("update person set cname="pavan" where id='102'");
st.addBatch(("update person set cname="lucky" where id='103'"); 
st.exectueBatch();
String sql="select * from person";
rs=st.executeQuery(sql);
System.out.println("NO name");
while(rs.next())
{
System.out.println(rs.getString(101)+"\t");
System.out.println(rs.getString(102));
}
rs.close();

st.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();

}
}
}
 
 

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...