MySQL Error **Please Help**

Discussion in 'Plugin Development' started by RDNachoz, Oct 25, 2013.

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

    RDNachoz

    Code:java
    1. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    2. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:415)
    3. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:483)
    4. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
    5. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:594)
    6. 25.10 09:51:49 [Server] SEVERE at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:132)
    7. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    8. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:117)
    9. 25.10 09:51:49 [Server] SEVERE at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:230)
    10. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
    11. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:838)
    12. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:897)
    13. 25.10 09:51:49 [Server] SEVERE at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:986)
    14. 25.10 09:51:49 [Server] SEVERE at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:532)
    15. 25.10 09:51:49 [Server] SEVERE at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
    16. 25.10 09:51:49 [Server] SEVERE at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    17. 25.10 09:51:49 [Server] SEVERE at me.rdnachoz.main.Commands.onCommand(Commands.java:104)
    18. 25.10 09:51:49 [Server] SEVERE at me.rdnachoz.main.Commands.addPoints(Commands.java:125)
    19. 25.10 09:51:49 [Server] SEVERE at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5616)
    20. 25.10 09:51:49 [Server] SEVERE at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576)
    21. 25.10 09:51:49 [Server] SEVERE at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656)
    22. 25.10 09:51:49 [Server] SEVERE at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
    23. 25.10 09:51:49 [Server] SEVERE at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    24. 25.10 09:51:49 [Server] SEVERE at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    25. 25.10 09:51:49 [Server] SEVERE at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    26. 25.10 09:51:49 [Server] SEVERE at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    27. 25.10 09:51:49 [Server] SEVERE java.sql.SQLException: Illegal operation on empty result set.

    Pleas help! The main (bottom) error comes from this method:
     
  2. What you need to do is replace "res.next()" with
    if(res.next()){

    }
    that makes sure that the result set isn't empty before it performs any mysql operations with it.
    You can just put the rest of the code after res.next() in that if statement.
     
  3. Offline

    1Rogue

    Code:java
    1. s = Main.c.createStatement();
    2. res = s.executeQuery("SELECT * FROM tableName WHERE player = '" + p + "'");
    3. res.next();
    4. if(res.getString("player") != null){
    5. totalpoints = res.getInt("tokens") + toAdd;
    6. s.executeUpdate("UPDATE tableName SET tokens='" + totalpoints + "' WHERE player='" + p + "'");
    7. System.out.println("You have " + totalpoints + "tokens!");
    8. }


    You do res.next(), but do not check the return value (which would tell you if there is a next value):

    Code:java
    1. s = Main.c.createStatement();
    2. res = s.executeQuery("SELECT * FROM tableName WHERE player = '" + p + "'");
    3. if (res.next()) {
    4. if(res.getString("player") != null){
    5. totalpoints = res.getInt("tokens") + toAdd;
    6. s.executeUpdate("UPDATE tableName SET tokens='" + totalpoints + "' WHERE player='" + p + "'");
    7. System.out.println("You have " + totalpoints + "tokens!");
    8. }
    9. }


    Also, this can all be done with a single query:

    Code:java
    1. s = Main.c.createStatement();
    2. res = s.executeUpdate("UPDATE `tableName` SET `tokens`=`tokens` + " + Integer.parseInt(toAdd) + " WHERE `player`='" + p + "' AND @value := `tokens`; SELECT @value;");
    3. if (res.next()) {
    4. System.out.println("You have " + res.getInt(0) + " tokens!");
    5. }


    Also, you should look into using Prepared Statements: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
     
  4. Offline

    RDNachoz

    Ok thanks! Now, on my JoinEvent for creating the tables I am getting this error:
    Code:java
    1. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    2. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:415)
    3. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:483)
    4. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
    5. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:594)
    6. 25.10 10:35:03 [Server] SEVERE at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:139)
    7. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.PendingConnection.d(PendingConnection.java:48)
    8. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.PendingConnection.e(PendingConnection.java:138)
    9. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.PlayerList.a(PlayerList.java:103)
    10. 25.10 10:35:03 [Server] SEVERE at net.minecraft.server.v1_6_R3.PlayerList.c(PlayerList.java:207)
    11. 25.10 10:35:03 [Server] SEVERE at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:463)
    12. 25.10 10:35:03 [Server] SEVERE at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:478)
    13. 25.10 10:35:03 [Server] SEVERE at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
    14. 25.10 10:35:03 [Server] SEVERE at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    15. 25.10 10:35:03 [Server] SEVERE at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    16. 25.10 10:35:03 [Server] SEVERE at java.lang.reflect.Method.invoke(Unknown Source)
    17. 25.10 10:35:03 [Server] SEVERE at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    18. 25.10 10:35:03 [Server] SEVERE at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    19. 25.10 10:35:03 [Server] SEVERE at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    20. 25.10 10:35:03 [Server] SEVERE at me.rdnachoz.listeners.Join.onPlayerJoin(Join.java:51)
    21. 25.10 10:35:03 [Server] SEVERE at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581)
    22. 25.10 10:35:03 [Server] SEVERE at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1662)
    23. 25.10 10:35:03 [Server] SEVERE at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)
    24. 25.10 10:35:03 [Server] SEVERE at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
    25. 25.10 10:35:03 [Server] SEVERE at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
    26. 25.10 10:35:03 [Server] SEVERE at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
    27. 25.10 10:35:03 [Server] SEVERE at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
    28. 25.10 10:35:03 [Server] SEVERE at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    29. 25.10 10:35:03 [Server] SEVERE java.sql.SQLException: Column count doesn't match value count at row 1


    And here is the line/s that it's on:

    Code:java
    1. if(!MySQL.isPlayerRegistered(event.getPlayer())){
    2. Statement s = null;
    3. try{
    4. s = Main.c.createStatement();
    5. s.executeUpdate("INSERT INTO tableName(`player`, `tokens`) VALUES ('" + event.getPlayer().getName() + "', '0', '0', '0', '0')");
    6. }catch(SQLException ex){
    7. ex.printStackTrace();
    8. }
    9. }
     
  5. Offline

    1Rogue

    You stated in your query you were going to update two values:

    Code:sql
    1. INSERT INTO tableName(`player`, `tokens`) VALUES /* ... */


    But then you supply 5 arguments:

    Code:java
    1. /* ... */ ('name', '0', '0', '0', '0')


    Just update the token value:

    Code:sql
    1. INSERT INTO `tableName` (`player`, `tokens`) VALUES ('name' , 0)
     
  6. Offline

    RDNachoz

    Thanks for the help! All working now :D
     
Thread Status:
Not open for further replies.

Share This Page