Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.seltix.api.java.mysql;
- import com.google.common.collect.Maps;
- import ru.seltix.api.bukkit.BukkitLogger;
- import ru.seltix.api.java.interfaces.Builder;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Map;
- @SuppressWarnings("All")
- public class SQLConnection {
- private String host, user, password, database;
- private int port;
- private Map<String, String> tables;
- private Connection connection;
- SQLConnection(String host, int port, String user, String password, String database, Map<String, String> tables) {
- this.host = host;
- this.port = port;
- this.user = user;
- this.password = password;
- this.database = database;
- this.tables = tables;
- }
- public <S> void connect(S synchroniezer) {
- try {
- synchronized (synchroniezer) {
- disconnect();
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String url = String.format("jdbc:mysql://%s:%s/%s", host, port, database);
- this.connection = DriverManager.getConnection(url, user, password);
- BukkitLogger.log("MySQL has been connected of: §e" + url);
- for (String table : tables.keySet()) {
- Executor.valueOf(getConnection()).execute("CREATE TABLE IF NOT EXISTS `" + table + "` " +
- "(" + tables.get(table) + ")");
- BukkitLogger.log("Table §e'" + table + "' §fhas been created!");
- }
- }
- } catch (Exception e) {
- BukkitLogger.error("Failed connect to MySQL:");
- e.printStackTrace();
- }
- }
- public void disconnect() {
- try {
- if (connection != null && !connection.isClosed()) {
- connection.close();
- BukkitLogger.log("Connection has been closed!");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public Connection getConnection() {
- if (connection == null)
- try {
- String url = String.format("jdbc:mysql://%s:%s/%s", host, port, database);
- this.connection = DriverManager.getConnection(url, user, password);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return connection;
- }
- public static SQLBuilder newBuilder() {
- return new SQLBuilder();
- }
- public static class SQLBuilder implements Builder<SQLConnection> {
- private String host, user, password, database;
- private int port;
- private Map<String, String> tables;
- public SQLBuilder() {
- this.host = "localhost";
- this.user = "root";
- this.password = "";
- this.database = "mysql";
- this.port = 3306;
- this.tables = Maps.newHashMap();
- }
- public SQLBuilder setHost(String host) {
- this.host = host;
- return this;
- }
- public SQLBuilder setPort(int port) {
- this.port = port;
- return this;
- }
- public SQLBuilder setUsername(String username) {
- this.user = username;
- return this;
- }
- public SQLBuilder setPassword(String password) {
- this.password = password;
- return this;
- }
- public SQLBuilder setDatabase(String database) {
- this.database = database;
- return this;
- }
- public SQLBuilder createTable(String table, String values) {
- this.tables.put(table, values);
- return this;
- }
- @Override
- public SQLConnection build() {
- return new SQLConnection(host, port, user, password, database, tables);
- }
- }
- }
- /*
- © SeltixFromHell 2018
- Вы можете использовать данный материал
- только для чтения.
- Копировать его можете только при условии, что оставите
- ссылку на мой ВК.
- http://vk.com/seltixhero_yt
- Также вы можете задавать вопросы мне в лс.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement