java大批量插入数据

来源:互联网 发布:淘宝收货时间最迟 编辑:程序博客网 时间:2024/06/06 05:10

import java.sql.BatchUpdateException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class testOracle {

 public static void main(String[] args) throws Exception {

  Connection conn = getOracleConnection();

  ResultSet rs = null;

  Statement stmt = null;

  try {

   conn = getOracleConnection();

   stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

   ResultSet.CONCUR_UPDATABLE);

   conn.setAutoCommit(false);
//   for(int i=0;i<10000;i++){
//    stmt.addBatch("INSERT INTO PLUGGEDMATERIALMAINTAINZONE(MAINTAINID, PLATEID) VALUES(1, 'Alex')");
//   }
   stmt.addBatch("INSERT INTO PLUGGEDMATERIALMAINTAINZONE(MAINTAINID, PLATEID) VALUES(1, 'Alex')");
   stmt.addBatch("INSERT INTO PLUGGEDMATERIALMAINTAINZONE(MAINTAINID, PLATEID) VALUES(2, 'Blex')");
   stmt.addBatch("INSERT INTO PLUGGEDMATERIALMAINTAINZONE(MAINTAINID, PLATEID) VALUES(3, 'Clex')");
   
   int[] updateCounts = stmt.executeBatch();

   System.out.println(updateCounts);

   conn.commit();
   if(updateCounts.length>0){
    System.out.println("插入数据已成功!");
   }
   rs = stmt.executeQuery("SELECT * FROM survey");

   while (rs.next()) {

    String id = rs.getString("id");

    String name = rs.getString("name");

    System.out.println("id=" + id + " name=" + name);

   }

  }catch (BatchUpdateException b) {

   System.err.println("SQLException: " + b.getMessage());

   System.err.println("SQLState: " + b.getSQLState());

   System.err.println("Message: " + b.getMessage());

   System.err.println("Vendor error code: " + b.getErrorCode());

   System.err.print("Update counts: ");

   int[] updateCounts = b.getUpdateCounts();

   for (int i = 0; i < updateCounts.length; i++) {
    System.err.print(updateCounts[i] + " ");
   }

  }catch (SQLException ex) {

   System.err.println("SQLException: " + ex.getMessage());

   System.err.println("SQLState: " + ex.getSQLState());

   System.err.println("Message: " + ex.getMessage());

   System.err.println("Vendor error code: " + ex.getErrorCode());

  }catch (Exception e) {

   e.printStackTrace();

   System.err.println("Exception: " + e.getMessage());

  }finally {
   if(rs!=null){
    rs.close();
   }
   if(stmt!=null){
    stmt.close();
   }
   if(conn!=null){
    conn.close();
   }
  }
 }

 private static Connection getHSQLConnection() throws Exception {

  Class.forName("org.hsqldb.jdbcDriver");

  System.out.println("Driver Loaded.");

  String url = "jdbc:hsqldb:data/tutorial";

  return DriverManager.getConnection(url, "sa", "");

 }

 public static Connection getMySqlConnection() throws Exception {

  String driver = "org.gjt.mm.mysql.Driver";

  String url = "jdbc:mysql://localhost/demo2s";

  String username = "oost";

  String password = "oost";

  Class.forName(driver);

  Connection conn = DriverManager.getConnection(url, username, password);

  return conn;

 }

 public static Connection getOracleConnection() throws Exception {

  String driver = "oracle.jdbc.driver.OracleDriver";

  String url = "jdbc:oracle:thin:@localhost:1521:orcl";

  String username = "bcia_airfield_ms";

  String password = "brilliant";

  Class.forName(driver); // load Oracle driver

  Connection conn = DriverManager.getConnection(url, username, password);

  return conn;

 }

}

原创粉丝点击