batchupdateexample.java

来自「java 完全探索的随书源码」· Java 代码 · 共 87 行

JAVA
87
字号
import java.sql.*;public class BatchUpdateExample{  // Private reference to an instance of the Connection  Connection connection = null;  // Default Constructor  public BatchUpdateExample( Connection conn )  {    super();    connection = conn;  }  // Private accessor for the connection  private Connection getConnection()  {    return connection;  }  // Print out the Employee Records backwards using a scrollable  // ResultSet  public void batchUpdates()  {    Statement stmt = null;    ResultSet rs = null;    try    {      // Start a transaction      getConnection().setAutoCommit( false );      stmt = getConnection().createStatement();      stmt.addBatch( "UPDATE EMP SET JOB = 1" );      stmt.addBatch( "UPDATE EMP SET HIREDATE = '17-DEC-1999'" );      // Submit the batch of commands for this statement to the database      stmt.executeBatch();      // Commit the transaction      getConnection().commit();      // Close the existing to be safe before opening a new one      stmt.close();      // Print out the Employees      stmt = getConnection().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 );      }    }    catch( SQLException ex )    {      ex.printStackTrace();    }  }  public static void main(String[] args)  {     // Use the previous DatabaseManager    Connection conn = DatabaseManager.getConnection();    BatchUpdateExample example = new BatchUpdateExample( conn );    example.batchUpdates();    // Always make sure to close the connection when you are finished    try    {      conn.close();    }    catch( SQLException ex )    {      ex.printStackTrace();    }    catch( Exception ex )    {      ex.printStackTrace();    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?