title: Implementing SQL in a Bukkit/Spigot plugin
body: <p>First of all, I'm new to these forums, so I'm sorry if (or when) I make a tiny mistake somewhere.</p>
  
  <p>After having learned HTML and CSS a while ago, I recently started learning Java.
  I'm a developer at a Minecraft server network, and one of the plugins I made is one that enables /color [color code or color name].</p>
  
  <p>Now, I've set up a MySQL database in which I created a table with UUID and Color as variables.
  I've been struggling to let the plugin connect to the database and store/retrieve the color of the player's name he chose, because it needs to work across multiple servers.</p>
  
  <p>I'd like the plugin to be optimised, so it only connects if needed and disconnects with the SQL database so it doesn't give too much lag.
  This is the code I have till now:</p>
  
  <pre><code>package nl.live.partyblaze.pbcolor;
  
  import org.bukkit.ChatColor;
  import org.bukkit.command.Command;
  import org.bukkit.command.CommandSender;
  import org.bukkit.entity.Player;
  import org.bukkit.plugin.java.JavaPlugin;
  
  public final class PBColor extends JavaPlugin
  {
      @Override
      public void onEnable()
      {
          getLogger().info("[PBColor] onEnable has been invoked!");
      }
  
      @Override
      public void onDisable()
      {
          getLogger().info("[PBColor] onDisable has been invoked!");
      }
  
      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
          if (cmd.getName().equalsIgnoreCase("color"))
          {
              if (args.length == 1)
              {
                  if (args[0].equalsIgnoreCase("a"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.GREEN + displ);
                      sender.sendMessage("§aYour name has been set to green!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("green"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.GREEN + displ);
                      sender.sendMessage("§aYour name has been set to green!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("b"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.AQUA + displ);
                      sender.sendMessage("§bYour name has been set to aqua!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("aqua"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.AQUA + displ);
                      sender.sendMessage("§bYour name has been set to aqua!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("c"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.RED + displ);
                      sender.sendMessage("§cYour name has been set to red!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("red"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.RED + displ);
                      sender.sendMessage("§cYour name has been set to red!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("d"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.LIGHT_PURPLE + displ);
                      sender.sendMessage("§dYour name has been set to light purple!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("lightpurple"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.LIGHT_PURPLE + displ);
                      sender.sendMessage("§dYour name has been set to light purple!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("e"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.YELLOW + displ);
                      sender.sendMessage("§eYour name has been set to yellow!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("yellow"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.YELLOW + displ);
                      sender.sendMessage("§eYour name has been set to yellow!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("f"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.WHITE + displ);
                      sender.sendMessage("§fYour name has been set to white!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("white"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.WHITE + displ);
                      sender.sendMessage("§fYour name has been set to white!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("1"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_BLUE + displ);
                      sender.sendMessage("§1Your name has been set to dark blue!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("darkblue"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_BLUE + displ);
                      sender.sendMessage("§1Your name has been set to dark blue!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("2"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_GREEN + displ);
                      sender.sendMessage("§2Your name has been set to dark green!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("darkgreen"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_GREEN + displ);
                      sender.sendMessage("§2Your name has been set to dark green!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("3"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_AQUA + displ);
                      sender.sendMessage("§3Your name has been set to dark aqua!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("darkaqua"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_AQUA + displ);
                      sender.sendMessage("§3Your name has been set to dark aqua!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("4"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_RED + displ);
                      sender.sendMessage("§4Your name has been set to dark red!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("darkred"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_RED + displ);
                      sender.sendMessage("§4Your name has been set to dark red!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("5"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_PURPLE + displ);
                      sender.sendMessage("§5Your name has been set to dark purple!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("darkpurple"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_PURPLE + displ);
                      sender.sendMessage("§5Your name has been set to dark purple!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("6"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.GOLD + displ);
                      sender.sendMessage("§6Your name has been set to gold!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("gold"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.GOLD + displ);
                      sender.sendMessage("§6Your name has been set to gold!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("7"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.GRAY + displ);
                      sender.sendMessage("§7Your name has been set to gray!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("gray"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.GRAY + displ);
                      sender.sendMessage("§7Your name has been set to gray!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("8"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_GRAY + displ);
                      sender.sendMessage("§8Your name has been set to dark gray!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("darkgray"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.DARK_GRAY + displ);
                      sender.sendMessage("§8Your name has been set to dark gray!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("9"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.BLUE + displ);
                      sender.sendMessage("§9Your name has been set to blue!");
                      return true;
                  }
  
                  if (args[0].equalsIgnoreCase("blue"))
                  {
                      String displ = sender.getName();
                      Player p = (Player) sender;
                      p.setDisplayName(ChatColor.BLUE + displ);
                      sender.sendMessage("§9Your name has been set to blue!");
                      return true;
                  }
              }
          }
          return false; 
      }
  }
  </code></pre>
  
  <p>Thanks in advance!</p>