batchupdates.java

来自「Java示例100」· Java 代码 · 共 87 行

JAVA
87
字号
/** * A simple sample to demonstrate Batch Updates. * Please use jdk1.2 or later version */import java.sql.*;public class BatchUpdates{  public static void main(String[] args)  {    Connection          conn = null;    Statement           stmt = null;    PreparedStatement   pstmt = null;    ResultSet           rset = null;    int                 i = 0;    try    {      DriverManager.registerDriver(new oracle.jdbc.OracleDriver());      String url = "jdbc:oracle:oci8:@";      try {        String url1 = System.getProperty("JDBC_URL");        if (url1 != null)          url = url1;      } catch (Exception e) {        // If there is any security exception, ignore it        // and use the default      }      // Connect to the database      conn = DriverManager.getConnection (url, "hr", "hr");      stmt = conn.createStatement();      try { stmt.execute(            "create table mytest_table (col1 number, col2 varchar2(20))");      } catch (Exception e1) {}      //      // Insert in a batch.      //      pstmt = conn.prepareStatement("insert into mytest_table values (?, ?)");      pstmt.setInt(1, 1);      pstmt.setString(2, "row 1");      pstmt.addBatch();      pstmt.setInt(1, 2);      pstmt.setString(2, "row 2");      pstmt.addBatch();      pstmt.executeBatch();      //      // Select and print results.      //      rset = stmt.executeQuery("select * from mytest_table");      while (rset.next())      {        System.out.println(rset.getInt(1) + ", " + rset.getString(2));      }    }    catch (Exception e)    {      e.printStackTrace();    }    finally    {      if (stmt != null)      {	try { stmt.execute("drop table mytest_table"); } catch (Exception e) {}        try { stmt.close(); } catch (Exception e) {}      }      if (pstmt != null)      {        try { pstmt.close(); } catch (Exception e) {}      }      if (conn != null)      {        try { conn.close(); } catch (Exception e) {}      }    }  }}

⌨️ 快捷键说明

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