executeupdateinsert查询返回0,但在手动插入时工作

cfh9epnr  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(347)

我有多个preparedstatements的示例,它们都是有效的,而且都是提交的。除此代码块不起作用外:

public static int InsertBatchProcessingLogs(Connection con, Data dt) {
    int batchID = -1;
    PreparedStatement preparedStatement = null;

    try {
        System.out.println("INSERT INTO BATCH PROCESSING LOGS");
        preparedStatement = con.prepareStatement("insert into batch_processing_logs (batchID, targetMSISDN,plancode,createddate) select ?,targetMSISDN,plancode,CURRENT_TIMESTAMP from batch_list_tbl where scheduleID = ? and batch_list_status = 1",1);
        preparedStatement.setString(1, dt.getValue("batchID"));
        preparedStatement.setString(2, dt.getValue("scheduleID"));
        batchID = preparedStatement.executeUpdate();
        // batchID = preparedStatement.executeUpdate("insert into batch_processing_logs (batchID, targetMSISDN,plancode,createddate) select "+dt.getValue("batchID")+",targetMSISDN,plancode,CURRENT_TIMESTAMP from batch_list_tbl where scheduleID = "+dt.getValue("batchID")+" "
        // +" and batch_list_status = 1");
        con.commit();
        System.out.println("INSERT QUERY: " + batchID);

        if (batchID > 0) {
           System.out.println("INSERT BATCH LOGS SUCCESSFUL" +dt.getValue("batchID")+dt.getValue("scheduleID"));
        } else {
            System.out.println("FAILED EXECUTIONS OF INSERT BATCH LOGS");
            System.out.println(preparedStatement);
        }
    } catch (Exception exprep) {
        System.out.println(exprep);
        // Log("InsertBatchProcessingLogs Exception:" + exprep.getMessage());
        exprep.printStackTrace();
        batchID = -1;
    }
    return batchID;
}

它总是返回0。如您所见,这里有一个preparedstatement的打印,所以当我尝试手动查询它时,它工作得非常好。我不知道为什么它不能在executeupdate()上工作:(它也不能打印stacktrace,没有显示错误。我试了很多,甚至评论行,它仍然不起作用。我不确定我是否看错了地方。我还尝试了setautocommit(false),然后在执行更新之后提交它。还尝试了setautocommit(true)并删除了con.commit()代码。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题