无法设置MySQL默认值?

时间:2022-09-22 16:08:16

So in my Bukkit Plugin (For Minecraft) I have created a way to connect to the database and I am able to retrieve information from it, (Statements etc.) so I don't need help with that but I cannot retrieve the tokens from the database, it connects and everything to show results but when I try to query it I receive errors.

所以在我Bukkit插件(Minecraft)我已经创建了一个连接到数据库,我能够检索信息,(语句等),所以我不需要帮助,但是我不能从数据库获取令牌,它连接和显示结果的一切,但当我尝试查询它接收错误。

@EventHandler
public void onJoin(PlayerJoinEvent event) throws SQLException {
    Player player = event.getPlayer();
    String name = player.getName();
    Statement statement = connection.createStatement();
    ResultSet res = statement.executeQuery("SELECT * FROM tokens WHERE PlayerName = '" + name + "';");
    res.next();

    int tokens = 0;

    if (res.getString("PlayerName") == null) {
        tokens = 0;
    } else {
        tokens = res.getInt("tokens");
    }

    player.sendMessage(tokens + "");
} 

Stacktrace:

加:

org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PlayerList.c(PlayerList.java:225) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PlayerList.a(PlayerList.java:116) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.LoginListener.c(LoginListener.java:78) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.LoginListener.a(LoginListener.java:42) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:149) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
Caused by: java.sql.SQLException: Illegal operation on empty result set.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5616) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at com.Twipply.DandD.DandD.onJoin(DandD.java:37) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.6.0_65]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[?:1.6.0_65]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[?:1.6.0_65]
    at java.lang.reflect.Method.invoke(Method.java:597) ~[?:1.6.0_65]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    ... 14 more

1 个解决方案

#1


2  

You need to check the return value of ResultSet.next(). It returns false if there are no more rows.

您需要检查ResultSet.next()的返回值。如果没有更多行,它将返回false。

If a SQL query does not include any matches, you cannot call getString() or similar methods on the ResultSet. It is not that you have a result with all null fields -- you have no result at all.

如果SQL查询不包含任何匹配项,则不能在ResultSet上调用getString()或类似的方法。并不是所有的空字段都有结果——您根本没有结果。

#1


2  

You need to check the return value of ResultSet.next(). It returns false if there are no more rows.

您需要检查ResultSet.next()的返回值。如果没有更多行,它将返回false。

If a SQL query does not include any matches, you cannot call getString() or similar methods on the ResultSet. It is not that you have a result with all null fields -- you have no result at all.

如果SQL查询不包含任何匹配项,则不能在ResultSet上调用getString()或类似的方法。并不是所有的空字段都有结果——您根本没有结果。