MySQL not connecting

Discussion in 'Spigot Plugin Development' started by FiXed, May 31, 2016.

  1. So I've made a bit of a MySQL class and when I try to load it, it doesn't seem to connect. This is my entire MySQL class:
    Code (Text):
    package net.emenbee.abilities.mysql;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import org.bukkit.Bukkit;

    import net.emenbee.abilities.Abilities;
    import net.emenbee.abilities.points.AbilityPoints;

    public class MySQL {
        private final String HOST;
        private final String DATABASE;
        private final String USER;
        private final String PASSWORD;
        private final int PORT;
        private Connection con;

        public MySQL(final String host, final int port, final String database, final String user, final String password) {
            this.HOST = host;
            this.PORT = port;
            this.DATABASE = database;
            this.USER = user;
            this.PASSWORD = password;
            this.connect();
        }

        public void connect() {
            try {
                this.con = DriverManager.getConnection(
                        "jdbc:mysql://" + this.HOST + ":" + this.PORT + "/" + this.DATABASE + "?autoReconnect=true",
                        this.USER, this.PASSWORD);
            } catch (SQLException e) {
            }
            Bukkit.getScheduler().runTaskLater(Abilities.getPlugin(), new Runnable() {
                @Override
                public void run() {
                    AbilityPoints.update();
                }
            }, 20L);
        }

        public void close() {
            try {
                if (this.con != null) {
                    this.con.close();
                }
            } catch (SQLException e) {
            }
        }

        public PreparedStatement prepareStatement(final String qry) {
            PreparedStatement st = null;
            try {
                st = this.con.prepareStatement(qry);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return st;
        }

        public void update(final PreparedStatement statement) {
            try {
                statement.executeUpdate();
                statement.close();
            } catch (SQLException e) {
                this.connect();
                e.printStackTrace();
            }
        }

        public boolean hasConnection() {
            return this.con != null;
        }

        public ResultSet query(final PreparedStatement statement) {
            try {
                return statement.executeQuery();
            } catch (SQLException e) {
                this.connect();
                e.printStackTrace();
                return null;
            }
        }
    }
     
    and it's just not connecting, there are no errors, could anyone look at my class and see if anything pops out to them? If not it's totally understandable but I have no clue as to why it's not connecting.
     
  2. Can you connect to that mysql database with ano there program like phpmyadmin? If not. It's probably you mysql host. If you use a free host or a cheap host. They probably have disabled outside Ips from connecting.
     
  3. it can definitely be connected to, other plugins have connected fine with it.
     
  4. When are you making the connection are you doing that in your main class?
     
  5. yes, I am
     
  6. There's probably no errors, because you catch the SQLException from the connection clause and then proceed to do nothing with it. Print the stacktrace and then you'll know why. I'm placing bets now on incorrect password or default mysql configuration
     
  7. okay, I'll do that and let you know my result :)