MySQL Booleans

Discussion in 'Plugin Development' started by WingedMLGPro, May 1, 2015.

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

    WingedMLGPro

    Hey Everyone!
    I am trying to make a Boolean with MySQL but it doesnt work as it turns it into a tinyint!
    Here is my code:
    Code:
    public static void createTable() {
            if (isConnected())
                try {
                    connection.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS CreditKits (UUID VARCHAR(100), PlayerName VARCHAR(100), ZombiePigman BOOLEAN, Enderman BOOLEAN, Witch BOOLEAN, Creeper BOOLEAN)");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
    Code:
    package me.benkea.Kits.CreditKitAPI;
    
    import org.bukkit.entity.Player;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    /**
    * © This Document and Code is STRICTLY copyrited(©) to Ben.
    * © If anyone takes any part of this code and uses it for
    * © Something public, Share with someone, uses it for API's,
    * © implementing it to their code and taking the rights for
    * © it is NOT allowed unless you get permission from me!
    * © This project SwiftyPVP was created by 35047
    * © at 27/04/15 at 8:20 PM
    */
    public class KitsAPI {
    
        public Boolean hasZombiePigman(Player p){
            ResultSet rs = CreditKitMySQL.getResult("SELECT ZombiePigman FROM CreditKits WHERE UUID='"+p.getUniqueId()+"'");
            try {
                if (rs.next()){
                    return rs.getBoolean("ZombiePigman");
                }
                return null;
            } catch (SQLException e){
                e.printStackTrace();
            }
            return false;
        }
        public Boolean hasEnderman(Player p){
            ResultSet rs = CreditKitMySQL.getResult("SELECT Enderman FROM CreditKits WHERE UUID='"+p.getUniqueId()+"'");
            try {
                if (rs.next()){
                    return rs.getBoolean("Enderman");
                }
                return false;
            } catch (SQLException e){
                e.printStackTrace();
            }
            return false;
        }
        public Boolean hasWitch(Player p){
            ResultSet rs = CreditKitMySQL.getResult("SELECT Witch FROM CreditKits WHERE UUID='"+p.getUniqueId()+"'");
            try {
                if (rs.next()){
                    return rs.getBoolean("Witch");
                }
                return false;
            } catch (SQLException e){
                e.printStackTrace();
            }
            return false;
        }
        public Boolean hasCreeper(Player p){
            ResultSet rs = CreditKitMySQL.getResult("SELECT Creeper FROM CreditKits WHERE UUID='"+p.getUniqueId()+"'");
            try {
                if (rs.next()){
                    return rs.getBoolean("Creeper");
                }
                return false;
            } catch (SQLException e){
                e.printStackTrace();
            }
            return false;
        }
        public void setHasZombiePigman(Player p, Boolean b){
            if (hasZombiePigman(p)==null){
                CreditKitMySQL.update("INSERT INTO `CreditKits`(`UUID`, `PlayerName`, `ZombiePigman`) VALUES ('"+p.getUniqueId()+"','"+p.getName()+"','"+b+"')");
            }
            else if (hasZombiePigman(p)!=null){
                CreditKitMySQL.update("UPDATE `CreditKits` SET `UUID`='"+p.getUniqueId()+"',`PlayerName`='"+p.getName()+"',`ZombiePigman`='"+b+"' WHERE UUID='"+p.getUniqueId()+"'");
            }
        }
        public void setHasEnderman(Player p, Boolean b){
            if (hasZombiePigman(p)==null){
                CreditKitMySQL.update("INSERT INTO `CreditKits`(`UUID`, `PlayerName`, `Enderman`) VALUES ('"+p.getUniqueId()+"','"+p.getName()+"','"+b+"')");
            }
            else if (hasZombiePigman(p)!=null){
                CreditKitMySQL.update("UPDATE `CreditKits` SET `UUID`='"+p.getUniqueId()+"',`PlayerName`='"+p.getName()+"',`Enderman`='"+b+"' WHERE UUID='"+p.getUniqueId()+"'");
            }
        }
        public void setHasWitch(Player p, Boolean b){
            if (hasZombiePigman(p)==null){
                CreditKitMySQL.update("INSERT INTO `CreditKits`(`UUID`, `PlayerName`, `Witch`) VALUES ('"+p.getUniqueId()+"','"+p.getName()+"','"+b+"')");
            }
            else if (hasZombiePigman(p)!=null){
                CreditKitMySQL.update("UPDATE `CreditKits` SET `UUID`='"+p.getUniqueId()+"',`PlayerName`='"+p.getName()+"',`Witch`='"+b+"' WHERE UUID='"+p.getUniqueId()+"'");
            }
        }
        public void setHasCreeper(Player p, Boolean b){
            if (hasZombiePigman(p)==null){
                CreditKitMySQL.update("INSERT INTO `CreditKits`(`UUID`, `PlayerName`, `Creeper`) VALUES ('"+p.getUniqueId()+"','"+p.getName()+"','"+b+"')");
            }
            else if (hasZombiePigman(p)!=null){
                CreditKitMySQL.update("UPDATE `CreditKits` SET `UUID`='"+p.getUniqueId()+"',`PlayerName`='"+p.getName()+"',`Creeper`='"+b+"' WHERE UUID='"+p.getUniqueId()+"'");
            }
        }
    }
    
     
  2. Offline

    mine-care

    @WingedMLGPro Yes indeed in mysql BOOLEAN takes values 0 and 1 (0 false, 1 true) at least thats what i know so far :3
    and a few notices (i could not avoid pointing them out xD)
    Java naming convention :3

    Can you copyright code based on bukkit? -thats a question i had for a loong time-
     
  3. Offline

    megamichiel

    What you can simply do is ResultSet#getString(<column>).equalsIgnoreCase("1"), that's how I do it
     
  4. Offline

    mythbusterma

    No, all code that references the Bukkit API at all must be released under the GPL or compatible licenses (i.e. it is free to be modified, used, and redistributed by anyone).
     
  5. Offline

    mine-care

Thread Status:
Not open for further replies.

Share This Page