Efficient way to do batch INSERTS with JDBC

前端 未结 10 581
无人共我
无人共我 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

    The Statement gives you the following option:

    Statement stmt = con.createStatement();
    
    stmt.addBatch("INSERT INTO employees VALUES (1000, 'Joe Jones')");
    stmt.addBatch("INSERT INTO departments VALUES (260, 'Shoe')");
    stmt.addBatch("INSERT INTO emp_dept VALUES (1000, 260)");
    
    // submit a batch of update commands for execution
    int[] updateCounts = stmt.executeBatch();
    
    0 讨论(0)
  • 2020-11-22 14:53

    In my code I have no direct access to the 'preparedStatement' so I cannot use batch, I just pass it the query and a list of parameters. The trick however is to create a variable length insert statement, and a LinkedList of parameters. The effect is the same as the top example, with variable parameter input length.See below (error checking omitted). Assuming 'myTable' has 3 updatable fields: f1, f2 and f3

    String []args={"A","B","C", "X","Y","Z" }; // etc, input list of triplets
    final String QUERY="INSERT INTO [myTable] (f1,f2,f3) values ";
    LinkedList params=new LinkedList();
    String comma="";
    StringBuilder q=QUERY;
    for(int nl=0; nl< args.length; nl+=3 ) { // args is a list of triplets values
        params.add(args[nl]);
        params.add(args[nl+1]);
        params.add(args[nl+2]);
        q.append(comma+"(?,?,?)");
        comma=",";
    }      
    int nr=insertIntoDB(q, params);
    

    in my DBInterface class I have:

    int insertIntoDB(String query, LinkedList <String>params) {
        preparedUPDStmt = connectionSQL.prepareStatement(query);
        int n=1;
        for(String x:params) {
            preparedUPDStmt.setString(n++, x);
        }
        int updates=preparedUPDStmt.executeUpdate();
        return updates;
    }
    
    0 讨论(0)
  • 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();
                            }
                        }
            
    
    0 讨论(0)
  • 2020-11-22 14:59

    You can use this rewriteBatchedStatements parameter to make the batch insert even faster.

    you can read here about the param: MySQL and JDBC with rewriteBatchedStatements=true

    0 讨论(0)
提交回复
热议问题