Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-将记录插入数据库时executebatch不工作_Java_Mysql - Fatal编程技术网

Java-将记录插入数据库时executebatch不工作

Java-将记录插入数据库时executebatch不工作,java,mysql,Java,Mysql,在从文件中提取单词并将其存储在哈希集中之后,我尝试将记录插入MYSQL中的表中 在获得500条记录后,我尝试使用executeBatch()插入到我的数据库中,但当执行完成时,我检查了我的表,没有插入任何记录 注意:当我使用ExecuteUpdate()时,记录将显示在我的表中。但不是ExecuteBatch(),因为我希望按批插入,而不是逐个插入。 我能知道我做错了什么吗 代码: 这是我调用该方法的循环(words只是一个数组,其中包含要插入到表中的单词): for(inti=1;i循环在哪里

在从文件中提取单词并将其存储在哈希集中之后,我尝试将记录插入MYSQL中的表中

在获得500条记录后,我尝试使用executeBatch()插入到我的数据库中,但当执行完成时,我检查了我的表,没有插入任何记录

注意:当我使用ExecuteUpdate()时,记录将显示在我的表中。但不是ExecuteBatch(),因为我希望按批插入,而不是逐个插入。 我能知道我做错了什么吗

代码:

这是我调用该方法的循环(words只是一个数组,其中包含要插入到表中的单词):


for(inti=1;i循环在哪里。试试这个

 connect = DriverManager
                .getConnection("jdbc:mysql://126.32.3.20/fulltext_ltat?"
                        + "user=root&password=root&rewriteBatchedStatements=true");

你的线圈在哪,试试这个

 connect = DriverManager
                .getConnection("jdbc:mysql://126.32.3.20/fulltext_ltat?"
                        + "user=root&password=root&rewriteBatchedStatements=true");

您执行批处理更新的模式已关闭。您应该只打开一次连接并准备一次语句。然后,重复多次,绑定参数,并将该语句添加到批处理中

// define a collection of paths and words somewhere
List<String> paths = new ArrayList<>();
List<String> words = new ArrayList<>();

try {
    // presumably you only want to insert so many records
    int LIMIT = 10000;
    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 < LIMIT; ++i) {
        preparedStatement.setString(1, paths.get(i));
        preparedStatement.setString(2, word.get(i));
        preparedStatement.addBatch();
        if (i % 500 == 0) {
            preparedStatement.executeBatch();
        }
    }

    // execute remaining batches
    preparedStatement.executeBatch();
}
catch (SQLException e) {
    e.printStackTrace();
}
finally {
    try {
        preparedStatement.close();
        connect.close();
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
}
//在某处定义路径和单词的集合
列表路径=新的ArrayList();
List words=new ArrayList();
试一试{
//大概您只想插入这么多记录
整数极限=10000;
Class.forName(“com.mysql.jdbc.Driver”);
connect=DriverManager
.getConnection(“jdbc:mysql://126.32.3.20/fulltext_ltat?"
+“user=root&password=root”);
String sql=“将忽略插入全文索引值(默认值,,?);”;
preparedStatement=connect.prepareStatement(sql);
对于(int i=0;i

我在这里所做的一个关键更改是为何时停止执行插入添加逻辑。目前,您的代码似乎有一个无限循环,这意味着它将永远运行。这可能不是您打算执行的操作。

您的批处理更新模式已关闭。您应该只打开连接并准备语句然后,多次迭代,绑定参数,并将该语句添加到批处理中

// define a collection of paths and words somewhere
List<String> paths = new ArrayList<>();
List<String> words = new ArrayList<>();

try {
    // presumably you only want to insert so many records
    int LIMIT = 10000;
    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 < LIMIT; ++i) {
        preparedStatement.setString(1, paths.get(i));
        preparedStatement.setString(2, word.get(i));
        preparedStatement.addBatch();
        if (i % 500 == 0) {
            preparedStatement.executeBatch();
        }
    }

    // execute remaining batches
    preparedStatement.executeBatch();
}
catch (SQLException e) {
    e.printStackTrace();
}
finally {
    try {
        preparedStatement.close();
        connect.close();
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
}
//在某处定义路径和单词的集合
列表路径=新的ArrayList();
List words=new ArrayList();
试一试{
//大概您只想插入这么多记录
整数极限=10000;
Class.forName(“com.mysql.jdbc.Driver”);
connect=DriverManager
.getConnection(“jdbc:mysql://126.32.3.20/fulltext_ltat?"
+“user=root&password=root”);
String sql=“将忽略插入全文索引值(默认值,,?);”;
preparedStatement=connect.prepareStatement(sql);
对于(int i=0;i

我在这里做的一个关键更改是为何时停止执行插入添加逻辑。目前,您的代码看起来有一个无限循环,这意味着它将永远运行。这可能不是您想要做的。这是做什么的?循环在哪里?您的preparedStatement必须在循环之外如果您移动连接,Prepare函数的预处理语句意味着如何生成?这有什么作用?循环在哪里?预处理语句必须在循环外如果移动连接,函数的预处理语句意味着如何生成?我的代码的哪一部分包括运行无限循环?如果将限制设为10000,那么如果我的哈希集包含更多在限制范围内,10000之后它不会插入,对吗?哎呀…你没有任何循环,这可能是没有批写入数据库的原因。因此,你需要在某个地方有一个循环,但它应该在我突出显示的地方,而不是在你打开连接并准备insert语句的地方。限制是设置单词that最多可以插入10000条记录,对吗?是的,我有多达10000条记录,一次最多插入500条(
最多插入500条
…最后一批可能不是500条,取决于您选择的限制)。我尝试了您的解决方案,得到了一个奇怪的结果。我有一个重复单词列表(事实上超过1000个)这是不应该发生的。是因为我也在另一个for循环中从另一个类调用该方法吗?如果你看我编辑的帖子,我添加了一些更改。我的代码的哪一部分包括运行无限循环?如果你将限制设置为10000,那么如果我的哈希集包含的内容超过限制,它将不会在10之后插入,000对吗?噢…你没有任何循环,这可能是没有批写入数据库的原因。因此,你需要在某个地方有一个循环,但它应该在我突出显示的地方,而不是在你打开连接并准备insert语句的地方。限制是将可插入的字设置为最多10000,我说得对吗?是的,我有多达10000条记录,一次最多插入500条(
最多插入500条
…根据您选择的限制,最后一批可能不是500条)。我尝试了您的解决方案,得到了一个奇怪的结果。我有一个
// define a collection of paths and words somewhere
List<String> paths = new ArrayList<>();
List<String> words = new ArrayList<>();

try {
    // presumably you only want to insert so many records
    int LIMIT = 10000;
    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 < LIMIT; ++i) {
        preparedStatement.setString(1, paths.get(i));
        preparedStatement.setString(2, word.get(i));
        preparedStatement.addBatch();
        if (i % 500 == 0) {
            preparedStatement.executeBatch();
        }
    }

    // execute remaining batches
    preparedStatement.executeBatch();
}
catch (SQLException e) {
    e.printStackTrace();
}
finally {
    try {
        preparedStatement.close();
        connect.close();
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
}