Followers

JDBC : Batch updations

package com.pack1;

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

public class BatchUpdation {
   
    public static void main(String[] args){
        Connection con = null;
        Statement stmt = null;
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            String url = "jdbc:derby://localhost:1527/hari";
            con = DriverManager.getConnection(url, "app","app");
            stmt = con.createStatement();
            String q1 = "update myemp set salary = 11111 where salary <= 7000";
            String q2 = "update myemp set salary = 9999 where salary > 7000";
            stmt.addBatch(q1);
            stmt.addBatch(q2);
            int r[] = stmt.executeBatch();
            for(int i=0;i<r.length;i++){
            System.out.println("No. of records updated "+r[i]);
            }
         
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }catch (SQLException ex1) {
            ex1.printStackTrace();
        }
    }
}

No comments:

Post a Comment