Solved How can I get a value from a MySQL database?

Discussion in 'Plugin Development' started by xpaintall, Aug 28, 2021.

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

    xpaintall

    So basically I am trying to get the integer from a player in a mysql database so something like this:
    Code:
        public int getTokens(Player player) {
            try{
                PreparedStatement ps = f.SQL.getConnection().prepareStatement("SELECT TOKENS FROM Tokens WHERE UUID=?");
                ps.setString(1, player.getUniqueId().toString());
                ps.executeUpdate();
                ResultSet rs = ps.executeQuery();
                int points = 0;
                if(rs.next()) {
                    points = rs.getInt("TOKENS");
                    return points;
                }
                ps.executeUpdate();
            }catch (SQLException e) {
                e.printStackTrace();
            }
            return 0;
        }
    Now the code's completely fine, everything works but if I try to display how many tokens has the player got:
    Code:
            int tokens = f.SManager.getTokens(player);
            manameta.setDisplayName("┬žaCurrent Vote token balance: " + tokens);
    it just gives me this error:

    Code:
    [15:20:10 WARN]: java.sql.SQLException: Can not issue executeUpdate() for SELECTs
    [15:20:10 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    [15:20:10 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    [15:20:10 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    [15:20:10 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    [15:20:10 WARN]:        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2365)
    [15:20:10 WARN]:        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2325)
    [15:20:10 WARN]:        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2310)
    [15:20:10 WARN]:        at com.xpaintall.MctallMain.SQL.SQLManager.getTokens(SQLManager.java:133)
    [15:20:10 WARN]:        at com.xpaintall.MctallMain.Cosmetics.CosmeticGUIs.CosmeticBoxGUI_Listener.onClick(CosmeticBoxGUI_Listener.java:25)
    [15:20:10 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [15:20:10 WARN]:        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    [15:20:10 WARN]:        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [15:20:10 WARN]:        at java.lang.reflect.Method.invoke(Method.java:498)
    [15:20:10 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306)
    [15:20:10 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    [15:20:10 WARN]:        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502)
    [15:20:10 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13)
    [15:20:10 WARN]:        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    [15:20:10 WARN]:        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654)
    [15:20:10 WARN]:        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557)
    [15:20:10 WARN]:        at java.lang.Thread.run(Thread.java:748)
    The com.xpaintall.MctallMain.Cosmetics.CosmeticGUIs.CosmeticBoxGUI_Listener.onClick(CosmeticBoxGUI_Listener.java:25) is the thing I listed above
     
  2. Offline

    CraftCreeper6

    @xpaintall
    You are calling ps.executeUpdate(); before you do executeQuery();

    You can just delete the executeUpdate();.
     
  3. Offline

    xpaintall

    @CraftCreeper6 thanks, it works now! But I am just wondering, when I try to display them it just says that I have one token instead of multiple (I executed a command that will give me one token multiple times and it's not working).
     
  4. Offline

    CraftCreeper6

    @xpaintall
    Could you rephrase that? And maybe show some code too so I can understand better.
     
  5. Offline

    xpaintall

    Oh yeah, sorry I never really worked with MySQL. My problem now is that it seems like that the only number it is displaying (or setting) is 1. So basically whenever I try to execute the "/votereward <playername>" command, it will give the player a vote token. I executed the command 3 times and it should say that I have 3 tokens, but it says that I only have one.

    Code:
        public void addTokens(Player player, int tokens) {
            try{
                PreparedStatement ps = f.SQL.getConnection().prepareStatement("UPDATE Tokens SET TOKENS=? WHERE UUID=?");
                ps.setInt(1, tokens);
                ps.setString(2, player.getUniqueId().toString());
                ps.executeUpdate();
            }catch (SQLException e) {
                e.printStackTrace();
            }
        }
    In my votereward command it executes the addTokens(Player player, int Tokens) void but it doesn't do anything but just set it to 1.

    Code:
            if(cmd.getName().equalsIgnoreCase("votereward")) {
                if(sender.hasPermission("mctall.votereward")) {
                    try {
                        String playerName = args[0];
                        Player player = Bukkit.getPlayer(playerName);
                        player.sendMessage(ChatColor.AQUA + "You get a vote token for voting, thanks!");
                            f.SManager.addTokens(player, +1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + "You don't have the permission! Only consoles can access this command!");
                }
            }
    Edit: should I do
    Code:
    f.SManager.addTokens(player, f.SManager.getTokens(player)+1);
    ?

    Edit 2:
    that actually fixed it I am a dumbass
     
    Last edited: Aug 28, 2021
  6. Offline

    CraftCreeper6

    @xpaintall
    You can't just do +1 to add another token. You have to get the original value and add 1 to it.

    Yep.
     
Thread Status:
Not open for further replies.

Share This Page