Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.erkes.survivalgames.mysql;
- import org.bukkit.configuration.file.*;
- import java.io.*;
- import java.util.concurrent.*;
- import me.erkes.survivalgames.*;
- import org.bukkit.*;
- import java.util.function.*;
- import java.sql.*;
- import org.bukkit.plugin.*;
- public class MySQL
- {
- File file;
- FileConfiguration cfg;
- private String HOST;
- private String DATABASE;
- private String USER;
- private String PASSWORD;
- private Connection con;
- private ExecutorService executor;
- public MySQL() {
- this.file = new File("plugins/SurvivalGames/", "MySQL.yml");
- this.cfg = (FileConfiguration)YamlConfiguration.loadConfiguration(this.file);
- this.HOST = "";
- this.DATABASE = "";
- this.USER = "";
- this.PASSWORD = "";
- if (!this.file.exists()) {
- this.cfg.set("mysql.host", (Object)"host");
- this.cfg.set("mysql.database", (Object)"database");
- this.cfg.set("mysql.user", (Object)"user");
- this.cfg.set("mysql.password", (Object)"password");
- try {
- this.cfg.save(this.file);
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- this.HOST = this.cfg.getString("mysql.host");
- this.DATABASE = this.cfg.getString("mysql.database");
- this.USER = this.cfg.getString("mysql.user");
- this.PASSWORD = this.cfg.getString("mysql.password");
- this.connect();
- }
- public void connect() {
- try {
- this.con = DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":3306/" + this.DATABASE + "?autoReconnect=true", this.USER, this.PASSWORD);
- this.executor = Executors.newCachedThreadPool();
- System.out.println("[SURVIVALGAMES-MySQL] VERBINDUNG HERGESTELLLT!");
- }
- catch (SQLException e) {
- Bukkit.broadcastMessage(SurvivalGames.prefix + "§cMySQL Daten stimmen nicht!");
- System.out.println("[SURVIVALGAMES-MySQL] VERBINDUNG FEHLGESCHLAGEN! FEHLER: " + e.getMessage());
- }
- }
- public void close() {
- try {
- if (this.con != null) {
- this.con.close();
- System.out.println("[SURVIVALGAMES-MySQL] VERBINDUNG ERFOLGREICH UNTERBROCHEN!");
- }
- }
- catch (SQLException e) {
- System.out.println("[SURVIVALGAMES-MySQL] VERBINDUNGSTRENNUNG FEHLGESCHLAGEN! FEHLER: " + e.getMessage());
- }
- }
- public void update(final PreparedStatement statement) {
- this.executor.execute(() -> SurvivalGames.mysql.queryUpdate(statement));
- }
- public void update(final String statement) {
- this.executor.execute(() -> SurvivalGames.mysql.queryUpdate(statement));
- }
- public void query(final PreparedStatement statement, final Consumer<ResultSet> consumer) {
- final ResultSet result;
- this.executor.execute(() -> {
- result = SurvivalGames.mysql.query(statement);
- Bukkit.getScheduler().runTask((Plugin)SurvivalGames.m, () -> consumer.accept(result));
- });
- }
- public void query(final String statement, final Consumer<ResultSet> consumer) {
- final ResultSet result;
- this.executor.execute(() -> {
- result = SurvivalGames.mysql.query(statement);
- Bukkit.getScheduler().runTask((Plugin)SurvivalGames.m, () -> consumer.accept(result));
- });
- }
- public PreparedStatement prepare(final String query) {
- try {
- return SurvivalGames.mysql.con.prepareStatement(query);
- }
- catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public void queryUpdate(final String query) {
- this.checkConnection();
- try (final PreparedStatement statement = this.con.prepareStatement(query)) {
- this.queryUpdate(statement);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void queryUpdate(final PreparedStatement statement) {
- this.checkConnection();
- try {
- statement.executeUpdate();
- }
- catch (Exception e) {
- e.printStackTrace();
- try {
- statement.close();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- finally {
- try {
- statement.close();
- }
- catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- public ResultSet query(final String query) {
- this.checkConnection();
- try {
- return this.query(this.con.prepareStatement(query));
- }
- catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public ResultSet query(final PreparedStatement statement) {
- this.checkConnection();
- try {
- return statement.executeQuery();
- }
- catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- private void checkConnection() {
- try {
- if (this.con == null || !this.con.isValid(10) || this.con.isClosed()) {
- this.openConnection();
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public Connection openConnection() throws Exception {
- Class.forName("com.mysql.jdbc.Driver");
- return this.con = DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":" + 3306 + "/" + this.DATABASE, this.USER, this.PASSWORD);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement