Stupid MYSQL question

Discussion in 'Plugin Development' started by beanonaboard, Apr 13, 2014.

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

    beanonaboard

    I think I am having a stupid moment but how can i return the in the int value from the count function?

    For some reason I thought it would return the int automatically?
    Code:java
    1. ResultSet score = statement.executeQuery("SELECT COUNT(*) FROM playerStats;");
    2.  
     
  2. Offline

    Timbo_KZ

    beanonaboard
    There you go:

    Code:java
    1. ResultSet score = statement.executeQuery("SELECT COUNT(*) AS score FROM playerStats;");
    2. int scoreInt = score.getInt("score");
     
  3. Offline

    beanonaboard


    thank you but when I do this I am getting an error in my console?

    Here is the stacktrace (I am not sure as I have not seen this error before, but line 73 is

    Code:java
    1. int scoreInt = score.getInt("LevelXP");
    2.  


    Code:
    [21:37:32 WARN]: java.sql.SQLException: Before start of result set
    [21:37:32 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
    ava:1073)
    [21:37:32 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
    ava:987)
    [21:37:32 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
    ava:982)
    [21:37:32 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
    ava:927)
    [21:37:32 WARN]:        at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImp
    l.java:841)
    [21:37:32 WARN]:        at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.jav
    a:2672)
    [21:37:32 WARN]:        at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.jav
    a:2813)
    [21:37:32 WARN]:        at me.beanonaboard.Leveling.MySQLManager.topPlayers(MySQ
    LManager.java:73)
    [21:37:32 WARN]:        at me.beanonaboard.Leveling.LevelingSystem.playerRank(Le
    velingSystem.java:124)
    [21:37:32 WARN]:        at me.beanonaboard.Leveling.Commands.onCommand(Commands.
    java:23)
    [21:37:32 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginComman
    d.java:44)
    [21:37:32 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:180)
    [21:37:32 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCo
    mmand(CraftServer.java:696)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.PlayerConnection.handleC
    ommand(PlayerConnection.java:953)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.PlayerConnection.a(Playe
    rConnection.java:815)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(Packe
    tPlayInChat.java:28)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(
    PacketPlayInChat.java:47)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.NetworkManager.a(Network
    Manager.java:148)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.ServerConnection.c(Sourc
    eFile:134)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.v(Minecr
    aftServer.java:667)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
    tedServer.java:260)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
    aftServer.java:558)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
    craftServer.java:469)
    [21:37:32 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.
    run(SourceFile:628)
    >
     
  4. Offline

    Timbo_KZ

    beanonaboard
    Post your full code. I want to be sure you're doing it right.
     
  5. Offline

    beanonaboard


    Sure here is the code I am using

    Code:java
    1. public int topPlayers(Player p) throws SQLException {
    2. if(!this.db.checkConnection()) {
    3. this.db.openConnection();
    4. }
    5.  
    6. Statement statement = this.db.getConnection().createStatement();
    7. ResultSet score = statement.executeQuery("SELECT COUNT(*) AS LevelXP FROM playerStats;");
    8. int scoreInt = score.getInt("LevelXP");
    9. return scoreInt;
    10. }
     
  6. Offline

    Timbo_KZ

    beanonaboard
    It looks like you need to call score.next() just to make sure it return true.

    Code:java
    1. public int topPlayers(Player p) throws SQLException {
    2. if(!this.db.checkConnection()) {
    3. this.db.openConnection();
    4. }
    5.  
    6. Statement statement = this.db.getConnection().createStatement();
    7. ResultSet score = statement.executeQuery("SELECT COUNT(*) AS LevelXP FROM playerStats;");
    8. int scoreInt;
    9. if(score.next())
    10. scoreInt = score.getInt("LevelXP");
    11. else
    12. scoreInt = 0;
    13. return scoreInt;
    14. }
     
    beanonaboard likes this.
  7. Offline

    beanonaboard


    Thank you wasn't sure what I was doing wrong!
     
Thread Status:
Not open for further replies.

Share This Page