Efficient way to do batch INSERTS with JDBC

前端 未结 10 719
无人共我
无人共我 2020-11-22 14:12

In my app I need to do a lot of INSERTS. Its a Java app and I am using plain JDBC to execute the queries. The DB being Oracle. I have enabled batching though, so it saves me

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 14:45

    This is a mix of the two previous answers:

      PreparedStatement ps = c.prepareStatement("INSERT INTO employees VALUES (?, ?)");
    
      ps.setString(1, "John");
      ps.setString(2,"Doe");
      ps.addBatch();
    
      ps.clearParameters();
      ps.setString(1, "Dave");
      ps.setString(2,"Smith");
      ps.addBatch();
    
      ps.clearParameters();
      int[] results = ps.executeBatch();
    

提交回复
热议问题