MySQL warning errors

Discussion in 'Plugin Development' started by Kiaeyi, Mar 22, 2014.

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

    Kiaeyi

    Hey all.

    I would just like to thank those who have taken their time to read and solve my problems, it really mean a lot to me. I am currently coding a custom economy plugin, but I've met with some warnings (not errors but warnings, although I want to get rid of them).

    Here's the error log:
    Code:
    [19:42:39] [Server thread/INFO]: Kiaeyi issued server command: /kc amount kiaeyi
    [19:42:39] [Server thread/INFO]: SQL: Query Executed.
    [19:42:39] [Server thread/WARN]: java.sql.SQLException: Before start of result set
    [19:42:39] [Server thread/WARN]:    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    [19:42:39] [Server thread/WARN]:    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
    [19:42:39] [Server thread/WARN]:    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
    [19:42:39] [Server thread/WARN]:    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    [19:42:39] [Server thread/WARN]:    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
    [19:42:39] [Server thread/WARN]:    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5656)
    [19:42:39] [Server thread/WARN]:    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5576)
    [19:42:39] [Server thread/WARN]:    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5616)
    [19:42:39] [Server thread/WARN]:    at m.kiaeyi.currency.main.onCommand(main.java:31)
    [19:42:39] [Server thread/WARN]:    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [19:42:39] [Server thread/WARN]:    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175)
    [19:42:39] [Server thread/WARN]:    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:952)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457)
    [19:42:39] [Server thread/WARN]:    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617)
    [19:43:18] [Server thread/INFO]: Kiaeyi lost connection: Disconnected
    [19:43:18] [Server thread/INFO]: Kiaeyi left the game.
    Here's my main class:
    Code:java
    1. package m.kiaeyi.currency;
    2.  
    3. import java.sql.ResultSet;
    4. import java.sql.SQLException;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class main extends JavaPlugin {
    13.  
    14. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    15. if (sender instanceof Player) {
    16. if (cmd.getName().equalsIgnoreCase("kc")){
    17. if (args[0].equalsIgnoreCase("amount")){
    18. QueryManager qm = new QueryManager("root","","localhost","minecraft_currency",3306);{
    19. String playername = args[1];
    20. if(qm.query("SELECT money FROM test1 WHERE username='" + playername + "'")){
    21. String toMessage="";
    22. ResultSet rs = qm.getResults();
    23. try {
    24. rs.beforeFirst();
    25. } catch (SQLException e1) {
    26. // TODO Auto-generated catch block
    27. e1.printStackTrace();
    28. }
    29. while (true) {
    30. try {
    31. toMessage += rs.getString("money");
    32. } catch (SQLException e) {
    33. // TODO Auto-generated catch block
    34. e.printStackTrace();
    35. }
    36. try {
    37. if (rs.next()) {
    38. toMessage +="";
    39. } else {
    40. break;
    41. }
    42. } catch (SQLException e) {
    43. // TODO Auto-generated catch block
    44. e.printStackTrace();
    45. }
    46. }
    47. sender.sendMessage(ChatColor.GOLD.toString() + ChatColor.BOLD + "---------- " + "Player" + " information" + " ----------");
    48. sender.sendMessage(" ");
    49. sender.sendMessage(ChatColor.YELLOW.toString() + "Player Name: " + playername);
    50. sender.sendMessage(ChatColor.YELLOW.toString() + "Side: ");
    51. sender.sendMessage(ChatColor.YELLOW.toString() + "Class: ");
    52. sender.sendMessage(ChatColor.YELLOW.toString() + "Rank: ");
    53. sender.sendMessage(ChatColor.YELLOW.toString() + "Last seen on: ");
    54. sender.sendMessage(ChatColor.YELLOW.toString() + "Total Kills: ");
    55. sender.sendMessage(ChatColor.YELLOW.toString() + "Total Deaths: ");
    56. sender.sendMessage(ChatColor.YELLOW.toString() + "Total Money: " + toMessage);
    57. sender.sendMessage(ChatColor.GOLD.toString() + ChatColor.BOLD + "---------- " + ChatColor.RED + "End of Information" + ChatColor.GOLD.toString() + ChatColor.BOLD + "---------- ");
    58. }
    59. }
    60. }
    61.  
    62. }
    63. }
    64. return false;
    65. }
    66. }
    67.  


    Here's my QueryManager class:
    Code:java
    1. package m.kiaeyi.currency;
    2.  
    3. import java.sql.DriverManager;
    4. import java.sql.ResultSet;
    5. import java.sql.SQLException;
    6. import java.sql.Connection;
    7.  
    8. public class QueryManager {
    9.  
    10. protected Connection connection;
    11. protected ResultSet queryReturn = null;
    12.  
    13. public QueryManager(String username, String password, String host, String name, int port){
    14. // Initialize a connection
    15. try {
    16. connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + name, username, password);
    17. } catch (SQLException e) {
    18. e.printStackTrace();
    19. }
    20. // Null out the variables to prevent reading from external sources
    21. username = "";
    22. password = "";
    23. name = "";
    24. host = "";
    25. port = 0;
    26. }
    27.  
    28. public boolean query(String query){
    29. if(connection != null){
    30. try {
    31. queryReturn = connection.prepareStatement(query).executeQuery();
    32. System.out.println("SQL: Query Executed.");
    33. return true;
    34. } catch (SQLException e){
    35. System.out.println("--------------------");
    36. System.out.println("SQL ERROR: Could not execute query.");
    37. System.out.println("SQL ERROR: Caused by " + e.getCause().toString());
    38. System.out.println("SQL ERROR: Exception Details-");
    39. e.printStackTrace();
    40. System.out.println("SQL ERROR: Could not execute query.");
    41. System.out.println("--------------------");
    42. return false;
    43. }
    44. }
    45. else {
    46. System.out.println("SQL ERROR: No valid connection.");
    47. return false;
    48. }
    49. }
    50. public ResultSet getResults(){
    51. return queryReturn;
    52. }
    53. public void close(){
    54. if(connection != null){
    55. try {
    56. connection.close();
    57. System.out.println("SQL: Connection Closed.");
    58. } catch (SQLException e) {
    59. e.printStackTrace();
    60. }
    61. }
    62. else {
    63. System.out.println("SQL ERROR: No valid connection.");
    64. }
    65. }
    66.  
    67.  
    68. }
    69.  


    I'm using the resource here http://forums.bukkit.org/threads/sql-query-handler.246391/.

    Thank you :D
     
    MRPS likes this.
  2. Offline

    MRPS

    Kiaeyi
    You are trying to do another query without first using .close() on your result set it looks like. In your try-catch-finally block, store the extracted value that you needed from the ResultSet into a variable then use rs.close() so you can get another ResultSet from query()

    Also, consider moving your SQL queries off of the main thread.

    Also, you might be better off with an SQLite solution if you're not using an externally hosted database. When I wrote that QueryManager class, it was with external databases in mind not localhost databases.
     
    Kiaeyi likes this.
  3. Offline

    hexaan

    Kiaeyi
    beforeFirst(); "Moves the cursor to the front of this ResultSet object, just before the first row."
    This means that the resultset is not pointing to a row, but right before one. You cannot call rs.getString("money"); like you are doing in line 31.

    You would have to use rs.next(); to make sure it will point to the first row.

    Also you while (true) is something you should never do when programming, unless you have a way of breaking that loop. In this case you might want to use while(rs.next()){ }

    Code:java
    1. ResultSet rs = qm.getResults();
    2. try {
    3. rs.beforeFirst();
    4. } catch (SQLException e1) {
    5. e1.printStackTrace();
    6. }
    7. while (true) {
    8. try {
    9. toMessage += rs.getString("money");
    10. } catch (SQLException e) {
    11. e.printStackTrace();
    12. }
    13. }
     
Thread Status:
Not open for further replies.

Share This Page