Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.extremepvp.MySQL;
- import java.util.concurrent.locks.*;
- import com.extremepvp.*;
- import java.sql.*;
- import java.util.concurrent.*;
- public class Connect {
- public static Connection con;
- private String HOST = "localhost";
- private String DATABASE = "test";
- private String USER = "root";
- private String PASSWORD = "";
- private Main m;
- public static ReentrantLock lock;
- static {
- Connect.lock = new ReentrantLock(true);
- }
- public Connect(final Main m) {
- this.m = m;
- }
- public Connect(String host, String database, String user, String password) {
- HOST = host;
- DATABASE = database;
- USER = user;
- PASSWORD = password;
- connectSql();
- prepareSQL();
- }
- public synchronized void connectSql() {
- if (!alreadyConnected()) {
- try {
- con = java.sql.DriverManager.getConnection(
- "jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
- System.out.println("As conexoes primarias e secundarias foram estabelecidas.");
- System.out.println("As conexoes foram feitas com sucesso.");
- } catch (SQLException e) {
- System.out.println("Erro ao iniciar as conexoes do mysql." + e.getMessage());
- }
- }
- }
- public static boolean alreadyConnected() {
- return con != null;
- }
- public static void disconnect() {
- try {
- con.close();
- System.out.print("O servidor se desconectou do MYSQL.");
- } catch (SQLException localSQLException) {
- }
- }
- public static PreparedStatement getStatement(String sql) {
- if (alreadyConnected()) {
- try {
- return con.prepareStatement(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public static ResultSet getResult(String sql) {
- if (alreadyConnected()) {
- try {
- PreparedStatement ps = getStatement(sql);
- return ps.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- public void prepareSQL() {
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `playertitles` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Uuid` varchar(255) NOT NULL, `titles` varchar(255), `activetitle` int(10), PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `playerranking` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Uuid` varchar(255) NOT NULL, `coins` int(10), `rewards` varchar(255), `xp` int(10), `xptotal` int(10), `level` int(10), PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `ranks` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `uuid` varchar(255) NOT NULL, `rank` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `Expires` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `uuid` varchar(255) NOT NULL, `expire` varchar(255) NOT NULL, `group` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `multiplier` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `multiplier` int(10), PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `uuidfetcher` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `uuid` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `lastip` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `rewards` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Uuid` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `copahg` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT, `uuid` varchar(255) NOT NULL, `grupo` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `motd` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `server` varchar(255) NOT NULL, `motd` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `broadcast` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `message` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `changelog` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `changes` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `clans` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `tag` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `clans_players` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `uuid` varchar(255) NOT NULL, `rank` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `clans_status` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `kills` int(10), `deaths` int(10), `wins` int(10), `coins` int(10), `elo` int(10), PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- this.SQLQuerySync(
- "CREATE TABLE IF NOT EXISTS `fakebanned` (`ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Uuid` varchar(255) NOT NULL, PRIMARY KEY (`ID`)) ENGINE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;");
- m.getLogger().info("Criando Tabelas no SQL");
- }
- public static void SQLdisconnect(final Connection con) {
- try {
- if (con != null && !con.isClosed()) {
- con.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void SQLdisconnect() {
- try {
- if (this.m.mainConnection != null && !this.m.mainConnection.isClosed()) {
- this.m.mainConnection.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void SQLQuery(final String sql) {
- if (!this.m.sql) {
- return;
- }
- final ExecutorService executor = Executors.newCachedThreadPool();
- executor.execute(new Thread(new Runnable() {
- @Override
- public void run() {
- Connect.lock.lock();
- try {
- final Statement stmt = Connect.this.m.mainConnection.createStatement();
- stmt.executeUpdate(sql);
- stmt.close();
- } catch (SQLException e) {
- Connect.this.m.getLogger().info("Erro ao tentar executar Query");
- Connect.this.m.getLogger().info(sql);
- Connect.this.m.getLogger().info(e.getMessage());
- }
- Connect.lock.unlock();
- }
- }));
- executor.shutdown();
- }
- public Connection getConnection() {
- return con;
- }
- public void SQLQuerySync(final String sql) {
- try {
- final Statement stmt = getConnection().prepareStatement(sql);
- stmt.executeUpdate(sql);
- stmt.close();
- } catch (SQLException e) {
- this.m.getLogger().info("Erro ao tentar executar Query");
- this.m.getLogger().info(sql);
- this.m.getLogger().info(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement