JDBC batch update

Jdbc Update is used when you want to save all the modify or changes values in a database.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

JDBC batch update

     

JDBC batch update is a collectively called when a group of SQL statements are executed simultaneously to a database as a single unit. The batch is sent to the database in a single request using connection object. The advantage of batch is to reduce the network calls between the front end application and its database rather than executing single SQL statement.

Understand with Example

In this Tutorial we want to understand you an example from JDBC batch update. For this we have a class JDBC Batch Update. Inside the main method we have the list of following step : Before defining class we need to import a java.sql package. This package contains the definition for all the classes which provides network interface to communicate between front end and back end. Loading a Driver  -  The next step is to load a driver, that call a Class.forName( ).This help you in establishing a connection between the front end -backend of application. The DriverManager.get connection( ) - This method return you a connection object, that is used to built connection between url and database.

  con.setAutoCommit(false)  :  This method return you the disable auto commit.

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

  st.addBatches (update ) : This is used to add the values of the parameter in the database. This method return you the modify value in the database using update query in the sql command. The method also shows the number of rows affected in a table.

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

  rs.next ( ) : This method return you the next successive value in the element series.  

Finally the println print the updated value of the element in the command prompt. Incase there is an exception occurred during a connection between the front end and the backend. The catch block is used, to handle an exception occurred using printstacktrace( ).

  printstacktrace( ): This method prints a stack trace for  Throw able object on the error output stream .

JdbcBatchUpdate.java

import java.sql.*;
public class JdbcBatchUpdate {
  public static void main(String args[]) {
  Connection con = null;
  Statement st = null;
  ResultSet rs = null;
  String url = "jdbc:mysql://localhost:3306/";
  String db = "komal";
  String driver = "com.mysql.jdbc.Driver";
  String user = "root";
  String pass = "root";
  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);// Disables auto-commit.
  st = con.createStatement();
  st.addBatch("update person set cname='Ajay' where id='1'");
  st.addBatch("update person set cname='Komal' where id='2'");
  st.addBatch("update person set cname='Santosh' where id='3'");
  st.executeBatch();
  String sql = "select * from person";
  rs = st.executeQuery(sql);
  System.out.println("No  \tName");
  while (rs.next()) {
  System.out.print(rs.getString(1) + " \t");
  System.out.println(rs.getString(2));
  }
  rs.close();
  st.close();
  con.close();
  catch (Exception e) {
  e.printStackTrace();
  }
  }

Output

No  	Name
4   	Komal
5   	Ajay
6   	Santosh

Download code

Advertisements

Ads
Share on Google+Share on Google+

JDBC batch update

Posted on: November 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC batch update  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads