为什么我的行没有更新?

kmbjn2e3  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(264)

我已经创建了一个准备好的语句,它应该更新一行,但是该行不会更新。

/**
 * Sets the last modified date of the Index to Now();
 * Automatically gets the number of files and total size of files in index that are included in the index and sets that data too.
 * @return
 * @throws SQLException 
 * @throws ClassNotFoundException 
 * @throws UnexpectedException 
 */
public void MarkUpdated() throws ClassNotFoundException, SQLException, UnexpectedException
{
    Connection connection = null;
    PreparedStatement statement1 = null;
    PreparedStatement statement2 = null;
    PreparedStatement statement3 = null;
    ResultSet rSet1 = null;
    ResultSet rSet2 = null;
    // First, lets obtain the file count
    try
    {
    Integer count;
    connection = ConnectionMgr.getConnectionToSc1();
    connection.setAutoCommit(true);
    statement1 = connection.prepareStatement
            ("SELECT COUNT(*) FROM indexFiles WHERE IndexID=? AND IndexUUID=?");
    statement1.setLong(1, IndexID);
    statement1.setString(2, IndexUUID);
    rSet1 = statement1.executeQuery();
    if (rSet1.next())
    {

        count = rSet1.getInt(1);

    }
    else
    {
        throw new UnexpectedException("Failed to Mark Updated - Couldn't get file count?");
    }
    // Now lets obtain the total amount of disk space these files are occupying
    // This is a bit more complicated
    Long size;
    statement2 = connection.prepareStatement
            ("SELECT SUM(FileSize) FROM files WHERE FileUUID IN "
            +"(SELECT FileUUID FROM indexFiles WHERE IndexID=? AND IndexUUID=?)");
    statement2.setLong(1, IndexID);
    statement2.setString(2, IndexUUID);
    rSet2 = statement2.executeQuery();
    if (rSet2.next())
    {
        size = rSet2.getLong(1);
    }
    else
    {
        throw new UnexpectedException("Failed to Mark Updated - Couldn't get sum of file sizes?");
    }

    // Now we mark the index as updated with the new file count
    statement3 = connection.prepareStatement
            ("UPDATE indexes SET FileCount=? AND Size=? WHERE IndexID=? AND IndexUUID=?");
    statement3.setInt(1, count);
    statement3.setLong(2, size);
    statement3.setLong(3, IndexID);
    statement3.setString(4, IndexUUID);
    //if (1 != 0)
    //{
    //  throw new UnexpectedException("Count " + count + " Size " + size + " IndexID " + IndexID + " IndexUUID " + IndexUUID);
    //}
    int rowsUpdated = statement3.executeUpdate();
    if (rowsUpdated != 1) {throw new UnexpectedException("Failed to mark an index as updated: Index UUID is " + IndexUUID);}
    } finally {
        DbUtils.closeQuietly(rSet1);
        DbUtils.closeQuietly(rSet2);
        DbUtils.closeQuietly(statement1);
        DbUtils.closeQuietly(statement2);
        DbUtils.closeQuietly(statement3);
        DbUtils.closeQuietly(connection);
    }
}

故意抛出异常故意抛出异常
您可以从我的调试代码中看到,我试图抛出一个异常,以查看所有设置是否正确。我可以确认我的表中存在具有此索引/uuid的行。
执行该语句时,不会引发任何异常,表明发生了更新-但是,在检查我的表时,这些值不会更新。
我在想为什么会这样?我已经检查过,并且在构建连接时没有将autocommit设置为false。filecount和size列都设置为默认值“0”,但默认值不应覆盖语句值,对吗?
提前谢谢你的帮助

cdmah0mi

cdmah0mi1#

在我看来,好像你的更新语法是错误的,第一个应该是逗号:

UPDATE indexes SET FileCount=?, Size=? WHERE IndexID=? AND IndexUUID=?

试试看。

相关问题