有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Bukkit MySQL货币系统

我正试图通过mysql创建一个货币系统。无论何时玩家加入服务器,都会通过mysql创建个人资料,这一点完全没有缺陷。我很难让真正的货币系统只支持mysql。每当我试图给播放器添加钱时,它就会抛出一个sql错误,并破坏用户的mysql配置文件

我在mysql中有以下字段 UUID、姓名、比特(金钱)、禁令

这里是错误

        [14:03:51 INFO]: Crypted lost connection: Disconnected
[14:03:51 ERROR]: Could not pass event PlayerQuitEvent to KnoxHub v1.0
org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerList.disconnect(PlayerList.java:346) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:844) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.NetworkManager.l(NetworkManager.java:314) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.ServerConnection.c(ServerConnection.java:145) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:817) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
Caused by: java.sql.SQLException: Can not issue SELECT via executeUpdate().
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1621) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        at crypted.knoxhub.Libraries.MySQL.Database.updateSQL(Database.java:132) ~[?:?]
        at crypted.knoxhub.Libraries.Economy.Bits.getBits(Bits.java:37) ~[?:?]
        at crypted.knoxhub.Libraries.Economy.Bits.AddBits(Bits.java:50) ~[?:?]
        at crypted.knoxhub.Hub.Utilities.CreateUser.onJoin(CreateUser.java:41) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.7-R0.1-SNAPSHOT-latest.jar:git-Spigot-f928e7a-e91aed8]
        ... 12 more

比特(货币)类

public Bits(Core p, UUID u)
{
    this.u = u;
    this.plugin = p;
    this.MySQL = new PlayerSQL(plugin);
}


public int getBits() throws Exception
{

  int bits2 =  MySQL.sql.updateSQL("SELECT FROM `profiles` WHERE `UUID`= '" + u + "';");

    return bits2;
}

public void setBits( int amount ) throws Exception{

    MySQL.sql.updateSQL("UPDATE FROM `profiles` SET `Bits`= '" + amount + "' WHERE `UUID`='" + u + "'");

}

public void AddBits( int amount ) throws Exception{

    MySQL.sql.updateSQL("UPDATE FROM `profiles` SET `Bits`= '" + getBits() + amount  + "' WHERE `UUID`='" + u + "'");

}

public void TakeBits( int amount ) throws Exception {

    MySQL.sql.updateSQL("UPDATE FROM `profiles` SET `Bits`= '" + getBits() + -amount  + "' WHERE `UUID`='" + u + "'");

}

在PlayerQuit活动上增加资金

    @EventHandler
public void onJoin(PlayerQuitEvent e) throws Exception {
    Player player = e.getPlayer();
    Bits bits = new Bits(plugin, player.getUniqueId());
    bits.AddBits(5000); 
    Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Debug");

}

共 (1) 个答案

  1. # 1 楼答案

    错误信息显示

    Caused by: java.sql.SQLException: Can not issue SELECT via executeUpdate().

    所以,问题是不能将SELECT关键字与.executeUpdate一起使用。您需要使用.executeQuery,或者在您的情况下使用.execute。所以,你的getBits()方法应该是这样的

    public int getBits() throws Exception{
    
        int bits =  MySQL.sql.execute("SELECT FROM `profiles` WHERE `UUID`= '" + u + "';");
    
        return bits;
    }
    

    你还应该调查一下^{}。尽管这与错误无关,但在不使用PreparedStatement的情况下获取用户输入可能会导致SQL Injection,这让攻击者可以运行他们想要的任何查询

    例如,当用户对一款小型游戏给出反馈时,如果您接受了用户输入,例如,使用查询

    "INSERT INTO `feedback` (id, userid, feedback) VALUES (NULL, " +  userid + ", " + feedback + ");"
    

    用户只需输入

    'feedback'"); DROP TABLE `feedback`;

    这将导致两种说法

    INSERT INTO `feedback` (id, userid, feedback) VALUES (NULL, 'userid', 'feedback');

    DROP TABLE `feedback`