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 (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()) {


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


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

            } catch (SQLException e) {


            }catch (ClassNotFoundException e){

        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(
    [22:51:52 WARN]:        at com.mysql.jdbc.Util.getInstance(
    [22:51:52 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(
    [22:51:52 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(
    [22:51:52 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(
    [22:51:52 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.createNewIO(
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.<init>(
    [22:51:52 WARN]:        at com.mysql.jdbc.JDBC4Connection.<init>(
    [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(
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.getInstance(
    [22:51:52 WARN]:        at com.mysql.jdbc.NonRegisteringDriver.connect(
    [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(
    [22:51:52 WARN]:        at me.iceblizzard.main.FancyHub.onEnable(
    [22:51:52 WARN]:        at
    [22:51:52 WARN]:        at
    [22:51:52 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugin(
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.enablePlugins(
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.reload(
    [22:51:52 WARN]:        at org.bukkit.Bukkit.reload(
    [22:51:52 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(
    [22:51:52 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(
    [22:51:52 WARN]:        at org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchServerCommand(
    [22:51:52 WARN]:        at net.minecraft.server.v1_10_R1.DedicatedServer.aL(
    [22:51:52 WARN]:        at net.minecraft.server.v1_10_R1.DedicatedServer.D(
    [22:51:52 WARN]:        at net.minecraft.server.v1_10_R1.MinecraftServer.C(
    [22:51:52 WARN]:        at
    [22:51:52 WARN]:        at Source)
    [22:51:52 WARN]: Caused by: java.lang.NullPointerException
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.getServerCharset(
    [22:51:52 WARN]:        at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(
    [22:51:52 WARN]:        at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(
    [22:51:52 WARN]:        at com.mysql.jdbc.MysqlIO.doHandshake(
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.coreConnect(
    [22:51:52 WARN]:        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(
    [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