Statement: executeBatch() : Statement « java.sql « Java by API






Statement: executeBatch()

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

public class Main {

  public static void main(String[] args) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

    Connection con;
    Statement stmt;
    ResultSet rs;

    try {
      Class.forName(driver);
      con = DriverManager.getConnection("jdbc:odbc:databaseName", "student", "student");
      // Start a transaction
      con.setAutoCommit(false);

      stmt = con.createStatement();
      stmt.addBatch("UPDATE EMP SET JOB = 1");

      // Submit the batch of commands for this statement to the database
      stmt.executeBatch();

      // Commit the transaction
      con.commit();

      // Close the existing to be safe before opening a new one
      stmt.close();

      // Print out the Employees
      stmt = con.createStatement();
      rs = stmt.executeQuery("SELECT * FROM EMP");
      // Loop through and print the employee number, job, and hiredate
      while (rs.next()) {
        int id = rs.getInt("EMPNO");
        int job = rs.getInt("JOB");
        String hireDate = rs.getString("HIREDATE");

        System.out.println(id + ":" + job + ":" + hireDate);
      }
      con.close();
    } catch (SQLException ex) {
      ex.printStackTrace();
    }
  }
}

           
         
  








Related examples in the same category

1.Statement.EXECUTE_FAILED
2.Statement.RETURN_GENERATED_KEYS
3.Statement.SUCCESS_NO_INFO
4.Statement: addBatch(String sql)
5.WebRowSet: execute(Connection conn)
6.Statement: executeQuery(String sql)
7.Statement: getFetchSize()
8.Statement: getGeneratedKeys()
9.Statement: getMaxFieldSize()
10.Statement: getMaxRows()
11.Statement: getQueryTimeout()
12.WebRowSet: setCommand(String cmd)
13.Statement: setFetchSize(int rows)