无法在MySQL中设置默认值

时间:2014-04-06 03:36:52

标签: java mysql

所以在我的Bukkit插件(For 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 + "");
} 

堆栈跟踪:

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 个答案:

答案 0 :(得分:2)

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

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