2/6/19

JDBC - Coding Reference Part - 3 Batch Operation | JAVA DB Conectivity


In this tutorial, I'm going to present Java Database Connectivity (JDBC) Past -3 which has how to handle the batch of SQL statement in java.

Previous class we have looked how to connect the database using JDBC, how to use the JDBC using java swing or GUI application.

Okay, We have to move part 03  how to handle the batch of SQL statement in java.

Steps:
1. Create Connection - Method

  • Load the JDBC Driver
  • Establish the Database Connection

2. Add Batch - Method

  • Create a Statement Object
  • Add the batch of SQL statement - addBatch(sqlQuery);
  • Execute the batch
  • Finally clear the statement object.

Code:


//Package name
package javadatabasee;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


public class JavaDatabasee {

    Connection con;
    
    public static void main(String[] args) {
     
        JavaDatabasee jb = new JavaDatabasee();
        jb.createConnection();
        jb.addBatchCreateStatement();
        jb.addBatchPreparedStatement();
    }
    
    void addBatchPreparedStatement(){
        
        try {
            
            PreparedStatement prst = con.prepareStatement("INSERT INTO infos(name,age,salary) VALUES(?,?,?)");
            prst.setString(1, "User6");
            prst.setInt(2, 25);
            prst.setFloat(3, (float) 25000.00);
            prst.addBatch();
            
            prst.clearParameters();            
            prst.setString(1, "User7");
            prst.setInt(2, 29);
            prst.setFloat(3, (float) 40000.00);
            prst.addBatch();
            
            prst.clearParameters();                        
            int x[] = prst.executeBatch();
            for(int i: x){
                System.out.println(i);
            }
            
            prst.close();
            
        } catch (SQLException ex) {
            Logger.getLogger(JavaDatabasee.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    void addBatchCreateStatement(){
        
        try {
            //Create Statement
            Statement stmt = con.createStatement();
            
            //Add Batch of sql statement
            stmt.addBatch("INSERT INTO infos(name,age,salary) VALUES('USER1',20,25000)");
            stmt.addBatch("INSERT INTO infos(name,age,salary) VALUES('USER2',21,23000)");
            stmt.addBatch("INSERT INTO infos(name,age,salary) VALUES('USER3',22,26000)");
            stmt.addBatch("INSERT INTO infos(name,age,salary) VALUES('USER4',23,29000)");
            stmt.addBatch("INSERT INTO infos(name,age,salary) VALUES('USER5',24,28000)");
            
            
            int es[]=stmt.executeBatch();
            
            for(int i : es){
                System.out.println(i);
            }
            
            stmt.close();
            
            
        } catch (SQLException ex) {
            Logger.getLogger(JavaDatabasee.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
   
    
    void createConnection(){
      
        try {
            
            final String url="jdbc:mysql://localhost/crud";
            final String username="root";
            final String pwd = "";
            
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            con = DriverManager.getConnection(url,username,pwd);
            
            System.out.println();
            System.out.println("Database Connected!");         
            
            
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(JavaDatabasee.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
}

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *