Solved MySQL not working properly!

Discussion in 'Plugin Development' started by Kermit_23, May 11, 2017.

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

    Kermit_23

    Hello,
    I am currently trying to store player names in a mysql database but it doesn't want to set them to the database! I am currently using huskehhh mysql api.
    Here is what i have reached so far (server connects successfully to the database without any problems and there are no existing errors in the console):

    Code:
        public void setupDB(String address, String port, String database, String username, String password)
                throws ClassNotFoundException, SQLException {
            this.db = new MySQL(address, port, database, username, password);
            this.db.openConnection();
            Statement statement = this.db.getConnection().createStatement();
            statement.executeUpdate("CREATE TABLE IF NOT EXISTS Whitelisted(Name varchar(32));");
            statement.close();
        }
    
        public void closeDatabase() throws SQLException {
            if (this.db != null && !this.db.checkConnection()) {
                this.db.closeConnection();
            }
        }
    
        public boolean isWhitelisted(Player p) throws SQLException, ClassNotFoundException {
            if (!this.db.checkConnection())
                this.db.openConnection();
            Statement statement = this.db.getConnection().createStatement();
            ResultSet rs = statement.executeQuery("SELECT Name FROM Whitelisted WHERE Name='" + p.getName() + "'");
            if (!rs.next()) {
                //System.out.println(ChatColor.RED + "NOT Whitelisted!");
                statement.close();
                rs.close();
                return false;
            } else {
                //System.out.println(ChatColor.RED + "IS Whitelisted!");
                statement.close();
                rs.close();
                return true;
            }
        }
    
        public void setWhitelisted(Player p) throws SQLException, ClassNotFoundException {
            if (!this.db.checkConnection())
                this.db.openConnection();
            Statement statement = this.db.getConnection().createStatement();
            if (!isWhitelisted(p)) {
                statement.executeUpdate("INSERT INTO Whitelisted(Name) VALUES ('" + p.getName() + "');");
                db.getConnection().commit();
                statement.close();
                //System.out.println(ChatColor.RED + "NOT Whitelisted!");
            } else {
                statement.close();
                //System.out.println(ChatColor.RED + "ALREADY Whitelisted!");
            }
        }
    
    *BUMP*[

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 11, 2017
Thread Status:
Not open for further replies.

Share This Page