java 在 jdbc 的循环中多次插入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3872673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:47:05  来源:igfitidea点击:

Multiple insert in a loop in jdbc

javasqljdbc

提问by gaurav

while (tokens.hasMoreTokens()) 
{
    keyword = tokens.nextToken();
    System.out.println("File= "+fileid+" Keyword=" + keyword);
    stmt.executeUpdate(
        "INSERT into TEXTVALUEINVERTEDINDEX " + "(FILEID, KEYWORD) values ('"
        + fileid + "', '" + keyword + "')"
    );      
}

This is the loop in which I'm updating the rows. The problem I'm facing is that when i run this only 1 value gets updated and when I comment the stmt.executeUpdate()line it displays all the possible entries in the database.

这是我更新行的循环。我面临的问题是,当我运行时,只有 1 个值被更新,当我评论该stmt.executeUpdate()行时,它会显示数据库中所有可能的条目。

回答by Matt Fellows

You need to use preparedStatements...

你需要使用preparedStatements...

PreparedStatement pStmt = connection.prepareStatement("INSERT into TEXTVALUEINVERTEDINDEX (FILEID, KEYWORD) values(?,?)");
while (tokens.hasMoreTokens()) 
    {
        keyword = tokens.nextToken();
        System.out.println("File= "+fileid+"    Keyword="+keyword);

        pStmt.setString(1, fileid); //This might be pStmt.SetInt(0, fileid) depending on teh type of fileid)
        pStmt.setString(2, keyword);

        pStmt.executeUpdate();
    }

then using this you can extend to us batch update...

然后使用它您可以扩展到我们批量更新...

PreparedStatement pStmt = connection.prepareStatement("INSERT into TEXTVALUEINVERTEDINDEX (FILEID, KEYWORD) values(?,?)");
    while (tokens.hasMoreTokens()) 
        {
            keyword = tokens.nextToken();
            System.out.println("File= "+fileid+"    Keyword="+keyword);

            pStmt.setString(1, fileid); //This might be pStmt.SetInt(0, fileid) depending on teh type of fileid)
            pStmt.setString(2, keyword);

            pStmt.addBatch();
        }
pStmt.executeBatch();

Not sure why your code isn't working though - but this will probably help in the long run...

不知道为什么你的代码不起作用 - 但从长远来看这可能会有所帮助......

回答by Pablo Santa Cruz

Your code should work. Make sure the sentence is not throwing any Exceptions when running by surrounding it with a try/catchblock:

您的代码应该可以工作。通过用try/catch块包围它来确保语句在运行时没有抛出任何异常:

try {
    stmt.executeUpdate("INSERT into TEXTVALUEINVERTEDINDEX " + 
         "(FILEID, KEYWORD) "+"values ('"+fileid+"', '"+keyword+"')"); 
} catch (SQLException e) {
    e.printStackTrace();
}

You should also consider using a PreparedStamentinstead since its use is very appropriate for your described scenario:

您还应该考虑使用 aPreparedStament代替,因为它的使用非常适合您描述的场景:

Something like this:

像这样的东西:

String sql = "insert into textvalueinvertedindex (fileid, keyword) values (?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
while (tokes.hasMoreTokens()) {
    keywords = tokens.nextToken();
    pstmt.setString(1, fileid);
    pstmt.setString(2, keyword);
    pstmt.executeUpdate();
}
pstmt.close();

回答by jutky

If you want all updates to be applied at once you can use batch execution, hereis an example

如果您希望一次应用所有更新,您可以使用批处理执行,是一个示例

回答by mory

Your date range and filter selection contained no results.

您的日期范围和过滤器选择没有包含任何结果。