Advertisement
Guest User

Untitled

a guest
Oct 15th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package mysqlclasses;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.configuration.file.FileConfiguration;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10.  
  11. public class MySQLAPI extends JavaPlugin {
  12.    
  13.    
  14.     public String prefix = ChatColor.GOLD + "M" + ChatColor.GRAY + "y" + ChatColor.GOLD + "S" + ChatColor.GRAY + "QL" + ChatColor.GOLD + "S" + ChatColor.GRAY + "ync" + ChatColor.YELLOW + " > " + ChatColor.RESET;
  15.        
  16.     public static String MySQL_host = "";
  17.     public static String MySQL_port = "";
  18.     public static String MySQL_user = "";
  19.     public static String MySQL_pass = "";
  20.     public static String MySQL_db = "";
  21.     public static Connection con;
  22.    
  23.     public void onEnable() {
  24.         FileConfiguration cfg = this.getConfig();
  25.         if(!this.getConfig().isSet("MYSQL")) {
  26.             cfg.set("MYSQL.host", "//enter your hostname here");
  27.             cfg.set("MYSQL.port", "//enter your port here");
  28.             cfg.set("MYSQL.user", "//enter your username here");
  29.             cfg.set("MYSQL.pass", "//enter your password here");
  30.             cfg.set("MYSQL.db", "//enter your database here");
  31.             this.saveConfig();
  32.         } else {
  33.             MySQL_host = cfg.getString("MYSQL.host");
  34.             MySQL_port = cfg.getString("MYSQL.port");
  35.             MySQL_user = cfg.getString("MYSQL.user");
  36.             MySQL_pass = cfg.getString("MYSQL.pass");
  37.             MySQL_db = cfg.getString("MYSQL.db");
  38.         }
  39.        
  40.         System.out.println("[MySQL] Plugin erfolgreich aktiviert!");
  41.         connect();
  42.     }
  43.    
  44.     public void onDisable() {
  45.         disconnect();
  46.         System.out.println("[MySQL] Plugin erfolgreich deaktiviert!");
  47.     }
  48.    
  49.     public static void connect() {
  50.         if(!isConnected()) {
  51.             try {
  52.                 con = DriverManager.getConnection("jdbc:mysql://" + MySQL_host + ":" + MySQL_port + "/" + MySQL_db, MySQL_user, MySQL_pass);
  53.                 System.out.println("[MySQL] Datenbankverbindung erfolgreich aufgebaut!");
  54.             } catch (SQLException e) {
  55.                 System.out.println("[MySQL] Datenbankverbindung gescheitert!");
  56.             }
  57.         }
  58.     }
  59.    
  60.     public static void disconnect() {
  61.         if(isConnected()) {
  62.             try {
  63.                 con.close();
  64.                 System.out.println("[MySQL] Datenbankverbindung erfolgreich geschlossen!");
  65.             } catch (SQLException e) {
  66.             }
  67.         }
  68.     }
  69.    
  70.     public static boolean isConnected() {      
  71.         return (con == null ? false : true);
  72.     }
  73.    
  74.     public static Connection getConnection() {
  75.         return con;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement