Search code examples
javamysqlminecraftbukkit

JavaInvalid value for getInt()


I try to make a script for bukkit. It needs to go up 1 in the mysql table if it kills a zombie. Whats wrong? or is there maybe another method?

Error: java.sql.SQLException: Invalid value for getInt() - 'CraftZombie'

        PreparedStatement kills = connection.prepareStatement("SELECT ? FROM `data` WHERE name=?");
        kills.setString(1,entity.toString());
        kills.setString(2,name);
        ResultSet result = kills.executeQuery();


        String entity_string = entity.toString();

        result.next();
        //int score = 1;
        //score = result.getInt(1);
        //Integer score2 = (Integer) result.getObject(entity_string);

        int SC = result.getInt(entity_string);



        PreparedStatement stat = connection.prepareStatement("UPDATE data SET ?=? WHERE name=?",
                ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_UPDATABLE);
        stat.setString(1,entity.toString());
        stat.setInt(2, SC + 1);
        stat.setString(3,name);

Solution

  • First of all, don't do mysql query in the event, that make the server lag.

    You have to create hashmap with the data you want to link to mysql server and do a scheduler Async task that do the query.

    Example:

    package test;
    
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Map.Entry;
    import java.util.UUID;
    import java.util.concurrent.ConcurrentHashMap;
    
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.mysql.jdbc.Connection;
    
    public class Event extends JavaPlugin implements Listener {
    
        // Mysql update data
        public static ConcurrentHashMap<UUID, Integer> player_zombiekills = new ConcurrentHashMap<UUID, Integer>();
    
        // Mysql connection data
        public static Connection connection;
        private String host = "localhost";
        private String database = "DB";
        private String username = "user";
        private String password = "123";
        private int port = 3306;
    
        //Kill zombie event
        @EventHandler
        public void zombieDeath(EntityDeathEvent eve) {
            if (!(eve.getEntity() instanceof Zombie)) return; // Stop if death is not zombie
            if (!(eve.getEntity().getKiller() instanceof Player)) return; // Stop if killer is not player
            UUID uid = eve.getEntity().getKiller().getUniqueId(); // Save player uuid
            if (player_zombiekills.contains(uid)) player_zombiekills.put(uid, 0); // Set kills 0 if not exist in hashmap
            player_zombiekills.put(uid, player_zombiekills.get(uid) + 1); // plus 1 kills value
        }
    
        //On enable connection
        public void onEnable() {
            try {
                if (connection != null && !connection.isClosed()) return;
                Class.forName("com.mysql.jdbc.Driver");
                connection = (Connection) DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
                if (!connection.isClosed()) System.out.println("[MySQL] " + "Connected to '" + database + "'");
            } catch (Exception e) {
                System.err.println("[MySQL] " + e.getMessage());
            }
    
            new BukkitRunnable() {
                public void run() {
                    // for updates
                    for (Entry<UUID, Integer> ent : player_zombiekills.entrySet()) {
                        try {
                            // send update
                            connection.createStatement().executeUpdate("UPDATE `tablename` SET `zombiekills`=`zombiekills`+" + ent.getValue() + " WHERE `uuid`='" + ent.getKey().toString() + "'");
                            // Sucess remove entry from hashmap to reset kills
                            player_zombiekills.remove(ent.getKey());
                        } catch (SQLException e) { // If error
                            System.err.println("[MySQL] " + e.getMessage()); // Print error in console
                        }
                    }
                }
            }.runTaskTimerAsynchronously(this, 20, 20 * 60 * 10); // Run async loop every 10 minutes
        }
    }