0

以下代码不会产生任何错误,但它似乎也没有做任何事情。

我正在尝试将玩家的统计数据保存到数据库中,如果他/她已经在数据库中,则更新它们。表'playerstats'有列[名称、击杀、死亡、治疗、上限、连胜、mvps]。'name' 是主键。

执行后,表保持不变,就像什么都没发生一样。我究竟做错了什么?

for (String pName : pNames) {

    PlayerStats pStats = getStats(pName);

    PreparedStatement pstmt = null;
    String updateString = "INSERT INTO darkagedb.playerstats VALUES (?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE name = ?, kills = kills + ?, deaths = deaths + ?, heals = heals + ?, caps = caps + ?, streak = GREATEST(streak,?), mvps = mvps + ?";

    try {
        conn.setAutoCommit(false);

        pstmt = conn.prepareStatement(updateString);
        pstmt.setString(1, pStats.name);
        pstmt.setInt(2, pStats.kills);
        pstmt.setInt(3, pStats.deaths);
        pstmt.setInt(4, pStats.heals);
        pstmt.setInt(5, pStats.caps);
        pstmt.setInt(6, pStats.streak);
        pstmt.setInt(7, pStats.mvps);
        pstmt.setString(8, pStats.name);
        pstmt.setInt(9, pStats.kills);
        pstmt.setInt(10, pStats.deaths);
        pstmt.setInt(11, pStats.heals);
        pstmt.setInt(12, pStats.caps);
        pstmt.setInt(13, pStats.streak);
        pstmt.setInt(14, pStats.mvps);
        conn.commit();
        System.out.println("saved stats for " + pStats.name);
        saved++;

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
        if (pstmt != null)
            pstmt.close();
        conn.setAutoCommit(true);
        } catch (SQLException e) {
        e.printStackTrace();
    }
}


    }
4

1 回答 1

1
    pstmt.setInt(14, pStats.mvps);
    pstmt.executeUpdate(); // You have to add this line
    conn.commit();

目前,您根本没有告诉 Prepared Statement 做任何事情。必须执行它才能产生任何结果。

于 2013-05-12T16:13:39.320 回答