· 8 years ago · Feb 24, 2018, 02:50 PM
1package com.extremepvp.MySQL;
2
3import java.util.concurrent.locks.*;
4
5import com.extremepvp.*;
6
7import java.sql.*;
8import java.util.concurrent.*;
9
10public class Connect {
11 private Main m;
12 public static ReentrantLock lock;
13
14 static {
15 Connect.lock = new ReentrantLock(true);
16 }
17
18 public Connect(final Main m) {
19 this.m = m;
20 }
21
22 public synchronized Connection trySQLConnection() {
23 if (!this.m.sql) {
24 this.m.getLogger().info("MySQL Desativado!");
25 return null;
26 }
27 try {
28 this.m.getLogger().info("Conectando ao MySQL");
29 Class.forName("com.mysql.jdbc.Driver").newInstance();
30 final String ip = Main.getInstance().getConfig().getString("sql-host");
31 this.m.getLogger().info("Tentando conexão com o mysql em " + ip);
32 final String conn = "jdbc:mysql://" + ip + ":" + 3306 + "/"
33 + Main.getInstance().getConfig().getString("sql-db")
34 + "?autoReconnect=true&failOverReadOnly=false&maxReconnects=2147483647";
35 return DriverManager.getConnection(conn, "root",
36 "");
37 } catch (ClassNotFoundException ex2) {
38 this.m.getLogger().warning("MySQL Driver nao encontrado!");
39 this.m.sql = false;
40 } catch (SQLException ex) {
41 this.m.getLogger().warning("Erro enquanto tentava conectar ao Mysql!");
42 ex.printStackTrace();
43 this.m.sql = false;
44 } catch (Exception ex3) {
45 this.m.getLogger().warning("Erro desconhecido enquanto tentava conectar ao MySQL.");
46 this.m.sql = false;
47 }
48 return null;
49 }
50
51 public void prepareSQL(final Connection con) {
52 if (this.m.sql) {
53 this.SQLQuerySync(
54 "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 ;");
55 this.SQLQuerySync(
56 "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 ;");
57 this.SQLQuerySync(
58 "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 ;");
59 this.SQLQuerySync(
60 "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 ;");
61 this.SQLQuerySync(
62 "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 ;");
63 this.SQLQuerySync(
64 "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 ;");
65 this.SQLQuerySync(
66 "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 ;");
67 this.SQLQuerySync(
68 "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 ;");
69 this.SQLQuerySync(
70 "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 ;");
71 this.SQLQuerySync(
72 "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 ;");
73 this.SQLQuerySync(
74 "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 ;");
75 this.SQLQuerySync(
76 "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 ;");
77 this.SQLQuerySync(
78 "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 ;");
79 this.SQLQuerySync(
80 "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 ;");
81 this.SQLQuerySync(
82 "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 ;");
83 this.m.getLogger().info("Criando Tabelas no SQL");
84 }
85 }
86
87 public static void SQLdisconnect(final Connection con) {
88 try {
89 if (con != null && !con.isClosed()) {
90 con.close();
91 }
92 } catch (SQLException e) {
93 e.printStackTrace();
94 }
95 }
96
97 public void SQLdisconnect() {
98 try {
99 if (this.m.mainConnection != null && !this.m.mainConnection.isClosed()) {
100 this.m.mainConnection.close();
101 }
102 } catch (SQLException e) {
103 e.printStackTrace();
104 }
105 }
106
107 public void SQLQuery(final String sql) {
108 if (!this.m.sql) {
109 return;
110 }
111 final ExecutorService executor = Executors.newCachedThreadPool();
112 executor.execute(new Thread(new Runnable() {
113 @Override
114 public void run() {
115 Connect.lock.lock();
116 try {
117 final Statement stmt = Connect.this.m.mainConnection.createStatement();
118 stmt.executeUpdate(sql);
119 stmt.close();
120 } catch (SQLException e) {
121 Connect.this.m.getLogger().info("Erro ao tentar executar Query");
122 Connect.this.m.getLogger().info(sql);
123 Connect.this.m.getLogger().info(e.getMessage());
124 }
125 Connect.lock.unlock();
126 }
127 }));
128 executor.shutdown();
129 }
130
131 public void SQLQuerySync(final String sql) {
132 if (!this.m.sql) {
133 return;
134 }
135 try {
136 final Statement stmt = this.m.mainConnection.createStatement();
137 stmt.executeUpdate(sql);
138 stmt.close();
139 } catch (SQLException e) {
140 this.m.getLogger().info("Erro ao tentar executar Query");
141 this.m.getLogger().info(sql);
142 this.m.getLogger().info(e.getMessage());
143 }
144 }
145}