Guest User

Untitled

a guest
May 14th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. package me.IHaveSwag;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.util.logging.Level;
  8.  
  9. import org.bukkit.Bukkit;
  10.  
  11. public class MySQL
  12. {
  13.    
  14.     private Main main;  
  15.     public MySQL(Main pl)
  16.     {
  17.         this.setMain(pl);
  18.     }
  19.   private String a;
  20.   private String b;
  21.   private String c;
  22.   private String d;
  23.   private String e;
  24.   private static Connection connection;
  25.  
  26.   public MySQL(String a, String b, String c, String d, String e)
  27.   {
  28.     this.a = a;
  29.     this.b = b;
  30.     this.c = c;
  31.     this.d = d;
  32.     this.e = e;
  33.   }
  34.  
  35.  
  36.   public Connection openConnection()
  37.   {
  38.     try
  39.     {
  40.       Class.forName("com.mysql.jdbc.Driver");
  41.       MySQL.connection = DriverManager.getConnection("jdbc:mysql://" + this.a + ":" + this.b + "/" + this.c, this.d, this.e);
  42.     }
  43.     catch (SQLException e)
  44.     {
  45.       Bukkit.getServer().getLogger().log(Level.SEVERE, "Error when try connect to mysql because: " + e.getMessage());
  46.     }
  47.     catch (ClassNotFoundException e)
  48.     {
  49.         Bukkit.getServer().getLogger().log(Level.SEVERE, "JDBC Driver not found!");
  50.     }
  51.     return MySQL.connection;
  52.   }
  53.  
  54.   public boolean checkConnection()
  55.   {
  56.     return MySQL.connection != null;
  57.   }
  58.  
  59.   public static Connection getConnection()
  60.   {
  61.     return MySQL.connection;
  62.   }
  63.  
  64.   public static void closeConnection()
  65.   {
  66.     if (connection != null) {
  67.       try
  68.       {
  69.         connection.close();
  70.       }
  71.       catch (SQLException e)
  72.       {
  73.           Bukkit.getServer().getLogger().log(Level.SEVERE, "Error when closing the MySQL!");
  74.       }
  75.     }
  76.   }
  77.  
  78.  
  79. public Main getMain() {
  80.     return main;
  81. }
  82.  
  83.  
  84. public void setMain(Main main) {
  85.     this.main = main;
  86. }
  87. }
Add Comment
Please, Sign In to add comment