java-无法将批处理1000插入表

kfgdxczn  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(319)

假设我有一个程序,将记录插入java中ddatabase的mysql表中。
我不是逐行插入,而是按1000条记录的批插入。使用 ExecuteBatch 方法,它似乎不起作用,因为它仍然逐行插入。
代码(仅代码段):

public void readDataBase(String path,String word) throws Exception {
        try {

            Class.forName("com.mysql.jdbc.Driver");

            connect = DriverManager
                    .getConnection("jdbc:mysql://126.32.3.20/fulltext_ltat?"
                            + "user=root&password=root");
   String sql="insert IGNORE into  fulltext_ltat.indextable values (default,?, ?) ";

            preparedStatement = connect.prepareStatement(sql);

        for(int i=0;i<1000;i++) {
            preparedStatement.setString(1, path);
            preparedStatement.setString(2, word);
            preparedStatement.addBatch();

            if (i % 1000 == 0) {

                preparedStatement.executeBatch();
                System.out.print("Add Thousand");
            }

        }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
                connect.close();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }

代码:调用上述

public static void main(String[] args) throws Exception {

    StopWatch stopwatch = new StopWatch();
    stopwatch.start();

    File folder = new File("D:\\PDF1");
    File[] listOfFiles = folder.listFiles();

    for (File file : listOfFiles) {
        if (file.isFile()) {
            HashSet<String> uniqueWords = new HashSet<>();
            String path = "D:\\PDF1\\" + file.getName();
            try (PDDocument document = PDDocument.load(new File(path))) {

                if (!document.isEncrypted()) {

                    PDFTextStripper tStripper = new PDFTextStripper();
                    String pdfFileInText = tStripper.getText(document);
                    String lines[] = pdfFileInText.split("\\r?\\n");
                    for (String line : lines) {
                        String[] words = line.split(" ");

                        for (String word : words) {
                            uniqueWords.add(word)
                            ;

                        }

                    }
                    // System.out.println(uniqueWords);

                }
            } catch (IOException e) {
                System.err.println("Exception while trying to read pdf document - " + e);
            }
            Object[] words = uniqueWords.toArray();

            MysqlAccessIndex connection = new MysqlAccessIndex();

            for(int i = 1 ; i <= words.length - 1 ; i++ ) {

                connection.readDataBase(path, words[i].toString());

            }

            System.out.println("Completed");

        }
    }

我运行程序的那一刻 if statement 总是执行,而不是检查是否有1000条记录,然后只执行以插入到数据库。
我做错什么了吗?

yfjy0ee7

yfjy0ee71#

在配置属性url中添加: allowMultiQueries=true

kb5ga3dv

kb5ga3dv2#

i % 1000 == 0 什么时候是真的 i==0 ,因此只在循环的第一次迭代中执行批处理。
您应该在循环之后执行批处理:

for (int i=0;i<1000;i++) {
        preparedStatement.setString(1, path);
        preparedStatement.setString(2, word);
        preparedStatement.addBatch();
    }
    preparedStatement.executeBatch();
    System.out.print("Add Thousand");

现在,如果您有10000条记录,并且希望每1000条执行一次批插入,则可以编写:

for (int i=0;i<10000;i++) {
        preparedStatement.setString(1, path);
        preparedStatement.setString(2, word);
        preparedStatement.addBatch();
        if ((i + 1) % 1000 == 0) {
            preparedStatement.executeBatch();
            System.out.print("Add Thousand");
        }
    }

编辑:为了不插入相同的 word 多次向表传递数组到方法:
改变

for(int i = 1 ; i <= words.length - 1 ; i++ ) {
            connection.readDataBase(path, words[i].toString());
        }

connection.readDataBase(path, words);

以及

public void readDataBase(String path,String word) throws Exception {

public void readDataBase(String path,String[] words) throws Exception {

最后,批插入循环将变成:

for (int i=0;i<words.length;i++) {
        preparedStatement.setString(1, path);
        preparedStatement.setString(2, words[i]);
        preparedStatement.addBatch();
        if ((i + 1) % 1000 == 0) {
            preparedStatement.executeBatch();
            System.out.print("Add Thousand");
        }
    }
    if (words.length % 1000 > 0) {
        preparedStatement.executeBatch();
        System.out.print("Add Remaining");
    }

相关问题