Problem with a MySQL Connection

Discussion in 'Plugin Development' started by Build_and_Break, May 26, 2014.

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

    Build_and_Break

    Hello,

    It seems I'm getting errors in my code with a MySQL connection on line 71. I know the credentials are correct and the MySQL server is running.

    Errors: [link]
    Code: [link]

    In the error link, I was using CB2918, and no other version I've tried has made any difference.
     
  2. Offline

    Lone_MisfitMC

    Could it be the plugin variable? Not yet initialized?

    Lone
     
  3. Offline

    Obnoxious_Ninja

  4. Offline

    Build_and_Break

    What do you mean the plugin variable? It has to have been initialized for it to even run the onEnable section. I'm going to try and put it in a private void and run it on enable.

    Lone_MisfitMC Wait, you may be right. It wasn't in the on enable section

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  5. Offline

    Europia79

    Build_and_Break
    i read your code before I look at the error and i correctly guessed the problem:
    Code:
    at code.husky.mysql.MySQL.openConnection(MySQL.java:61)
    This is some old code that I have that works... I need to go back and update it whenever I get time, but it might help you with your problem connecting to the database:

    Code:java
    1. import com.vexsoftware.votifier.model.Vote;
    2. import com.vexsoftware.votifier.model.VoteListener;
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8. import java.sql.Statement;
    9. import java.util.Date;
    10. import java.util.logging.Level;
    11. import java.util.logging.Logger;
    12. import org.bukkit.Bukkit;
    13.  
    14. /**
    15. *
    16. * @author Nikolai Kalashnikov (Europia79)
    17. * [email][email protected][/email]
    18. *
    19. * Tested on Jan 16th, 2014 using
    20. * [url]https://minestatus.net/votifier/test[/url]
    21. *
    22. */
    23. public class MailboxVoteListener implements VoteListener {
    24.  
    25. // 264 = diamonds
    26. private int rewardId = 264;
    27. private int rewardqty = 2;
    28. private String voter = null;
    29.  
    30. private String host = "localhost";
    31. private String port = "3306";
    32. private String database = "iconomy";
    33. private String user = "iconomy";
    34. private String password = "mypass";
    35.  
    36. private Connection connect = null;
    37. private PreparedStatement pstat = null;
    38. private ResultSet resultSet = null;
    39.  
    40. @Override
    41. public void voteMade(Vote vote) {
    42.  
    43. voter = vote.getUsername();
    44.  
    45. try {
    46.  
    47. Class.forName("com.mysql.jdbc.Driver");
    48.  
    49. connect = DriverManager
    50. .getConnection("jdbc:mysql://"
    51. + host + ":"
    52. + port + "/"
    53. + database + "?"
    54. + "user=" + user + "&"
    55. + "password=" + password);
    56.  
    57. pstat = connect.prepareStatement("INSERT INTO `WA_Items` (`playerName`, `itemId`, `qty`) VALUES (?, ?, ?)");
    58. pstat.setString(1, voter);
    59. pstat.setInt(2, rewardId);
    60. pstat.setInt(3, rewardqty);
    61. pstat.executeUpdate();
    62.  
    63. } catch (Exception ex) {
    64.  
    65. Logger.getLogger(MailboxVoteListener.class.getName()).log(Level.SEVERE, null, ex);
    66.  
    67. } finally {
    68. close();
    69. }
    70.  
    71. }
    72.  
    73. private void close() {
    74. try {
    75. if (resultSet != null) {
    76. resultSet.close();
    77. }
    78.  
    79. if (pstat != null) {
    80. pstat.close();
    81. }
    82.  
    83. if (connect != null) {
    84. connect.close();
    85. }
    86. } catch (Exception ex) {
    87.  
    88. Logger.getLogger(MailboxVoteListener.class.getName()).log(Level.SEVERE, null, ex);
    89.  
    90. }
    91. }
    92.  
    93. }
    94.  
    95.  


    Only reason why I'm mentioning this is because I'm using a different url driver than the top answer on stackoverflow, but I know mine works.

    The top answer on stackoverflow has
    Code:
    Class.forName("org.gjt.mm.mysql.Driver");
    I have (and also the tutorial links have)
    Code:
    Class.forName("com.mysql.jdbc.Driver");
    Read up on the tutorials in the stackoverflow answers. (Since we don't have your MySQL.java code to see what's wrong).

    Good luck.
     
  6. Offline

    Build_and_Break

    Nevermind, that was in the correct spot in onEnable.

    Here's the link to all the MySQL code it's using: https://github.com/TacticalCactupi/LW/tree/master/src/code

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  7. Offline

    rsod

    Yes, plugin seems to be null. Also you should handle mysql queries asyncronously.
     
  8. Offline

    Europia79

    rsod

    Yeah, when I get time, I gotta go back and change my MailboxVoteListener, but I'm not too sure that his plugin variable is null because it's never used inside his MySQL.java. He passes all needed parameters to connect to the database in the constructor.

    Build_and_Break

    I'm not sure that code corresponds to the stack trace ? The line numbers seem to be off.

    For the code that you're running and testing... I'm curious what's on line 61 of MySQL.java

    For whatever server you're testing this on, you might need the mysql.jar inside your /lib/ directory
    /server/lib/mysql.jar
    /server/plugins/your.jar
    /server/world/
    /server/craftbukkit.jar
    /server/server.properties
     
  9. Offline

    1Rogue

    This is your basic setup:

    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. public static Main plugin;
    4.  
    5. public void onEnable() {
    6. MySQL sql = new MySQL(plugin, /*other fields*/);
    7. Connection c = sql.openConnection();
    8. }
    9.  
    10. }


    The problem is that your "plugin" variable is never intialized, and thus holds a null reference. You either need to set that value, or pass your class instance:

    Code:java
    1. plugin = this;
    2. MySQL sql = new MySQL(plugin, /*other fields*/);
    3. //or
    4. MySQL sql = new MySQL(this, /*other fields*/);


    I would also highly advise against using a public static plugin field, you should be passing instances, not accessing a public field which can easily be set somewhere else, breaking your plugin.
     
  10. Offline

    Build_and_Break

    Alright, after just changing it to "this", it seems I've got errors with authentication.

    http://hastebin.com/worosaqida.vbs

    Again, the MySQL connection credentials were correct, it was online and working. I even made another account and tried that one but I got the SAME errors.

    Thanks in advance,

    -Build
     
Thread Status:
Not open for further replies.

Share This Page