Solved Cannot Connect To Database

Discussion in 'Spigot Plugin Development' started by Dragon0617_, Dec 23, 2018.

  1. Hey guys I've been stuck on this for a couple hours and I cant make a perfect connection to the database. I did type my login right, but it still wont work.

    Code:
    Code (Text):

    package me.iceblizzard.mysql;

    import me.iceblizzard.file.ConfigManager;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;

    import java.sql.*;

    public class MySQLMain {

        private static MySQLMain instance = new MySQLMain();
        private Connection connection;

        public static MySQLMain getInstance() {
            return instance;
        }

        public void mysqlSetup() {
            String host = ConfigManager.getConfigManager().getPath("host");
           
            int port = ConfigManager.getConfigManager().getInt("port");

            String database = ConfigManager.getConfigManager().getPath("database");

            String username = ConfigManager.getConfigManager().getPath("username");

            String password = ConfigManager.getConfigManager().getPath("password");
           
            try {

                synchronized (this) {

                    if (getConnection() != null && !connection.isClosed()) {
                        return;
                    }

                    Class.forName("com.mysql.jdbc.Driver");

                    setConnection(DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" +
                            database, username, password));

                    createTable();

                    Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "MYSQL CONNECTED");
                }

            } catch (SQLException e) {

                e.printStackTrace();

            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }

        }
       
        public Connection getConnection() {
            return connection;

        }

        public void setConnection(Connection connection) {
            this.connection = connection;

        }
    }
     
    Error Log:
    Code (Text):
    [FancyHub] Enabling FancyHub v1.16
    [22:51:52 WARN]: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
    [22:51:52 WARN]:        at sun.reflect.GeneratedConstructorAccessor401.newInstance(Unknown Source)
    [22:51:52 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [22:51:52 WARN]:        at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [22:51:52 WARN]:        at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    [22:51:52 WARN]:        at com.mysql.jdbc.Util.getInstance(Util.java:387)
    [22:51:52 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    [22:51:52 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
    [22:51:52 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
    [22:51:52 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862)
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2331)
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084)
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    [22:51:52 WARN]:        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    [22:51:52 WARN]:        at sun.reflect.GeneratedConstructorAccessor398.newInstance(Unknown Source)
    [22:51:52 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    [22:51:52 WARN]:        at java.lang.reflect.Constructor.newInstance(Unknown Source)
    [22:51:52 WARN]:        at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    [22:51:52 WARN]:        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    [22:51:52 WARN]:        at java.sql.DriverManager.getConnection(Unknown Source)
    [22:51:52 WARN]:        at java.sql.DriverManager.getConnection(Unknown Source)
    [22:51:52 WARN]:        at me.iceblizzard.mysql.MySQLMain.mysqlSetup(MySQLMain.java:42)
    [22:51:52 WARN]:        at me.iceblizzard.main.FancyHub.onEnable(FancyHub.java:42)
    [22:51:52 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:292)
    [22:51:52 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
    [22:51:52 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405)
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugin(CraftServer.java:373)
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(CraftServer.java:323)
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.reload(CraftServer.java:747)
    [22:51:52 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:539)
    [22:51:52 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25)
    [22:51:52 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:647)
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchServerCommand(CraftServer.java:633)
    [22:51:52 WARN]:        at net.minecraft.server.v1_10_R1.DedicatedServer.aL(DedicatedServer.java:438)
    [22:51:52 WARN]:        at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:401)
    [22:51:52 WARN]:        at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668)
    [22:51:52 WARN]:        at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567)
    [22:51:52 WARN]:        at java.lang.Thread.run(Unknown Source)
    [22:51:52 WARN]: Caused by: java.lang.NullPointerException
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3004)
    [22:51:52 WARN]:        at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1908)
    [22:51:52 WARN]:        at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1837)
    [22:51:52 WARN]:        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1207)
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254)
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285)
    [22:51:52 WARN]:        ... 29 more
    All the help will be appreciated.
     
  2. try changing the setConnection line to
    Code (Java):
    Connection connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
    Also check if your mysql service is running
     
  3. Nope still getting the same error
     
  4. did you checked if your mysql service is running?, are you using windows or linux on the machine that has the mysql server
     
  5. Windows and my database is running it says:
    Screenshot (33).png
     
  6. make sure your mysql and mysql-connector-java are the same version on your pc
     
  7. The MySQL I'm running is 8.0
    The jar file is version 8.0.13
     
  8. what version is your mysql-connector-java
     
  9. mysql-connector-java-8.0.13
     
  10. make sure your mysql version is also 8.0.13
     
  11. They're both version 8.0.13
     
  12. There could be 2 issues here:

    1.) The config file isn't returning the correct path/string
    2.) Your MySQL server is bound to localhost and is not visible outside.
     
  13. Here's my config
    Code (Text):
    host: "localhost"
    port: 3306
    database: "test"
    username: "root"
    password: "password"
     
    Wdym isn't visible outside?
     
  14. Is there something that I have to change in the MySQL settings?
     
  15. Is the MySQL running on your computer?
     
  16. Yes it is running.
     
  17. Update: Now it says I can't connect:
    Screenshot (34).png
     
  18. Try using the static IP to connect and open the port
     
  19. How do you do that
     
  20. Might as well change databases since this one is being a pain in the ass. Anyone know of good MySQL databases I could use (that are free). I don't mind using MongoDB
     
    #20 Dragon0617_, Dec 24, 2018
    Last edited: Dec 24, 2018