Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.leon.ascalterfriends.sql;
- import net.md_5.bungee.config.Configuration;
- import net.md_5.bungee.config.ConfigurationProvider;
- import net.md_5.bungee.config.YamlConfiguration;
- import java.io.File;
- import java.io.IOException;
- import java.sql.*;
- public class MySQL {
- public static Connection publiccon;
- private static Connection privatecon;
- public static String host = "";
- public static String datenbankname = "";
- public static String benutzername = "";
- public static String passwort = "";
- public static void connect() {
- try {
- publiccon = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + datenbankname + "?autoReconnect=true", benutzername, passwort);
- System.out.println("§aDie MySQL Verbindung konnte hergestellt werden");
- try {
- File mysql = new File("plugins/system/mysql.yml");
- Configuration cfg;
- cfg = ConfigurationProvider.getProvider(YamlConfiguration.class).load(mysql);
- host = cfg.getString("MySQL.host");
- benutzername = cfg.getString("MySQL.benutzername");
- passwort = cfg.getString("MySQL.passwort");
- datenbankname = cfg.getString("MySQL.datenbankname");
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- } catch (SQLException e) {
- System.out.println("§cDie MySQL Verbindung ist gescheitert, folgender Fehler liegt vor: §7");
- e.printStackTrace();
- }
- }
- public static void disconnect() {
- if (publiccon != null) {
- try {
- publiccon.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static boolean isRegistered(String uuid) {
- try {
- PreparedStatement tabelle = publiccon.prepareStatement("SELECT * FROM Info WHERE uuid=?");
- tabelle.setString(1, uuid);
- ResultSet result = tabelle.executeQuery();
- boolean Contains = result.next();
- tabelle.close();
- result.close();
- return Contains;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public static void register(String uuid) {
- try {
- PreparedStatement tabelle = publiccon.prepareStatement("INSERT INTO Info values(?)");
- tabelle.setString(1, uuid);
- tabelle.execute();
- tabelle.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void update(String qry) {
- Statement st = null;
- try {
- st = publiccon.createStatement();
- st.executeUpdate(qry);
- } catch (Exception e) {
- connect();
- System.err.println(e);
- }
- closeStatement(st);
- }
- public static void closeStatement(Statement st) {
- if (st != null)
- try {
- st.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void createTables() {
- MySQL.update("CREATE TABLE IF NOT EXISTS Online(name varchar(32),Server varchar(200))");
- MySQL.update("CREATE TABLE IF NOT EXISTS Freunde(uuid varchar(100),uuidfriend varchar(100))");
- MySQL.update("CREATE TABLE IF NOT EXISTS Info(name varchar(32),uuid varchar(100))");
- }
- public static void setOrdnerAndConfig() {
- File ordner = new File("plugins/system");
- if (!ordner.exists()) {
- ordner.mkdir();
- }
- File mysql = new File("plugins/system/mysql.yml");
- if (!mysql.exists()) {
- try {
- mysql.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- return;
- }
- try {
- Configuration cfg = ConfigurationProvider.getProvider(YamlConfiguration.class).load(mysql);
- cfg.set("MySQL.host", "localhost");
- cfg.set("MySQL.benutzername", "root");
- cfg.set("MySQL.passwort", "123");
- cfg.set("MySQL.datanbankname", "freundesys");
- ConfigurationProvider.getProvider(YamlConfiguration.class).save(cfg, mysql);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement