Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: How to Create a batch update in JDBC

Java: How to Create a batch update in JDBC
{filelink=7022}

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class BatchUp
 {
  public static void main(String[] args) throws Exception
  	{
      Connection  conn=null;
    try
    	{
     // open connection to MySQL database
      conn = DriverManager.getConnection("jdbc:mysql://localhost/cours", "root", "");
      conn.setAutoCommit(false);  // Disable AutoCommit mode
      Statement stmt = conn.createStatement();
      stmt.addBatch("INSERT INTO Test (id, name, price) VALUE (0, 'Sakoba test', '1500€')");
      stmt.addBatch("INSERT INTO Test (id, name, price) VALUE (0, 'Steve Jhon', '2500€')");
      stmt.addBatch("INSERT INTO Test (id, name, price) VALUE (0, 'NiColas Salo', '100€')");
      // Run the batch
      int[] results = stmt.executeBatch();
      conn.commit();
    } catch (SQLException ex)
    {
    	// otherwise, cancel the batch
        conn.rollback();
      ex.printStackTrace();
    } finally {
      if (conn!= null) {
        conn.close();
      }
    }
  }
}

Users who found this page were searching for:

  • how to use scanner in java
  • how to output dialog box in java
  • how to use formatter in java
  • marriage of progress bar and jdbc table creation java
  • perl oracle batch update

No comments yet.

Leave a Comment