从我的代码中获取控制台中的一些错误

时间:2016-11-02 00:55:55

标签: mysql bukkit

我正在编写一个stats插件,它在加入时,从数据库中检索统计数据,将它们实现为哈希映射,并且在退出时它将这些新的统计数据实现到数据库中,原因是没有这样做。与每秒打开连接一样多延迟。

如果这意味着什么,我正在使用MariaDB。

下面的HashMaps

public static HashMap<String, Integer> kills = new HashMap<String, Integer>();
public static HashMap<String, Integer> deaths = new HashMap<String, Integer>(); 

以下代码

   @EventHandler
public void onDeath(PlayerDeathEvent event){
    Player p = event.getEntity();
    Player k = p.getKiller();
    kills.put(k.getName(), kills.get(k.getName() + 1));
    deaths.put(p.getName(), deaths.get(p.getName() + 1));
    removeScoreboard(p);
    setScoreboard(p);
    removeScoreboard(k);
    setScoreboard(k);
}

@EventHandler
public void onJoin(PlayerJoinEvent e){
    OfflinePlayer p2 = e.getPlayer();
    Player p = e.getPlayer();
    openConnection();
    try{
        int previousDeaths = 0;
        int previousKills = 0;

        if(playerDataContainsPlayer(p)){
            PreparedStatement sql = connection.prepareStatement("SELECT deaths FROM `player_data` WHERE player=?;");
            PreparedStatement sql2 = connection.prepareStatement("SELECT kills FROM `player_data` WHERE player=?;");

            ResultSet result = sql.executeQuery();
            ResultSet result2 = sql2.executeQuery();
            result2.next();
            result.next();

            previousDeaths = result.getInt("deaths");
            previousKills = result2.getInt("kills");

            kills.put(p.getName(), previousKills);
            deaths.put(p.getName(), previousDeaths);

            sql.close();
            sql2.close();
            result.close();
            result2.close();
            removeScoreboard(p);
            setScoreboard(p);
        } else {
            PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `player_data` values(?,0,0);");
            newPlayer.setString(1, p.getUniqueId().toString());
            newPlayer.setInt(2, 0);
            newPlayer.setInt(3, 0);
            newPlayer.execute();
            newPlayer.close();
            kills.put(p.getName(), 0);
            deaths.put(p.getName(), 0);
            removeScoreboard(p);
            setScoreboard(p);
        }

    }catch(Exception e1){
        e1.printStackTrace();
    } finally{
        closeConnection();
    }
}

@EventHandler
public void onLeave(PlayerQuitEvent e){
    Player p = e.getPlayer();
    openConnection();
    try{
        int previousDeaths = 0;
        int previousKills = 0;

        if(playerDataContainsPlayer(p)){
            PreparedStatement sql = connection.prepareStatement("SELECT deaths FROM `player_data` WHERE player=?;");
            PreparedStatement sql2 = connection.prepareStatement("SELECT kills FROM `player_data` WHERE player=?;");
            sql.setString(1, p.getUniqueId().toString());
            sql2.setString(1, p.getUniqueId().toString());

            ResultSet result = sql.executeQuery();
            ResultSet result2 = sql2.executeQuery();
            result2.next();
            result.next();

            previousDeaths = result.getInt("deaths");
            previousKills = result2.getInt("kills");

            PreparedStatement killsUpdate = connection.prepareStatement("UPDATE `player_data` SET kills=? WHERE player=?;");
            killsUpdate.setInt(1, kills.get(p.getName()));
            killsUpdate.setString(2, p.getUniqueId().toString());
            killsUpdate.executeUpdate();

            PreparedStatement deathsUpdate = connection.prepareStatement("UPDATE `player_data` SET deaths=? WHERE player=?;");
            deathsUpdate.setInt(1, deaths.get(p.getName()));
            deathsUpdate.setString(2, p.getUniqueId().toString());
            deathsUpdate.executeUpdate();

            deathsUpdate.close();
            killsUpdate.close();
            sql.close();
            sql2.close();
            result.close();
            result2.close();
        }

    }catch(Exception e1){
        e1.printStackTrace();
    } finally{
        closeConnection();
    }
}

每当你加入时,它会说[20:51:43 WARN]:java.sql.SQLException:没有为参数1指定值

每当你杀死某人或死亡时,你的杀戮就会失效,你的死亡就会变成无效。我不确定为什么会这样,任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:2)

此处您尚未将PreparedStatement的参数设置为where condition。

PreparedStatement sql = connection.prepareStatement("SELECT deaths FROM `player_data` WHERE player=?");
PreparedStatement sql2 = connection.prepareStatement("SELECT kills FROM `player_data` WHERE player=?");

根据条件数据类型设置参数。 例如:preparedStatement.setInt(1,1001);

请参阅:https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

第二个问题:

请设置hashmap值如下

        previousDeaths = result.getInt("deaths");
        previousKills = result2.getInt("kills");

        kills.put(p.getName(), previousKills);
        deaths.put(p.getName(), previousDeaths);

        PreparedStatement killsUpdate = connection.prepareStatement("UPDATE `player_data` SET kills=? WHERE player=?;");
        killsUpdate.setInt(1, kills.get(p.getName()));
        killsUpdate.setString(2, p.getUniqueId().toString());
        killsUpdate.executeUpdate();