1.15.2 MySQL is null

Discussion in 'Spigot Plugin Development' started by Snowmite, Mar 21, 2020.

  1. I am trying to connect to a MySQL server on my Amazon RDS server. When the plugin enables and connects, everything is perfect. However, whenever I try to pull data off of the database, it spams my warning message I put.
    Code:
    Code (Text):
    package me.snowmite.covidclicker;

    import me.snowmite.covidclicker.command.ClickerCommand;
    import me.snowmite.covidclicker.gui.ClickerGUI;
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    import java.sql.*;

    public class Core extends JavaPlugin implements Listener {

        private static Core instance;

        public static int clickRate = 0;
        public static int clicks = 0;
        public static String nextDonationDate = "";

        public static Core getInstance() {
            return instance;
        }

        private static Connection connection;
        private String host, database, username, password;
        private int port;

        public static Connection getConnection() {
            return connection;
        }

        @Override
        public void onEnable() {
            instance = this;

            getCommand("covidclicker").setExecutor(new ClickerCommand());

            host = "amazon"; <- Changed for obvious reasons
            database = "covidplugin";
            username = "someuser"; <- Changed for obvious reasons
            password = "somepass$"; <- Changed for obvious reasons
            port = 3306;

            try{
                openConnection();
            }catch(SQLException | ClassNotFoundException e){

            }
                    try {
                        if(Core.getConnection()==null){
                            getLogger().info("Null connection!");
                            return;
                        }
                        Statement statement = Core.getInstance().getConnection().createStatement();
                        ResultSet result = statement.executeQuery("SELECT * FROM clicker WHERE 1");
                        while(result.next()){
                            clicks = result.getInt("clicks");
                            clickRate = result.getInt("click_rate");
                            nextDonationDate = result.getString("next_donation_date");
                        }
                        result.close();
                        statement.close();
                    } catch (SQLException /*| ClassNotFoundException*/ e) {
                    }
         

            getCommand("covidclicker").setExecutor(new ClickerCommand());

            getServer().getPluginManager().registerEvents(new ClickerGUI(), this);

            getLogger().info("COVID-19 Clicker v" + getDescription().getVersion() + "v has been enabled!");
        }

        @Override
        public void onDisable() {
            getLogger().info("COVID-19 Clicker v" + getDescription().getVersion() + "v has been disabled!");
        }

        public void openConnection() throws SQLException, ClassNotFoundException {
            if (connection != null && !connection.isClosed()) {
                return;
            }

            synchronized (this) {
                if (connection != null && !connection.isClosed()) {
                    return;
                }
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
            }
        }

    }
     
    This is the part that produces the null message:
    Code (Text):
                    try {
                        if(Core.getConnection()==null){
                            getLogger().info("Null connection!");
                            return;
                        }
                        Statement statement = Core.getInstance().getConnection().createStatement();
                        ResultSet result = statement.executeQuery("SELECT * FROM clicker WHERE 1");
                        while(result.next()){
                            clicks = result.getInt("clicks");
                            clickRate = result.getInt("click_rate");
                            nextDonationDate = result.getString("next_donation_date");
                        }
                        result.close();
                        statement.close();
                    } catch (SQLException /*| ClassNotFoundException*/ e) {
                    }
     
  2. Print the exception in the catch block instead of just having it blank and it will tell you what's wrong
    e.printStackTrace();
     
  3. ok so it said, database “covidplugin” could not be found. I am using MySQL Workbench, and it says on AWS that the name IS “covidplugin”