PrinzKenny1

Untitled

Feb 3rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package skywars.kenny.db;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.configuration.file.FileConfiguration;
  5. import org.bukkit.configuration.file.YamlConfiguration;
  6. import skywars.kenny.main.main;
  7.  
  8. import java.io.File;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.SQLException;
  12.  
  13. public class dbmain {
  14.  
  15. static final File file = new File("plugins/SkyWars", "config.yml");
  16. static final FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  17.  
  18. public static final String host = cfg.getString("host");
  19. public static final int port = cfg.getInt("port");
  20. public static final String username = cfg.getString("username");
  21. public static final String password = cfg.getString("password");
  22. public static final String database = cfg.getString("database");
  23. static Connection con;
  24.  
  25. public static void connect() {
  26. if (!(isConnected())) {
  27. try {
  28. startRunnable();
  29. con = DriverManager.getConnection(
  30. "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username,
  31. password);
  32. Bukkit.getConsoleSender().sendMessage("[MySQL] ยง2Connected");
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }
  38.  
  39. public static void disconnect() {
  40. if (isConnected()) {
  41. try {
  42. con.close();
  43. con = null;
  44. Bukkit.getConsoleSender().sendMessage("[MySQL] ยง4Disconnected");
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50.  
  51. public static void startRunnable() {
  52. Bukkit.getScheduler().scheduleSyncRepeatingTask(main.plugin, new Runnable() {
  53.  
  54. @Override
  55. public void run() {
  56. if (!(isConnected())) {
  57. try {
  58. con = DriverManager.getConnection(
  59. "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username,
  60. password);
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66. }, 20, 20);
  67. }
  68.  
  69. public static boolean isConnected() {
  70. return (con != null);
  71. }
  72.  
  73. public static Connection getConnection() {
  74. return con;
  75. }
  76.  
  77. }
Add Comment
Please, Sign In to add comment