为什么我的行没有更新?

时间:2018-04-26 15:31:15

标签: java mysql jdbc

我创建了一个应该更新行的Prepared Statement,但是行没有更新。

    /**
 * 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);
    }
}

故意抛出异常 Purposely thrown exception

您可以从我的调试代码中看到我尝试抛出异常以查看所有内容都已正确设置。我可以确认我的表中存在具有此IndexID / UUID的行。

执行该语句并且不会抛出任何异常,表示发生了更新 - 但是,在检查我的表时,这些值不会更新。

我想弄清楚为什么会这样?我已经检查过,在构建连接时我没有将autocommit设置为false。 FileCount和Size列都设置为默认值'0' - 但默认值不应覆盖语句值,对吗?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:2)

在我看来,如果您的更新语法错误,第一个AND应该是逗号:

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

试试。