Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.gg.utils;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MySQL {
- public static Connection con;
- public static Connection getCon() {
- return con;
- }
- public static void connect() {
- try {
- con = DriverManager.getConnection(
- "jdbc:mysql://" + CFGManager.MYSQLcfg.getString("HOST") + ":" + CFGManager.MYSQLcfg.getInt("PORT")
- + "/" + CFGManager.MYSQLcfg.getString("DATABASE") + "?autoReconnect=true",
- CFGManager.MYSQLcfg.getString("USER"), CFGManager.MYSQLcfg.getString("PASSWORD"));
- createTable();
- createNickTable();
- System.out.println("§7[§cMySQL§7] §cVerbindung hergestellt!");
- } catch (SQLException e) {
- System.out.println("§7[§cMySQL§7] §cDie Verbindung ist fehlgeschlagen! Error: " + e.getMessage());
- }
- }
- public static boolean nick(String uuid) {
- boolean s = false;
- if (isConnected()) {
- ResultSet rs = query("SELECT * FROM Nick WHERE UUID= '" + uuid + "'");
- try {
- if (rs.getString("joinNicked") == "true") {
- s = true;
- }
- } catch (SQLException e) {
- }
- }
- return s;
- }
- public static void resetnick(String uuid) {
- if (isConnected()) {
- if (playerExist(uuid)) {
- update("UPDATE Nick SET Nickname= '' WHERE UUID= '" + uuid + "';");
- update("UPDATE Nick SET isNicked= 'false' WHERE UUID= '" + uuid + "';");
- } else {
- createPlayer(uuid);
- }
- }
- }
- public static boolean isConnected() {
- return getCon() != null;
- }
- public static void update(String qry) {
- if (isConnected()) {
- try {
- PreparedStatement ps = getCon().prepareStatement(qry);
- ps.executeUpdate();
- } catch (SQLException exc) {
- exc.printStackTrace();
- }
- }
- }
- public static ResultSet query(String qry) {
- ResultSet rs;
- try {
- PreparedStatement ps = getCon().prepareStatement(qry);
- rs = ps.executeQuery();
- return rs;
- } catch (SQLException exc) {
- }
- return null;
- }
- public static void close() {
- if (isConnected()) {
- try {
- getCon().close();
- } catch (SQLException exc) {
- exc.printStackTrace();
- }
- }
- }
- public static void createTable() {
- if (isConnected()) {
- update("CREATE TABLE IF NOT EXISTS GunGame(UUID varchar(64), Kills int, Deaths int, Level int)");
- }
- }
- public static void createNickTable() {
- if (isConnected()) {
- update("CREATE TABLE IF NOT EXISTS Nick (UUID VARCHAR(64), joinNicked VARCHAR(10), Nickname VARCHAR(16), isNicked VARCHAR(10)");
- }
- }
- public static boolean playerExist(String uuid) {
- if (isConnected()) {
- try {
- if (con != null) {
- ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
- if (rs.next()) {
- return rs.getString("UUID") != null;
- }
- }
- } catch (SQLException exc) {
- exc.printStackTrace();
- }
- }
- return false;
- }
- public static void createPlayer(String uuid) {
- if (isConnected()) {
- if (!playerExist(uuid)) {
- update("INSERT INTO GunGame(UUID, Kills, Deaths, Level) VALUES ('" + uuid + "', '0', '0', '0');");
- }
- }
- }
- public static void addKill(String uuid) {
- if (isConnected()) {
- if (playerExist(uuid)) {
- ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
- try {
- update("UPDATE GunGame SET Kills= '" + rs.getInt("Kills") + 1 + "' WHERE UUID= '" + uuid + "';");
- } catch (SQLException e) {
- }
- } else {
- createPlayer(uuid);
- }
- }
- }
- public static void addDeath(String uuid) {
- if (isConnected()) {
- if (playerExist(uuid)) {
- ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
- try {
- update("UPDATE GunGame SET Deaths= '" + rs.getInt("Deaths") + 1 + "' WHERE UUID= '" + uuid + "';");
- } catch (SQLException e) {
- }
- } else {
- createPlayer(uuid);
- }
- }
- }
- public static void removePlayer(String uuid) {
- if (isConnected()) {
- if (playerExist(uuid)) {
- update("DELETE * FROM GunGame WHERE UUID= '" + uuid + "'");
- }
- }
- }
- public static int getKills(String uuid) {
- int i = 0;
- if (isConnected()) {
- if (playerExist(uuid)) {
- ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
- try {
- i = rs.getInt("Kills");
- } catch (SQLException e) {
- }
- } else {
- createPlayer(uuid);
- }
- }
- return i;
- }
- public static int getDeaths(String uuid) {
- int i = 0;
- if (isConnected()) {
- if (playerExist(uuid)) {
- ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
- try {
- i = rs.getInt("Deaths");
- } catch (SQLException e) {
- }
- } else {
- createPlayer(uuid);
- }
- }
- return i;
- }
- public static void setHighestLevel(String uuid, int Level) {
- if (isConnected()) {
- if (playerExist(uuid)) {
- update("UPDATE GunGame SET Level= '" + Level + "' WHERE UUID= '" + uuid + "';");
- } else {
- createPlayer(uuid);
- }
- }
- }
- public static int getHighestLevel(String uuid) {
- int i = 0;
- if (isConnected()) {
- if (playerExist(uuid)) {
- ResultSet rs = query("SELECT * FROM GunGame WHERE UUID= '" + uuid + "'");
- try {
- i = rs.getInt("Level");
- } catch (SQLException e) {
- }
- } else {
- createPlayer(uuid);
- }
- }
- return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement