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

Java-JDBC: execute update query using ‘batch’ method

Java-JDBC:  execute update query using 'batch' method
{filelink=7030}

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.BatchUpdateException;
public class Batch_Del
 {
  public static void main(String[] argv) throws Exception
  	 {
      Connection conn= null;
  try
  	{
     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cours",
        "root", "password");
      Statement stmt = conn.createStatement();
      conn.setAutoCommit(false);
    // create two queries
    String req1 = "Update Test set prix=prix * 2";
    String req2 = "DELETE FROM Test WHERE ID>5";
    // Add query to batch
    stmt.addBatch(req1);
    stmt.addBatch(req2);
    // execute both queries
    int resultats[] = stmt.executeBatch();
    System.out.println("Successfully!");
     }catch(BatchUpdateException err)
     {
     	// Cancel
     	conn.rollback();
        err.printStackTrace()	;
     }
     conn.commit();
     conn.close();
  }
}

Users who found this page were searching for:

  • python nofocus noaction
  • python read file character by character
  • python programming percentage calculation
  • How to put program onto a Python GUI
  • delete python variable

No comments yet.

Leave a Comment