java大批量插入数据

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;

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值