Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.lorenzo.methods;
- 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;
- public class MySQL {
- public static String host = "localhost";
- public static String port = "3306";
- public static String database = "Nick";
- public static String username = "root";
- public static String password = "etygyfeL";
- public static Connection connection;
- public static Connection getConnection() {
- return connection;
- }
- public static void connectMySQL() {
- if (connection == null){
- try {
- connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public static void disconnect() {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public static void createTableIfNotExist() {
- if (connection != null) {
- try {
- PreparedStatement ps = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS Nick (Spielername VARCHAR(100), UUID VARCHAR(100))");
- ps.executeUpdate();
- ps.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- public static boolean isPlayerExisting(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("SELECT * FROM Nick WHERE UUID = ?");
- ps.setString(1, p.toString());
- ResultSet result = ps.executeQuery();
- boolean isExisting = result.next();
- result.close();
- ps.close();
- return isExisting;
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return false;
- }
- public static void registerPlayer(Player p) {
- if (isPlayerExisting(p)) {
- return;
- }
- try {
- PreparedStatement ps = connection.prepareStatement("INSERT INTO Nick (Spielername, UUID) VALUES (?, ?)");
- ps.setString(1, p.getName());
- ps.setString(2, p.getUniqueId().toString());
- ps.execute();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static void setPlayerName(UUID uuid, String name) {
- try {
- PreparedStatement ps = connection.prepareStatement("UPDATE Nick SET Spielername = ? WHERE UUID = ?");
- ps.setString(1, name);
- ps.setString(2, uuid.toString());
- ps.executeUpdate();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public static void unregisterPlayer(Player p) {
- try {
- PreparedStatement ps = connection.prepareStatement("DELETE FROM Nick WHERE UUID = ?");
- ps.setString(1, p.getUniqueId().toString());
- ps.executeUpdate();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- //ONENABLE
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement