Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.phil.mysql;
- import org.bukkit.Bukkit;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import java.io.*;
- import java.sql.*;
- /* created by fphil */
- public class MySQLManager {
- public static String database;
- private static Connection con;
- private static String host;
- private static String port;
- private static String username;
- private static String password;
- public static boolean isConnected ( ) {
- return con != null;
- }
- public static void createfile ( ) {
- File file = new File ( "plugins//BeastAPI//mysql//mysql.yml" );
- YamlConfiguration cfg = YamlConfiguration.loadConfiguration ( file );
- if ( ! file.exists ( ) ) {
- cfg.set ( "host" , "localhost" );
- cfg.set ( "port" , "3306" );
- cfg.set ( "database" , "database" );
- cfg.set ( "username" , "root" );
- cfg.set ( "password" , "password" );
- try {
- cfg.save ( file );
- } catch ( IOException e ) {
- e.printStackTrace ( );
- }
- }
- host = cfg.getString ( "host" );
- port = cfg.getString ( "port" );
- database = cfg.getString ( "database" );
- username = cfg.getString ( "username" );
- password = cfg.getString ( "password" );
- }
- public static void connect ( ) {
- if ( ! isConnected ( ) ) {
- try {
- con = DriverManager.getConnection ( "jdbc:mysql://" +
- host + ":" + port + "/" + database + "?autoReconnect=true" , username ,
- password );
- System.out.println ( "CONNECTED" );
- } catch ( SQLException ex ) {
- Bukkit.getConsoleSender ( ).sendMessage ( database );
- Bukkit.getConsoleSender ( ).sendMessage ( "§cES IST EIN FEHLER MIT DER MYSQL VERBINDUNG AUFGETRETEN , MEHR DARÜBER IN DEN LOGS!" );
- File file = new File ( "plugins//BeastAPI//mysql//logs//" + System.currentTimeMillis ( ) + ".yml" );
- YamlConfiguration cfg = YamlConfiguration.loadConfiguration ( file );
- Writer writer = new StringWriter ( );
- ex.printStackTrace ( new PrintWriter ( writer ) );
- String error = writer.toString ( );
- cfg.set ( "ERROR" , error );
- if ( ! file.exists ( ) ) {
- try {
- cfg.save ( file );
- } catch ( IOException e ) {
- e.printStackTrace ( );
- }
- }
- }
- }
- }
- public static void disconnect ( ) {
- try {
- if ( con != null ) {
- con.close ( );
- System.out.println ( "DISCONNECT" );
- }
- } catch ( SQLException ex ) {
- ex.printStackTrace ( );
- }
- }
- public static void update ( String qry ) {
- try {
- Statement st = con.createStatement ( );
- st.executeUpdate ( qry );
- st.close ( );
- } catch ( SQLException ex ) {
- connect ( );
- ex.printStackTrace ( );
- }
- }
- public static ResultSet query ( String qry ) {
- ResultSet rs = null;
- try {
- Statement st = con.createStatement ( );
- rs = st.executeQuery ( qry );
- } catch ( SQLException ex ) {
- connect ( );
- ex.printStackTrace ( );
- }
- return rs;
- }
- public static void createTable ( String table , String data ) {
- update ( "CREATE TABLE IF NOT EXISTS " + table + "(" + data + ");" );
- }
- public static void insert ( String table , Player player , String points ) {
- update ( "INSERT INTO " + table + " (uuid,name,points) VALUES ('" + player.getPlayer ( ).getUniqueId ( ).toString ( ) + "','" + player.getPlayer ( ).getName ( ) + "','" + points + "');" );
- }
- public static void update ( Player player , String table , String what , String value ) {
- MySQLManager.update ( "UPDATE " + table + " SET " + what + "='" + value + "' WHERE UUID='" + player.getUniqueId ( ).toString ( ) + "';" );
- }
- public static boolean exists ( String uuid , String table ) {
- try {
- ResultSet rs = MySQLManager.query ( ( "SELECT * FROM " + table + " WHERE UUID= '" + uuid + "'" ) );
- if ( rs.next ( ) )
- return rs.getString ( "UUID" ) != null;
- return false;
- } catch ( SQLException e ) {
- e.printStackTrace ( );
- }
- return false;
- }
- public static String get ( String uuid , String what , String get , String table ) {
- try {
- ResultSet rs = MySQLManager.query ( ( "SELECT * FROM " + table + " WHERE " + what + "= '" + uuid + "'" ) );
- if ( rs.next ( ) )
- return rs.getString ( get );
- } catch ( SQLException e ) {
- e.printStackTrace ( );
- }
- return uuid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement