Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. public class MySQL {
  2.  
  3.     public static String USER = Main.cfg.getString("MySQL.user");
  4.     public static String HOST = Main.cfg.getString("MySQL.host");
  5.     public static String PASSWORD = Main.cfg.getString("MySQL.password");
  6.     public static String DATABASE = Main.cfg.getString("MySQL.database");
  7.    
  8.  
  9.     public static Connection con;
  10.    
  11.    
  12.  
  13.     // Connect to the MYSQL-Database
  14.     public static void connect() {
  15.         try {
  16.             con = DriverManager.getConnection(
  17.                     "jdbc:mysql://" + HOST + ":3306/" + DATABASE,
  18.                     USER, PASSWORD);
  19.             Server server = Bukkit.getServer();
  20.             ConsoleCommandSender console = server.getConsoleSender();
  21.             console.sendMessage(ChatColor.DARK_PURPLE + "MySQL-Connection: "+ ChatColor.GREEN + "Connected!");
  22.         } catch (SQLException localSQLException) { localSQLException.printStackTrace(); }
  23.     }
  24.  
  25.     // Close the MYSQL-Connection
  26.     public static void close() {
  27.         if (con != null) {
  28.             try {
  29.                 con.close();
  30.             } catch (SQLException localSQLException) { localSQLException.printStackTrace(); }
  31.         }
  32.     }
  33.  
  34.     //Check if the MYSQL-Connection closed!
  35.     // Return true if the Connection was closed. false if the connection is alive
  36.     public static boolean isClosed() {
  37.         try {
  38.             return con.isClosed();
  39.         } catch (SQLException localSQLException) { localSQLException.printStackTrace(); }
  40.         return false;
  41.     }
  42.  
  43.     //Creates an UPDATE-SQL Query!
  44.     public static void Update(String qry) {
  45.         if ((con == null) || (isClosed())) {
  46.             connect();
  47.         }
  48.         try {
  49.             Statement stmt = con.createStatement();
  50.             stmt.executeUpdate(qry);
  51.         } catch (SQLException localSQLException) { localSQLException.printStackTrace(); }
  52.     }
  53.  
  54.     ///     MYSQL Query     ///
  55.     ///     Return the ResultSet ///
  56.     public static ResultSet Query(String qry) {
  57.         if ((con == null) || (isClosed())) {
  58.             connect();
  59.         }
  60.         ResultSet rs = null;
  61.         try {
  62.             Statement stmt = con.createStatement();
  63.             rs = stmt.executeQuery(qry);
  64.         } catch (SQLException localSQLException) { localSQLException.printStackTrace(); }
  65.         return rs;
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement