Publish Your Article Here

Write an example for batch update using Statement.


Batch update is nothing but executing set of queries at a time. Batch updates reduces number of database calls. In batch processing, batch should not contain select query. You can add queries by calling addBatch() method and can execute the bunch of queries by calling executeBatch() method. When using batch updates with Statement object, you can use multiple types of queries which can be acceptable in executeUpdate() method.

package com.java2novice.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class MyStatementBatchUpdate {

        public static void main(String a[]){
                Connection con = null;
                Statement st = null;
                try {
                        con = DriverManager.
                                getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
                        st = con.createStatement();
                        st.addBatch("update emp set sal=3000 where empid=200");
                        st.addBatch("insert into emp values (100,4000)");
                        st.addBatch("update emp set emp name='Ram' where empid=345");
                        int count[] = st.executeBatch();
                        for(int i=1;i<=count.length;i++){
                                System.out.println("Query "+i+" has effected "+count[i]+" times");
                } catch (ClassNotFoundException e) {
                        try {
                        } catch (SQLException e1) {
                } catch (SQLException e) {
                        try {
                        } catch (SQLException e1) {
                } finally{
                                if(st != null) st.close();
                                if(con != null) con.close();
                        } catch(Exception ex){}
<< Previous Program | Next Program >>

List Of All JDBC Programs:

  1. How to create JDBC Connection?
  2. What is Statement & ResultSet in JDBC?
  3. How to execute and read select queries using JDBC?
  4. How to update a record in the database using JDBC?
  5. How to execute any type of query in JDBC?
  6. What are the types of JDBC Statements available?
  7. Write an example code for JDBC prepared statement.
  8. Write an example for JDBC prepared statement with ResultSet.
  9. How to get primary key value (auto-generated keys) from inserted queries using JDBC?
  10. Write a simple program for CallableStatement statement to execute stored procedure.
  11. Write a program for CallableStatement statement with stored procedure returns OUT parameters.
  12. Write a program for CallableStatement statement with batch execution.
  13. Write a program to execute SQL function using CallableStatement.
  14. Write a program to execute SQL cursors using CallableStatement.
  15. How to get column properties from ResultSet using ResultSetMetaData?
  16. Write an example for batch update using Statement.
  17. Write an example for batch update using PreparedStatement.
  18. What are the types of ResultSets in JDBC?
  19. Write an example for scrollable result set with read only mode.
  20. Write an example for updatable result set.
  21. How to insert an image into database table? or Write an example for inserting BLOB into table.
  22. How to read an image from database table? or Write an example for reading BLOB from table.
  23. What is DatabaseMetaData? Write an example code.
  24. How to get JDBC Connection object using properties file?
Knowledge Centre
Can we call servlet destory() from service()?
As you know, destory() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call destory(). In case, if you call destory method from service(), it just execute the code written in the destory(), but it wont kill the servlet instance. destroy() will be called before killing the servlet instance by servlet engine.
Famous Quotations
Discipline is just choosing between what you want now and what you want most.
-- Unknown Author

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2024 by Nataraja Gootooru. All Rights Reserved.