JDBC批量插入

    public void batchInsert() {
        String sql = "insert into goods(name) values(?)";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < 10000; i++) {
                ps.setObject(1,"name_" + i);

                ps.addBatch();

                if (i % 500 == 0) {
                    ps.executeBatch();

                    ps.clearBatch();
                }
            }
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn,ps);
        }
    }

 

posted on 2022-03-30 08:08  金满仓  阅读(312)  评论(0编辑  收藏  举报

导航