Efficient way to do batch INSERTS with JDBC

前端 未结 10 596
无人共我
无人共我 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条回答
  •  逝去的感伤
    2020-11-22 14:53

    Batch insert using statement

    int a= 100;
                try {
                            for (int i = 0; i < 10; i++) {
                                String insert = "insert into usermaster"
                                        + "("
                                        + "userid"
                                        + ")"
                                        + "values("
                                        + "'" + a + "'"
                                        + ");";
                                statement.addBatch(insert);
                                System.out.println(insert);
                                a++;
                            }
                          dbConnection.commit();
                        } catch (SQLException e) {
                            System.out.println(" Insert Failed");
                            System.out.println(e.getMessage());
                        } finally {
                
                            if (statement != null) {
                                statement.close();
                            }
                            if (dbConnection != null) {
                                dbConnection.close();
                            }
                        }
            
    

提交回复
热议问题