JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Write an example for batch update using PreparedStatement.


Description:

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. When we are using PreparedStatement to execute batch update, we have to run the same query multiple times. Below examples shows how to do batch updates with PreparedStatement.


Code:
package com.java2novice.jdbc;

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

public class MyBatchPreparedStmt {

	public static void main(String a[]){
		
		Connection con = null;
		PreparedStatement pst = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.
					getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
						,"user","password");
			con.setAutoCommit(false);
			pst = con.prepareStatement("update emp set sal=? where empid=?");
			pst.setInt(1, 3000);
			pst.setInt(2, 200);
			pst.addBatch();
			pst.setInt(1, 4000);
			pst.setInt(2, 230);
			pst.addBatch();
			int count[] = pst.executeBatch();
			for(int i=1;i<=count.length;i++){
				System.out.println("Query "+i+" has effected "+count[i]+" times");
			}
			con.commit();
		} catch (ClassNotFoundException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally{
			try{
				if(pst != null) pst.close();
				if(con != null) con.close();
			} catch(Exception ex){}
		}
	}
}
<< Previous Program | Next Program >>
blog comments powered by Disqus

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
doPost Vs doGet methods
doGet() method is used to get information, while doPost() method is used for posting information. doGet() requests can't send large amount of information and is limited to 240-255 characters. However, doPost()requests passes all of its data, of unlimited length. A doGet() request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
Famous Quotations
Never argue with a fool, onlookers may not be able to tell the difference.
-- Mark Twain

About Author

Most Visited Pages

Other Interesting Sites

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