Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.neoplay.QuickSG.MySQL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import org.bukkit.entity.Player;
- import io.github.neoplay.QuickSG.QuickSG;
- public class MySQL {
- public static String host = QuickSG.cfg.getString("MySQL.Host");
- public static String port = QuickSG.cfg.getString("MySQL.Port");
- public static String database = QuickSG.cfg.getString("MySQL.Datenbank");
- public static String username = QuickSG.cfg.getString("MySQL.Benutzername");
- public static String password = QuickSG.cfg.getString("MySQL.Passwort");
- public static Connection connection;
- public static Connection getConnection() {
- return connection;
- }
- public static boolean isConnected() {
- return connection != null;
- }
- public static void connect() {
- if (!isConnected()) {
- try {
- connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username,
- password);
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public static void disconnect() {
- if (isConnected()) {
- try {
- connection.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public static void createTableIfNotExists() {
- if (isConnected()) {
- try {
- PreparedStatement ps = getConnection().prepareStatement(
- "CREATE TABLE IF NOT EXISTS QuickSG_Stats_Player (Name VARCHAR(255), UUID VARCHAR(255), Kills INT(11), Deaths INT(11), Wins INT(11), Looses INT(11), Draws INT(11))");
- ps.executeUpdate();
- ps.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public static boolean isUserExisting(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- boolean user = result.next();
- result.close();
- ps.close();
- return user;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return false;
- }
- public static boolean isUserExisting(UUID uuid) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- boolean user = result.next();
- result.close();
- ps.close();
- return user;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return false;
- }
- public static void registerPlayer(Player p) {
- if (isUserExisting(p)) {
- return;
- }
- try {
- PreparedStatement ps = connection.prepareStatement(
- "INSERT INTO QuickSG_Stats_Player (Spielername, UUID, Kills, Deaths, Wins, Looses, Draws) VALUES (?, ?, ?, ?, ?, ?, ?)");
- ps.setString(1, p.getName());
- ps.setString(2, p.getUniqueId().toString());
- ps.setInt(3, 0);
- ps.setInt(4, 0);
- ps.setInt(5, 0);
- ps.setInt(6, 0);
- ps.setInt(7, 0);
- ps.execute();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static void setPlayerName(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("UPDATE QuickSG_Stats_Player SET Name = ? WHERE UUID = ?");
- ps.setString(1, p.getName());
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static String getPlayer(UUID uuid) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- String name = result.getString("Name");
- result.close();
- ps.close();
- return name;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return "null";
- }
- public static int getKills(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int kills = result.getInt("Kills");
- result.close();
- ps.close();
- return kills;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getKills(UUID uuid) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int kills = result.getInt("Kills");
- result.close();
- ps.close();
- return kills;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getDeaths(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int deaths = result.getInt("Deaths");
- result.close();
- ps.close();
- return deaths;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getDeaths(UUID uuid) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int deaths = result.getInt("Deaths");
- result.close();
- ps.close();
- return deaths;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static double getKD(Player p) {
- try {
- PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps1.setString(1, p.getUniqueId().toString());
- ResultSet result1 = ps1.executeQuery();
- result1.next();
- double kills = result1.getInt("Kills");
- result1.close();
- ps1.close();
- PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps2.setString(1, p.getUniqueId().toString());
- ResultSet result2 = ps2.executeQuery();
- result2.next();
- double deaths = result2.getInt("Deaths");
- result2.close();
- ps2.close();
- if ((kills == 0.0D) && (deaths == 0.0D)) {
- return 0.0D;
- }
- if (deaths == 0.0D) {
- return kills;
- }
- return Math.round(kills / deaths * 100.0D) / 100.0D;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1.0D;
- }
- public static double getKD(UUID uuid) {
- try {
- PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps1.setString(1, uuid.toString());
- ResultSet result1 = ps1.executeQuery();
- result1.next();
- double kills = result1.getInt("Kills");
- result1.close();
- ps1.close();
- PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps2.setString(1, uuid.toString());
- ResultSet result2 = ps2.executeQuery();
- result2.next();
- double deaths = result2.getInt("Deaths");
- result2.close();
- ps2.close();
- if ((kills == 0.0D) && (deaths == 0.0D)) {
- return 0.0D;
- }
- if (deaths == 0.0D) {
- return kills;
- }
- return Math.round(kills / deaths * 100.0D) / 100.0D;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1.0D;
- }
- public static int getGamesPlayed(Player p) {
- try {
- PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps1.setString(1, p.getUniqueId().toString());
- ResultSet result1 = ps1.executeQuery();
- result1.next();
- int wins = result1.getInt("Wins");
- result1.close();
- ps1.close();
- PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps2.setString(1, p.getUniqueId().toString());
- ResultSet result2 = ps2.executeQuery();
- result2.next();
- int looses = result2.getInt("Looses");
- result2.close();
- ps2.close();
- PreparedStatement ps3 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps3.setString(1, p.getUniqueId().toString());
- ResultSet result3 = ps3.executeQuery();
- result3.next();
- int undecideds = result3.getInt("Draws");
- result3.close();
- ps3.close();
- return wins + looses + undecideds;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getGamesPlayed(UUID uuid) {
- try {
- PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps1.setString(1, uuid.toString());
- ResultSet result1 = ps1.executeQuery();
- result1.next();
- int wins = result1.getInt("Wins");
- result1.close();
- ps1.close();
- PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps2.setString(1, uuid.toString());
- ResultSet result2 = ps2.executeQuery();
- result2.next();
- int looses = result2.getInt("Looses");
- result2.close();
- ps2.close();
- PreparedStatement ps3 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps3.setString(1, uuid.toString());
- ResultSet result3 = ps3.executeQuery();
- result3.next();
- int undecideds = result3.getInt("Draws");
- result3.close();
- ps3.close();
- return wins + looses + undecideds;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getWins(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int wins = result.getInt("Wins");
- result.close();
- ps.close();
- return wins;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getWins(UUID uuid) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int wins = result.getInt("Wins");
- result.close();
- ps.close();
- return wins;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getLooses(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int looses = result.getInt("Looses");
- result.close();
- ps.close();
- return looses;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getLooses(UUID uuid) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int looses = result.getInt("Looses");
- result.close();
- ps.close();
- return looses;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getUndecideds(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int wins = result.getInt("Draws");
- result.close();
- ps.close();
- return wins;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static int getUndecideds(UUID uuid) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps.setString(1, uuid.toString());
- ResultSet result = ps.executeQuery();
- result.next();
- int wins = result.getInt("Draws");
- result.close();
- ps.close();
- return wins;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1;
- }
- public static double getWL(Player p) {
- try {
- PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps1.setString(1, p.getUniqueId().toString());
- ResultSet result1 = ps1.executeQuery();
- result1.next();
- double wins = result1.getInt("Wins");
- result1.close();
- ps1.close();
- PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps2.setString(1, p.getUniqueId().toString());
- ResultSet result2 = ps2.executeQuery();
- result2.next();
- double looses = result2.getInt("Looses");
- result2.close();
- ps2.close();
- if ((wins == 0.0D) && (looses == 0.0D)) {
- return 0.0D;
- }
- if (looses == 0.0D) {
- return wins;
- }
- return Math.round(wins / looses * 100.0D) / 100.0D;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1.0D;
- }
- public static double getWL(UUID uuid) {
- try {
- PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps1.setString(1, uuid.toString());
- ResultSet result1 = ps1.executeQuery();
- result1.next();
- double wins = result1.getInt("Wins");
- result1.close();
- ps1.close();
- PreparedStatement ps2 = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player WHERE UUID = ?");
- ps2.setString(1, uuid.toString());
- ResultSet result2 = ps2.executeQuery();
- result2.next();
- double looses = result2.getInt("Looses");
- result2.close();
- ps2.close();
- if ((wins == 0.0D) && (looses == 0.0D)) {
- return 0.0D;
- }
- if (looses == 0.0D) {
- return wins;
- }
- return Math.round(wins / looses * 100.0D) / 100.0D;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return -1.0D;
- }
- public static int getRank(Player p) {
- int rank = -1;
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player ORDER BY Wins DESC");
- ResultSet result = ps.executeQuery();
- while (result.next()) {
- String uuid2 = result.getString("UUID");
- if (uuid2.equalsIgnoreCase(p.getUniqueId().toString())) {
- rank = result.getRow();
- break;
- }
- }
- result.close();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return rank;
- }
- public static int getRank(UUID uuid) {
- int rank = -1;
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM QuickSG_Stats_Player ORDER BY Wins DESC");
- ResultSet result = ps.executeQuery();
- while (result.next()) {
- String uuid2 = result.getString("UUID");
- if (uuid2.equalsIgnoreCase(uuid.toString())) {
- rank = result.getRow();
- break;
- }
- }
- result.close();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return rank;
- }
- public static void addKill(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("UPDATE QuickSG_Stats_Player SET Kills = ? WHERE uuid = ?");
- ps.setInt(1, getKills(p) + 1);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static void addDeath(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("UPDATE QuickSG_Stats_Player SET Deaths = ? WHERE uuid = ?");
- ps.setInt(1, getDeaths(p) + 1);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static void addWin(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("UPDATE QuickSG_Stats_Player SET Wins = ? WHERE uuid = ?");
- ps.setInt(1, getWins(p) + 1);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static void addLoose(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("UPDATE QuickSG_Stats_Player SET Looses = ? WHERE uuid = ?");
- ps.setInt(1, getLooses(p) + 1);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static void addUndecided(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("UPDATE QuickSG_Stats_Player SET Draws = ? WHERE uuid = ?");
- ps.setInt(1, getUndecideds(p) + 1);
- ps.setString(2, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement