Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. package ru.seltix.mysql;
  2.  
  3. import com.google.common.collect.Maps;
  4. import net.md_5.bungee.config.Configuration;
  5. import org.bukkit.configuration.file.FileConfiguration;
  6. import org.jetbrains.annotations.NotNull;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.util.Map;
  12.  
  13. public class SQLManager {
  14.  
  15. private String host, username, password, database;
  16. private Map<String, String> tables = Maps.newHashMap();
  17. private int port;
  18. private Connection connection;
  19.  
  20. private SQLManager(String host, int port, String username, String password, String database, Map<String, String> tables) {
  21. this.host = host;
  22. this.port = port;
  23. this.username = username;
  24. this.password = password;
  25. this.database = database;
  26. this.tables = tables;
  27. }
  28.  
  29. private SQLManager(String host, int port, String username, String password, String database) {
  30. this.host = host;
  31. this.port = port;
  32. this.username = username;
  33. this.password = password;
  34. this.database = database;
  35. }
  36.  
  37. public String getHost() {
  38. return host;
  39. }
  40.  
  41. public String getPassword() {
  42. return password;
  43. }
  44.  
  45. public String getUsername() {
  46. return username;
  47. }
  48.  
  49. public String getDatabase() {
  50. return database;
  51. }
  52.  
  53. public int getPort() {
  54. return port;
  55. }
  56.  
  57. public void connect(Object synchronizer) {
  58. try {
  59. synchronized (synchronizer) {
  60. disconnect();
  61.  
  62. Class.forName("com.mysql.jdbc.Driver").newInstance();
  63. String url = String.format("jdbc:mysql://%s:%s/%s", host, port, database);
  64. connection = DriverManager.getConnection(url, username, password);
  65.  
  66. if (tables != null && tables.keySet().size() != 0)
  67. for (String table : tables.keySet()) {
  68. String tableValues = tables.get(table);
  69. if (table != null && tableValues != null) {
  70. ExecuteManager.valueOf(connection).execute("CREATE TABLE IF NOT EXISTS `"
  71. + table + "` (" + tableValues + ")");
  72. }
  73. }
  74. }
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78. }
  79.  
  80. public void disconnect() {
  81. try {
  82. if (!connection.isClosed()) connection.close();
  83. } catch (SQLException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87.  
  88. public Connection getConnection() {
  89. return connection;
  90. }
  91.  
  92. @NotNull
  93. public static Builder builder() {
  94. return new Builder();
  95. }
  96.  
  97. public static class Builder {
  98.  
  99. private String host, username, password, database;
  100. private int port;
  101. private Map<String, String> tables;
  102.  
  103. public Builder bukkitConfigurtion(FileConfiguration configuration, String section) {
  104. return host(configuration.getString(section + ".Host"))
  105. .port(configuration.getInt(section + ".Port"))
  106. .user(configuration.getString(section + ".UserName"))
  107. .password(configuration.getString(section + ".Password"))
  108. .scheme(configuration.getString(section + ".DataBase"));
  109. }
  110.  
  111. public Builder bungeeConfiguration(Configuration configuration, String section) {
  112. return host(configuration.getString(section + ".Host"))
  113. .port(configuration.getInt(section + ".Port"))
  114. .user(configuration.getString(section + ".UserName"))
  115. .password(configuration.getString(section + ".Password"))
  116. .scheme(configuration.getString(section + ".DataBase"));
  117. }
  118.  
  119. public Builder host(String host) {
  120. this.host = host;
  121. return this;
  122. }
  123.  
  124. public Builder user(String username) {
  125. this.username = username;
  126. return this;
  127. }
  128.  
  129. public Builder password(String password) {
  130. this.password = password;
  131. return this;
  132. }
  133.  
  134. public Builder scheme(String database) {
  135. this.database = database;
  136. return this;
  137. }
  138.  
  139. public Builder port(int port) {
  140. this.port = port;
  141. return this;
  142. }
  143.  
  144. public Builder table(String name, String values) {
  145. if (tables == null) tables = Maps.newHashMap();
  146. tables.put(name, values);
  147. return this;
  148. }
  149.  
  150. public SQLManager build() {
  151. return new SQLManager(host, port, username, password, database, tables);
  152. }
  153.  
  154. }
  155.  
  156. }
  157. /*
  158. © SeltixFromHell 2018
  159. Вы можете использовать данный материал
  160. только для чтения.
  161. Копировать его можете только при условии, что оставите
  162. ссылку на мой ВК.
  163. http://vk.com/seltixhero_yt
  164.  
  165. Также вы можете задавать вопросы мне в лс.
  166. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement