Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.seltix.mysql;
- import com.google.common.collect.Maps;
- import net.md_5.bungee.config.Configuration;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.jetbrains.annotations.NotNull;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Map;
- public class SQLManager {
- private String host, username, password, database;
- private Map<String, String> tables = Maps.newHashMap();
- private int port;
- private Connection connection;
- private SQLManager(String host, int port, String username, String password, String database, Map<String, String> tables) {
- this.host = host;
- this.port = port;
- this.username = username;
- this.password = password;
- this.database = database;
- this.tables = tables;
- }
- private SQLManager(String host, int port, String username, String password, String database) {
- this.host = host;
- this.port = port;
- this.username = username;
- this.password = password;
- this.database = database;
- }
- public String getHost() {
- return host;
- }
- public String getPassword() {
- return password;
- }
- public String getUsername() {
- return username;
- }
- public String getDatabase() {
- return database;
- }
- public int getPort() {
- return port;
- }
- public void connect(Object synchronizer) {
- try {
- synchronized (synchronizer) {
- disconnect();
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String url = String.format("jdbc:mysql://%s:%s/%s", host, port, database);
- connection = DriverManager.getConnection(url, username, password);
- if (tables != null && tables.keySet().size() != 0)
- for (String table : tables.keySet()) {
- String tableValues = tables.get(table);
- if (table != null && tableValues != null) {
- ExecuteManager.valueOf(connection).execute("CREATE TABLE IF NOT EXISTS `"
- + table + "` (" + tableValues + ")");
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void disconnect() {
- try {
- if (!connection.isClosed()) connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public Connection getConnection() {
- return connection;
- }
- @NotNull
- public static Builder builder() {
- return new Builder();
- }
- public static class Builder {
- private String host, username, password, database;
- private int port;
- private Map<String, String> tables;
- public Builder bukkitConfigurtion(FileConfiguration configuration, String section) {
- return host(configuration.getString(section + ".Host"))
- .port(configuration.getInt(section + ".Port"))
- .user(configuration.getString(section + ".UserName"))
- .password(configuration.getString(section + ".Password"))
- .scheme(configuration.getString(section + ".DataBase"));
- }
- public Builder bungeeConfiguration(Configuration configuration, String section) {
- return host(configuration.getString(section + ".Host"))
- .port(configuration.getInt(section + ".Port"))
- .user(configuration.getString(section + ".UserName"))
- .password(configuration.getString(section + ".Password"))
- .scheme(configuration.getString(section + ".DataBase"));
- }
- public Builder host(String host) {
- this.host = host;
- return this;
- }
- public Builder user(String username) {
- this.username = username;
- return this;
- }
- public Builder password(String password) {
- this.password = password;
- return this;
- }
- public Builder scheme(String database) {
- this.database = database;
- return this;
- }
- public Builder port(int port) {
- this.port = port;
- return this;
- }
- public Builder table(String name, String values) {
- if (tables == null) tables = Maps.newHashMap();
- tables.put(name, values);
- return this;
- }
- public SQLManager build() {
- return new SQLManager(host, port, username, password, database, tables);
- }
- }
- }
- /*
- © SeltixFromHell 2018
- Вы можете использовать данный материал
- только для чтения.
- Копировать его можете только при условии, что оставите
- ссылку на мой ВК.
- http://vk.com/seltixhero_yt
- Также вы можете задавать вопросы мне в лс.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement