本文介绍了无法在 MySQL 中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的 Bukkit 插件(对于 Minecraft)中,我创建了一种连接到数据库的方法,并且我能够从中检索信息(语句等),因此我不需要这方面的帮助,但我无法检索来自数据库的令牌,它连接并显示结果的一切,但当我尝试查询它时,我收到错误.

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.

@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

推荐答案

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

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

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

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.

这篇关于无法在 MySQL 中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 14:34