Solved SQL connection to my own database error

Discussion in 'Plugin Development' started by calebbfmv, Nov 23, 2013.

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

    calebbfmv

    Code:
    23:26:57 [SEVERE] [SpaceWarz] Could not connect to MySQL server! because: Access
    denied for user 'root'@'localhost' (using password: NO)
    23:26:57 [SEVERE] [SpaceWarz] Could not connect to MySQL server! because: Access
    denied for user 'root'@'localhost' (using password: NO)
    23:26:57 [SEVERE] Error occurred while enabling SpaceWarz v0.5.6B (Is it up to d
    ate?)
    java.lang.NullPointerException
            at net.bhnetworks.spacewars.utils.MySQL.updateSQL(MySQL.java:139)
            at net.bhnetworks.spacewars.SpaceWars.connect(SpaceWars.java:41)
            at net.bhnetworks.spacewars.SpaceWars.onEnable(SpaceWars.java:25)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:382)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav
    a:288)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.
    java:270)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:61
    8)
            at org.bukkit.Bukkit.reload(Bukkit.java:277)
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    24)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe
    r.java:532)
            at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchServerCommand(Craf
    tServer.java:519)
            at net.minecraft.server.v1_6_R3.DedicatedServer.as(DedicatedServer.java:
    276)
            at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    41)
            at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    83)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :415)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    
    I get that error every time I am starting my serer with my plugin trying to connect to a database, and I have no idea whats causing it. File is set up like so:
    Code:
        user: root
        database: test
        host: localhost
        port: 3306
        password: -snip-
    
    SQL Class (thanks Husky)
    Code:java
    1. package net.bhnetworks.spacewars.utils;
    2.  
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.ResultSet;
    6. import java.sql.SQLException;
    7. import java.sql.Statement;
    8. import java.util.logging.Level;
    9.  
    10. import org.bukkit.plugin.Plugin;
    11.  
    12.  
    13. /**
    14. * Connects to and uses a MySQL database
    15. *
    16. * @author -_Husky_-
    17. * @author tips48
    18. */
    19. public class MySQL extends Database {
    20. private final String user;
    21. private final String database;
    22. private final String password;
    23. private final String port;
    24. private final String hostname;
    25.  
    26.  
    27. private Connection connection;
    28.  
    29.  
    30. /**
    31. * Creates a new MySQL instance
    32. *
    33. * @param plugin
    34. * Plugin instance
    35. * @param hostname
    36. * Name of the host
    37. * @param portnmbr
    38. * Port number
    39. * @param database
    40. * Database name
    41. * @param username
    42. * Username
    43. * @param password
    44. * Password
    45. */
    46. public MySQL(Plugin plugin, String hostname, String port, String database, String username, String password) {
    47. super(plugin);
    48. this.hostname = hostname;
    49. this.port = port;
    50. this.database = database;
    51. this.user = username;
    52. this.password = password;
    53. this.connection = null;
    54. }
    55.  
    56.  
    57. @Override
    58. public Connection openConnection() {
    59. try {
    60. Class.forName("com.mysql.jdbc.Driver");
    61. String statement = "CREATE DATABASE IF NOT EXISTS "+database;
    62. Connection conn = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port, user, password);
    63. Statement st = conn.createStatement();
    64. st.executeUpdate(statement);
    65. st.close();
    66. conn.close();
    67. connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
    68. } catch (SQLException e) {
    69. plugin.getLogger().log(Level.SEVERE, "Could not connect to MySQL server! because: " + e.getMessage());
    70. plugin.getLogger().log(Level.SEVERE, "JDBC Driver not found!");
    71. }
    72. return connection;
    73. }
    74.  
    75.  
    76. @Override
    77. public boolean checkConnection() {
    78. return connection != null;
    79. }
    80.  
    81.  
    82. @Override
    83. public Connection getConnection() {
    84. return connection;
    85. }
    86.  
    87.  
    88. @Override
    89. public void closeConnection() {
    90. if (connection != null) {
    91. try {
    92. connection.close();
    93. } catch (SQLException e) {
    94. plugin.getLogger().log(Level.SEVERE, "Error closing the MySQL Connection!");
    95. e.printStackTrace();
    96. }
    97. }
    98. }
    99.  
    100.  
    101. public ResultSet querySQL(String query) {
    102.  
    103.  
    104. Connection c = openConnection();
    105. Statement s = null;
    106.  
    107.  
    108. try {
    109. s = c.createStatement();
    110. } catch (SQLException e1) {
    111. e1.printStackTrace();
    112. }
    113.  
    114.  
    115. ResultSet ret = null;
    116.  
    117.  
    118. try {
    119. ret = s.executeQuery(query);
    120.  
    121. } catch (SQLException e) {
    122. e.printStackTrace();
    123. }
    124.  
    125.  
    126. return ret;
    127. }
    128.  
    129.  
    130. public void updateSQL(String update) {
    131.  
    132.  
    133. Connection c = openConnection();
    134. Statement s = null;
    135.  
    136.  
    137. try {
    138. s = c.createStatement();
    139. s.executeUpdate(update);
    140. } catch (SQLException e1) {
    141. e1.printStackTrace();
    142. }
    143.  
    144.  
    145. }
    146.  
    147.  
    148. }
    149.  
    150.  

    Any ideas?
     
  2. Offline

    1Rogue

    What are you extending that requires you to super the plugin up?

    Also, please include the full class. The line numbers are messed up with what you provided.
     
  3. Offline

    calebbfmv

    1Rogue
    Changed the OP, the weird thing is, it works for one plugin, but not another, any idea what causes this?
     
  4. Offline

    1Rogue

    copy/paste direct from the class, what you've shown isn't from the same version as the error is.
     
  5. Offline

    phillmac

    Check your MySQL server, what address is it listening on? if it is listening on anything other then 0.0.0.0 or 127.0.0.1 then you will have to use that address rather then localhost to connect.. Also check what privileges your user has - do you have an allow connections from address set that is an ip or something preventing you from connecting on localhost? also check you are using the right password, and finally using root is probably OK for testing, but not a good idea for production servers.
     
  6. Offline

    calebbfmv

    1Rogue phillmac
    I found the error:
    In my connect method:
    Code:
    String pass = config.getString("SpaceWars.MySQL.pass");
    In my setupConfig method:
    Code:
    config.addDefault("SpaceWars.MySQL.password"
    
    Simple little typo....
     
    1Rogue likes this.
  7. Offline

    1Rogue

    calebbfmv likes this.
Thread Status:
Not open for further replies.

Share This Page