Java 基于for循环更新数据库中的相同记录

Java 基于for循环更新数据库中的相同记录,java,sql,xml,Java,Sql,Xml,在这种情况下,状态为F的文档需要以文件名保存在表日志中 我的表格应具有以下表示形式: ID: Incremented FILE: fileNameWithOutExt (name of the file) ERROR: errorCode 但是,同一文件中具有相同状态的所有文档都需要插入到同一记录中,如下所示: ID: 1 FILE:test ERROR: id:5,id:9,id:10 我的if status语句位于传递xml文件中所有子项的for循环中。状态为F的记录需要连接在同一记录

在这种情况下,状态为F的文档需要以文件名保存在表日志中

我的表格应具有以下表示形式:

ID: Incremented
FILE:  fileNameWithOutExt (name of the file)
ERROR: errorCode
但是,同一文件中具有相同状态的所有文档都需要插入到同一记录中,如下所示:

ID: 1
FILE:test
ERROR: id:5,id:9,id:10
我的if status语句位于传递xml文件中所有子项的for循环中。状态为F的记录需要连接在同一记录中

if(status.equals("F")){
    elemValue = element.getChild("id").getValue();
    String fileNameWithExt = f.getName();
    String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
    saveLogs(fileNameWithOutExt, elemValue);
}

private void saveLogs(String fileNameWithOutExt, String elemValue){
    String errorCode = "id:"+ elemValue;
    String query = "INSERT INTO LOGS (FILE,ERROR)VALUES ('"+fileNameWithOutExt+"','"+errorCode+"')";
    String content = "";
    content = SqlTool.selectOneString("DB", query);   
}

您不能仅使用insert指令进行更新。 尝试查看更新指令,简单的方法是使用SQL脚本,如:

UPDATE MyTable SET FieldA=@FieldA WHERE Key=@Key

IF @@ROWCOUNT = 0
    INSERT INTO MyTable (FieldA) VALUES (@FieldA)

您不能仅使用insert指令进行更新。 尝试查看更新指令,简单的方法是使用SQL脚本,如:

UPDATE MyTable SET FieldA=@FieldA WHERE Key=@Key

IF @@ROWCOUNT = 0
    INSERT INTO MyTable (FieldA) VALUES (@FieldA)