Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. package api.general.sql;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DatabaseMetaData;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. import org.bukkit.configuration.file.YamlConfiguration;
  11.  
  12. import api.general.main.GeneralAPI;
  13.  
  14. public class MySQL {
  15.  
  16. private static String user;
  17. private static String database;
  18. private static String password;
  19. private static String port;
  20. private static String hostname;
  21. private static Connection connection = null;
  22.  
  23. public static boolean checkTable(String name) {
  24.  
  25. boolean exists = false;
  26. try {
  27. DatabaseMetaData meta = MySQL.getConnection().getMetaData();
  28. ResultSet res;
  29.  
  30. res = meta.getTables(null, null, "", new String[] { "TABLE" });
  31. while (res.next()) {
  32. if (res.getString("TABLE_NAME").equals(name))
  33. exists = true;
  34. }
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. return exists;
  39. }
  40.  
  41. public static void loadMySQLSettings() {
  42.  
  43. File file = new File(GeneralAPI.getInstance().getDataFolder(), "mysql.yml");
  44. YamlConfiguration ymc = YamlConfiguration.loadConfiguration(file);
  45.  
  46. hostname = ymc.getString("mysql.hostname");
  47. port = ymc.getString("mysql.port");
  48. user = ymc.getString("mysql.user");
  49. password = ymc.getString("mysql.password");
  50. database = ymc.getString("mysql.database");
  51.  
  52. }
  53.  
  54. public static Connection getConnection() {
  55.  
  56. if (isConnected())
  57. return connection;
  58. else {
  59. System.out.println("[GeneralAPI-MySQL] Lost MySQL Connection. Reconnecting...");
  60. connect();
  61. return connection;
  62. }
  63.  
  64. }
  65.  
  66. public static boolean isConnected() {
  67. try {
  68. return connection != null && connection.isValid(120);
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. }
  72. return false;
  73. }
  74.  
  75. public static void connect() {
  76.  
  77. if (isConnected()) {
  78. try {
  79. connection.close();
  80. } catch (SQLException e) {
  81. }
  82. }
  83.  
  84. try {
  85. Class.forName("com.mysql.jdbc.Driver");
  86. connection = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database, user,
  87. password);
  88. System.out.println("[GeneralAPI-MySQL] Connected to database!");
  89. } catch (SQLException e) {
  90. System.out.println("[GeneralAPI-MySQL] Could not connect to MySQL server! Because: " + e.getMessage());
  91. } catch (ClassNotFoundException e) {
  92. System.out.println("[GeneralAPI-MySQL] JDBC Driver not found!");
  93. }
  94.  
  95. }
  96.  
  97. public static void closeConnection() {
  98. if (connection != null) {
  99. try {
  100. connection.close();
  101. connection = null;
  102. } catch (SQLException e) {
  103. System.out.println("[GeneralAPI-MySQL] Error while closing the MySQL Connection!");
  104. e.printStackTrace();
  105. }
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement