Advertisement
Guest User

Untitled

a guest
Oct 4th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. package ru.seltix.api.java.mysql;
  2.  
  3. import com.google.common.collect.Maps;
  4. import ru.seltix.api.bukkit.BukkitLogger;
  5. import ru.seltix.api.java.interfaces.Builder;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.SQLException;
  10. import java.util.Map;
  11.  
  12. @SuppressWarnings("All")
  13. public class SQLConnection {
  14.  
  15. private String host, user, password, database;
  16. private int port;
  17. private Map<String, String> tables;
  18.  
  19. private Connection connection;
  20.  
  21. SQLConnection(String host, int port, String user, String password, String database, Map<String, String> tables) {
  22. this.host = host;
  23. this.port = port;
  24. this.user = user;
  25. this.password = password;
  26. this.database = database;
  27. this.tables = tables;
  28. }
  29.  
  30. public <S> void connect(S synchroniezer) {
  31. try {
  32. synchronized (synchroniezer) {
  33. disconnect();
  34.  
  35. Class.forName("com.mysql.jdbc.Driver").newInstance();
  36. String url = String.format("jdbc:mysql://%s:%s/%s", host, port, database);
  37. this.connection = DriverManager.getConnection(url, user, password);
  38.  
  39. BukkitLogger.log("MySQL has been connected of: §e" + url);
  40.  
  41. for (String table : tables.keySet()) {
  42. Executor.valueOf(getConnection()).execute("CREATE TABLE IF NOT EXISTS `" + table + "` " +
  43. "(" + tables.get(table) + ")");
  44. BukkitLogger.log("Table §e'" + table + "' §fhas been created!");
  45. }
  46. }
  47. } catch (Exception e) {
  48. BukkitLogger.error("Failed connect to MySQL:");
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. public void disconnect() {
  54. try {
  55. if (connection != null && !connection.isClosed()) {
  56. connection.close();
  57. BukkitLogger.log("Connection has been closed!");
  58. }
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public Connection getConnection() {
  65. if (connection == null)
  66. try {
  67. String url = String.format("jdbc:mysql://%s:%s/%s", host, port, database);
  68. this.connection = DriverManager.getConnection(url, user, password);
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. }
  72. return connection;
  73. }
  74.  
  75. public static SQLBuilder newBuilder() {
  76. return new SQLBuilder();
  77. }
  78.  
  79. public static class SQLBuilder implements Builder<SQLConnection> {
  80.  
  81. private String host, user, password, database;
  82. private int port;
  83. private Map<String, String> tables;
  84.  
  85. public SQLBuilder() {
  86. this.host = "localhost";
  87. this.user = "root";
  88. this.password = "";
  89. this.database = "mysql";
  90. this.port = 3306;
  91. this.tables = Maps.newHashMap();
  92. }
  93.  
  94. public SQLBuilder setHost(String host) {
  95. this.host = host;
  96. return this;
  97. }
  98. public SQLBuilder setPort(int port) {
  99. this.port = port;
  100. return this;
  101. }
  102. public SQLBuilder setUsername(String username) {
  103. this.user = username;
  104. return this;
  105. }
  106. public SQLBuilder setPassword(String password) {
  107. this.password = password;
  108. return this;
  109. }
  110. public SQLBuilder setDatabase(String database) {
  111. this.database = database;
  112. return this;
  113. }
  114. public SQLBuilder createTable(String table, String values) {
  115. this.tables.put(table, values);
  116. return this;
  117. }
  118.  
  119. @Override
  120. public SQLConnection build() {
  121. return new SQLConnection(host, port, user, password, database, tables);
  122. }
  123. }
  124.  
  125. }
  126. /*
  127. © SeltixFromHell 2018
  128. Вы можете использовать данный материал
  129. только для чтения.
  130. Копировать его можете только при условии, что оставите
  131. ссылку на мой ВК.
  132. http://vk.com/seltixhero_yt
  133.  
  134. Также вы можете задавать вопросы мне в лс.
  135. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement