· 8 years ago · Feb 26, 2018, 06:14 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
12 public static Connection con;
13 private String HOST = "localhost";
14 private String DATABASE = "test";
15 private String USER = "root";
16 private String PASSWORD = "";
17 private Main m;
18 public static ReentrantLock lock;
19
20 static {
21 Connect.lock = new ReentrantLock(true);
22 }
23
24 public Connect(final Main m) {
25 this.m = m;
26 }
27
28 public Connect(String host, String database, String user, String password) {
29 HOST = host;
30 DATABASE = database;
31 USER = user;
32 PASSWORD = password;
33 connectSql();
34 prepareSQL();
35 }
36
37 public synchronized void connectSql() {
38 if (!alreadyConnected()) {
39 try {
40 con = java.sql.DriverManager.getConnection(
41 "jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
42 System.out.println("As conexoes primarias e secundarias foram estabelecidas.");
43 System.out.println("As conexoes foram feitas com sucesso.");
44 } catch (SQLException e) {
45 System.out.println("Erro ao iniciar as conexoes do mysql." + e.getMessage());
46
47 }
48 }
49 }
50
51 public static boolean alreadyConnected() {
52 return con != null;
53 }
54
55 public static void disconnect() {
56 try {
57 con.close();
58 System.out.print("O servidor se desconectou do MYSQL.");
59 } catch (SQLException localSQLException) {
60 }
61 }
62
63 public static PreparedStatement getStatement(String sql) {
64 if (alreadyConnected()) {
65 try {
66 return con.prepareStatement(sql);
67 } catch (SQLException e) {
68 e.printStackTrace();
69 }
70 }
71 return null;
72 }
73
74 public static ResultSet getResult(String sql) {
75 if (alreadyConnected()) {
76 try {
77 PreparedStatement ps = getStatement(sql);
78 return ps.executeQuery();
79 } catch (SQLException e) {
80 e.printStackTrace();
81 }
82 }
83 return null;
84 }
85
86 public void prepareSQL() {
87 this.SQLQuerySync(
88 "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 ;");
89 this.SQLQuerySync(
90 "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 ;");
91 this.SQLQuerySync(
92 "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 ;");
93 this.SQLQuerySync(
94 "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 ;");
95 this.SQLQuerySync(
96 "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 ;");
97 this.SQLQuerySync(
98 "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 ;");
99 this.SQLQuerySync(
100 "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 ;");
101 this.SQLQuerySync(
102 "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 ;");
103 this.SQLQuerySync(
104 "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 ;");
105 this.SQLQuerySync(
106 "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 ;");
107 this.SQLQuerySync(
108 "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 ;");
109 this.SQLQuerySync(
110 "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 ;");
111 this.SQLQuerySync(
112 "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 ;");
113 this.SQLQuerySync(
114 "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 ;");
115 this.SQLQuerySync(
116 "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 ;");
117 m.getLogger().info("Criando Tabelas no SQL");
118 }
119
120 public static void SQLdisconnect(final Connection con) {
121 try {
122 if (con != null && !con.isClosed()) {
123 con.close();
124 }
125 } catch (SQLException e) {
126 e.printStackTrace();
127 }
128 }
129
130 public void SQLdisconnect() {
131 try {
132 if (this.m.mainConnection != null && !this.m.mainConnection.isClosed()) {
133 this.m.mainConnection.close();
134 }
135 } catch (SQLException e) {
136 e.printStackTrace();
137 }
138 }
139
140 public void SQLQuery(final String sql) {
141 if (!this.m.sql) {
142 return;
143 }
144 final ExecutorService executor = Executors.newCachedThreadPool();
145 executor.execute(new Thread(new Runnable() {
146 @Override
147 public void run() {
148 Connect.lock.lock();
149 try {
150 final Statement stmt = Connect.this.m.mainConnection.createStatement();
151 stmt.executeUpdate(sql);
152 stmt.close();
153 } catch (SQLException e) {
154 Connect.this.m.getLogger().info("Erro ao tentar executar Query");
155 Connect.this.m.getLogger().info(sql);
156 Connect.this.m.getLogger().info(e.getMessage());
157 }
158 Connect.lock.unlock();
159 }
160 }));
161 executor.shutdown();
162 }
163
164 public Connection getConnection() {
165 return con;
166 }
167
168 public void SQLQuerySync(final String sql) {
169
170 try {
171 final Statement stmt = getConnection().prepareStatement(sql);
172 stmt.executeUpdate(sql);
173 stmt.close();
174 } catch (SQLException e) {
175 this.m.getLogger().info("Erro ao tentar executar Query");
176 this.m.getLogger().info(sql);
177 this.m.getLogger().info(e.getMessage());
178 }
179 }
180}