Solved Help with HashMaps/Databases?

Discussion in 'Plugin Development' started by Xp10d3, Dec 15, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    Xp10d3

    Alright, so I'm trying to use a HashMap to change a value of my database since I don't know much about MySQL. I get no errors and when I do the command everything is fine except it doesn't add the value to the database. I have no clue what I'm doing wrong and it's super confusing. The problem is specifically in my commands class and this try-catch statement right here:
    Code:
                            int argumentTwo = Integer.parseInt(args[1]);
                            try {
                                PreparedStatement statement1 = core.getConnection()
                                        .prepareStatement("SELECT " + core.table + " SET GOLD=? WHERE UUID=?");
                                ResultSet results = statement1.executeQuery();
                                results.next();
                                statement1.setInt(1, results.getInt("GOLD") + argumentTwo);
                                statement1.setString(2, target.getUniqueId().toString());
                                target.sendMessage(ChatColor.GREEN + "You have recieved " + argumentTwo + " gold!");
                                player.sendMessage("Gave " + target + argumentTwo + " gold.");
                                statement1.executeUpdate();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
    
    (I already set the HashMap which is this: final HashMap<UUID, Integer> coins = new HashMap<>();)

    Commands Class:
    Code:
    package corelia.koc.main;
    
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor {
      
        public Commands(Core core) {
            this.core = core;
            Bukkit.getPluginCommand("add").setExecutor(this);
        }
      
        final HashMap<UUID, Integer> coins = new HashMap<>();
      
        private Core core;
      
        // For entering data use: https://bukkit.org/threads/mysql-update-column.243601/
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
            Player player = (Player) sender;
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to access this command!");
            }
            if (cmd.getName().equalsIgnoreCase("add")) {
                if (args.length == 2) {
                    Player target = Bukkit.getServer().getPlayer(args[0]);
                    if (target == null) {
                        player.sendMessage(ChatColor.RED + "That player isn't online!");
                    } else {
                        if (isInt(args[1]) == true) {
                            /*
                            try {
                                PreparedStatement statement = core.getConnection().prepareStatement("SELECT * FROM " + core.table + " WHERE UUID=?");
                                statement.setString(1, uuid.toString());
                                ResultSet results = statement.executeQuery();
                                results.next();
                              
                                int argumentTwo = Integer.parseInt(args[1]);
                              
                                //coins.put(target.getUniqueId(), results.getInt("GOLD") + argumentTwo);
                                results.next();
                                player.sendMessage("Gave " + target + argumentTwo +" gold.");
                                target.sendMessage(ChatColor.GREEN + "You have recieved " + argumentTwo + " gold.");
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                            */
                          
                            // Note: Set ResultSet to 0 NOT 1. That is likely the error.
                            int argumentTwo = Integer.parseInt(args[1]);
                            try {
                                PreparedStatement statement1 = core.getConnection()
                                        .prepareStatement("SELECT " + core.table + " SET GOLD=? WHERE UUID=?");
                                ResultSet results = statement1.executeQuery();
                                results.next();
                                statement1.setInt(1, results.getInt("GOLD") + argumentTwo);
                                statement1.setString(2, target.getUniqueId().toString());
                                target.sendMessage(ChatColor.GREEN + "You have recieved " + argumentTwo + " gold!");
                                player.sendMessage("Gave " + target + argumentTwo + " gold.");
                                statement1.executeUpdate();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        } else {
                            player.sendMessage(ChatColor.RED + "Please use an integer. Example: /pay Xp10d3 50");
                        }
                    }
                } else {
                    player.sendMessage(ChatColor.RED + "Too few arguments! Correct usage: /pay <player> <amount>");
                }
            }
            return false;
        }
      
        public boolean isInt(String s) {
            try {
                Integer.parseInt(s);
                return true;
            } catch (NumberFormatException e) {
                return false;
            }
        }
    }
    
    
    Core Class:
    Code:
    package corelia.koc.main;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin implements Listener {
      
        // MySQL/Database stuff.
        private Connection connection;
        public String host, database, username, password, table;
        public int port;
      
        // Gets the config value.
        FileConfiguration config = getConfig();
    
        // When the server starts up, create the custom config, load the config, setup the MySQL stuff,
        // and register the MySQLSetterGetter class.
        public void onEnable() {
            loadConfig();
            mysqlSetup();
          
            new Commands(this);
          
            this.getServer().getPluginManager().registerEvents(new MysqlSetterGetter(), this);
        }
      
        // Loads the config (default one).
        public void loadConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    
        // Connects to the config to get the host, port, database name, username, password, and table.
        // Also sets up the MySQL connection.
        public void mysqlSetup() {
            host = this.getConfig().getString("host");
            port = this.getConfig().getInt("port");
            database = this.getConfig().getString("database");
            username = this.getConfig().getString("username");
            password = this.getConfig().getString("password");
            table = this.getConfig().getString("table");
    
            try {
    
                synchronized (this) {
                    if (getConnection() != null && !getConnection().isClosed()) {
                        return;
                    }
    
                    Class.forName("com.mysql.jdbc.Driver");
                    setConnection(
                            DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database,
                                    this.username, this.password));
    
                    Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL CONNECTED");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        // Gets the connection.
        public Connection getConnection() {
            return connection;
        }
    
        // Sets the connection.
        public void setConnection(Connection connection) {
            this.connection = connection;
        }
    
    }
    
    I also get the error that no value is specified for parameter 1.
     
    Last edited: Dec 17, 2019
Thread Status:
Not open for further replies.

Share This Page