Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.lp.bs.sys.mysql.sys;
- import de.lp.bs.sys.Bungee;
- import net.md_5.bungee.api.ChatColor;
- import net.md_5.bungee.api.ProxyServer;
- 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.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MYSQL {
- public static String username;
- public static String password;
- public static String database;
- public static String host;
- public static String port;
- public static Connection con;
- public MYSQL() {}
- public static boolean connect() {
- if (!isConnected()) {
- try {
- con = DriverManager.getConnection( "jdbc:mysql://" + host + ":" + port + "/" + database, username, password );
- ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_GREEN + " Datenbank Verbindindung aufgebaut..." );
- } catch (SQLException e) {
- e.printStackTrace();
- ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_RED + " Keine Verbindung zur Datenbank!" );
- ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_RED + " Bitte pruefe die DB Einstellungen!" );
- }
- }
- return true;
- }
- public static boolean isConnected() {return con != null;}
- public static void createTable() {
- try{
- /** PLAYERLIST **/
- con.createStatement().executeQuery("");
- ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_GREEN + "MYSQL Tabelle wurde erstellt!" );
- }catch (SQLException e){
- e.printStackTrace();
- ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_RED + "MYSQL Tabelle konnte nicht erstellt werden!" );
- }
- }
- public static void update(String qry) {
- if (!isConnected()) {
- try {
- con.createStatement().executeQuery( qry );
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static ResultSet getResult(String qry){
- if (isConnected()){
- try {
- return con.createStatement().executeQuery( qry );
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- private static File getMySQLFile() {
- return new File( "plugins/ChatClear", "mysql.yml" );
- }
- private static Configuration getMySQLFileConfig() {
- try {
- if (!Bungee.getInstance().getProxy().getPluginsFolder().exists()) {
- Bungee.getInstance().getProxy().getPluginsFolder().mkdir();
- File file = new File(Bungee.getInstance().getDataFolder().getPath(), "mysql.lp");
- if (!file.exists()){
- file.createNewFile();
- }
- Configuration config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
- config.set("test", true);
- ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, file);
- }
- }catch (IOException e){
- e.printStackTrace();
- }
- return null;
- }
- public static void setStandardMySQL() {
- Configuration cfg = getMySQLFileConfig();
- cfg.set( "username", "root" );
- cfg.set( "password" , "password" );
- cfg.set( "database", "database");
- cfg.set( "host" , "localhost");
- cfg.set( "port", "3306");
- try {
- ConfigurationProvider.getProvider(YamlConfiguration.class).save(cfg, getMySQLFile());
- }catch (IOException e) {
- e.printStackTrace();
- ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_RED + " Es konnte keine MySQL Datei erstellt werden!" );
- }
- }
- public static void readMysql() {
- Configuration cfg = getMySQLFileConfig();
- MYSQL.username = cfg.getString( "username" );
- MYSQL.password = cfg.getString( "password" );
- MYSQL.database = cfg.getString( "database" );
- MYSQL.host = cfg.getString( "host" );
- MYSQL.port = cfg.getString( "port" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement