Prepared Statement With Batch Update : Batch Update « Database SQL JDBC « Java






Prepared Statement With Batch Update

  

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

public class Main {
  public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial",
        "root", "root");
    con.setAutoCommit(false);
    String sql = "INSERT product VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "A");
    prest.setInt(2, 2002);
    prest.addBatch();
    prest.setString(1, "B");
    prest.setInt(2, 1998);
    prest.addBatch();
    prest.setString(1, "C");
    prest.setInt(2, 1980);
    prest.addBatch();
    prest.setString(1, "D");
    prest.setInt(2, 1975);
    prest.addBatch();
    int count[] = prest.executeBatch();
    con.commit();
    con.close();
  }
}

   
    
  








Related examples in the same category

1.Create a batch update in JDBC
2.Batch Update Insert
3.Batch update: add Batch commands
4.Check Batch Update Result
5.Batch Update Demo
6.Batch update for MySQL
7.Deal with batch update exception and results
8.Demo Prepared Statement Add Batch MySQL
9.Statement Batch Update
10.Determining If a Database Supports Batching