Advertisement
Guest User

Untitled

a guest
Aug 29th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.14 KB | None | 0 0
  1. package network;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.lang.String;
  12. import java.util.UUID;
  13.  
  14. import net.md_5.bungee.BungeeCord;
  15. import net.md_5.bungee.config.Configuration;
  16. import net.md_5.bungee.config.ConfigurationProvider;
  17. import net.md_5.bungee.config.YamlConfiguration;
  18.  
  19. public enum DB {
  20. PLAYERS_ACCOUNTS("uuid VARCHAR(40), name VARCHAR(16), address VARCHAR(40), rank VARCHAR(20), join_time VARCHAR(10), PRIMARY KEY(uuid)"),
  21.  
  22. NETWORK_PROXIES("server varchar(10), PRIMARY KEY(server)"),
  23. NETWORK_POPULATIONS("server varchar(10), population INT, PRIMARY KEY(server)"),
  24. NETWORK_COMMAND_DISPATCHER("id INT NOT NULL AUTO_INCREMENT, server varchar(10), command varchar(250), PRIMARY KEY(id)"),
  25. NETWORK_SERVER_LIST("data_type varchar(15), data_value varchar(100), PRIMARY KEY(data_type)"),
  26. NETWORK_SERVER_STATUS("id INT NOT NULL AUTO_INCREMENT, game_name VARCHAR(25), server_number INT, listed_priority INT, lore VARCHAR(100), players INT, max_players INT, PRIMARY KEY(id)");
  27.  
  28. private String table = null;
  29. private String keys = "";
  30. private Databases database = null;
  31.  
  32. private DB(String query) {
  33. String databaseName = toString().split("_")[0];
  34. database = Databases.valueOf(databaseName);
  35. table = toString().replaceAll(databaseName, "");
  36. table = table.substring(1, table.length()).toLowerCase();
  37. String [] declarations = query.split(", ");
  38. for(int a = 0; a < declarations.length - 1; ++a) {
  39. String declaration = declarations[a].split(" ")[0];
  40. if(!declaration.equals("id")) {
  41. keys += "`" + declaration + "`, ";
  42. }
  43. }
  44. keys = keys.substring(0, keys.length() - 2);
  45. database.connect();
  46. try {
  47. if(database.getConnection() != null) {
  48. database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS " + table + " (" + query + ")").execute();
  49. }
  50. } catch(SQLException e) {
  51. BungeeCord.getInstance().getLogger().info(e.getMessage());
  52. }
  53. }
  54.  
  55. public String getName() {
  56. return table;
  57. }
  58.  
  59. public Connection getConnection() {
  60. return this.database.getConnection();
  61. }
  62.  
  63. public boolean isKeySet(String key, String value) {
  64. PreparedStatement statement = null;
  65. ResultSet resultSet = null;
  66. try {
  67. statement = getConnection().prepareStatement("SELECT COUNT(" + key + ") FROM " + getName() + " WHERE " + key + " = '" + value + "' LIMIT 1");
  68. resultSet = statement.executeQuery();
  69. return resultSet.next() && resultSet.getInt(1) > 0;
  70. } catch(SQLException e) {
  71. BungeeCord.getInstance().getLogger().info(e.getMessage());
  72. } finally {
  73. close(statement, resultSet);
  74. }
  75. return false;
  76. }
  77.  
  78. public boolean isKeySet(String[] keys, String[] values) {
  79. PreparedStatement statement = null;
  80. ResultSet resultSet = null;
  81. try {
  82. String query = "SELECT COUNT(" + keys[0] + ") FROM " + getName() + " WHERE " + keys[0] + " = '" + values[0] + "'";
  83. for(int a = 1; a < keys.length; ++a) {
  84. query += " AND " + keys[a] + " = '" + values[a] + "'";
  85. }
  86. statement = getConnection().prepareStatement(query + " LIMIT 1");
  87. resultSet = statement.executeQuery();
  88. return resultSet.next() && resultSet.getInt(1) > 0;
  89. } catch(SQLException e) {
  90. BungeeCord.getInstance().getLogger().info(e.getMessage());
  91. } finally {
  92. close(statement, resultSet);
  93. }
  94. return false;
  95. }
  96.  
  97. public boolean isUUIDSet(UUID uuid) {
  98. return isUUIDSet("uuid", uuid);
  99. }
  100.  
  101. public boolean isUUIDSet(String key, UUID uuid) {
  102. return isKeySet(key, uuid.toString());
  103. }
  104.  
  105. public int getInt(String key, String value, String requested) {
  106. PreparedStatement statement = null;
  107. ResultSet resultSet = null;
  108. try {
  109. statement = getConnection().prepareStatement("SELECT " + requested + " FROM " + getName() + " WHERE " + key + " = '" + value + "' LIMIT 1");
  110. resultSet = statement.executeQuery();
  111. if(resultSet.next()) {
  112. return resultSet.getInt(requested);
  113. }
  114. } catch(SQLException e) {
  115. BungeeCord.getInstance().getLogger().info(e.getMessage());
  116. } finally {
  117. close(statement, resultSet);
  118. }
  119. return 0;
  120. }
  121.  
  122. public int getInt(String [] keys, String [] values, String requested) {
  123. PreparedStatement statement = null;
  124. ResultSet resultSet = null;
  125. try {
  126. String query = "SELECT " + requested + " FROM " + getName() + " WHERE " + keys[0] + " = '" + values[0] + "'";
  127. for(int a = 1; a < keys.length; ++a) {
  128. query += " AND " + keys[a] + " = '" + values[a] + "'";
  129. }
  130. statement = getConnection().prepareStatement(query);
  131. resultSet = statement.executeQuery();
  132. if(resultSet.next()) {
  133. return resultSet.getInt(requested);
  134. }
  135. } catch(SQLException e) {
  136. BungeeCord.getInstance().getLogger().info(e.getMessage());
  137. } finally {
  138. close(statement, resultSet);
  139. }
  140. return 0;
  141. }
  142.  
  143. public void updateInt(String set, int update, String key, String value) {
  144. PreparedStatement statement = null;
  145. try {
  146. statement = getConnection().prepareStatement("UPDATE " + getName() + " SET " + set + " = '" + update + "' WHERE " + key + " = '" + value + "'");
  147. statement.execute();
  148. } catch(SQLException e) {
  149. BungeeCord.getInstance().getLogger().info(e.getMessage());
  150. } finally {
  151. close(statement);
  152. }
  153. }
  154.  
  155. public void updateInt(String set, int update, String [] keys, String [] values) {
  156. PreparedStatement statement = null;
  157. try {
  158. String query = "UPDATE " + getName() + " SET " + set + " = '" + update + "' WHERE " + keys[0] + " = '" + values[0] + "'";
  159. for(int a = 0; a < keys.length; ++a) {
  160. query += " AND " + keys[a] + " = '" + values[a] + "'";
  161. }
  162. statement = getConnection().prepareStatement(query);
  163. statement.execute();
  164. } catch(SQLException e) {
  165. BungeeCord.getInstance().getLogger().info(e.getMessage());
  166. } finally {
  167. close(statement);
  168. }
  169. }
  170.  
  171. public String getString(String key, String value, String requested) {
  172. PreparedStatement statement = null;
  173. ResultSet resultSet = null;
  174. try {
  175. statement = getConnection().prepareStatement("SELECT " + requested + " FROM " + getName() + " WHERE " + key + " = '" + value + "' LIMIT 1");
  176. resultSet = statement.executeQuery();
  177. if(resultSet.next()) {
  178. return resultSet.getString(requested);
  179. }
  180. } catch(SQLException e) {
  181. BungeeCord.getInstance().getLogger().info(e.getMessage());
  182. } finally {
  183. close(statement, resultSet);
  184. }
  185. return null;
  186. }
  187.  
  188. public String getString(String [] keys, String [] values, String requested) {
  189. PreparedStatement statement = null;
  190. ResultSet resultSet = null;
  191. try {
  192. String query = "SELECT " + requested + " FROM " + getName() + " WHERE " + keys[0] + " = '" + values[0] + "'";
  193. for(int a = 1; a < keys.length; ++a) {
  194. query += " AND " + keys[a] + " = '" + values[a] + "'";
  195. }
  196. statement = getConnection().prepareStatement(query + " LIMIT 1");
  197. resultSet = statement.executeQuery();
  198. if(resultSet.next()) {
  199. return resultSet.getString(requested);
  200. }
  201. } catch(SQLException e) {
  202. BungeeCord.getInstance().getLogger().info(e.getMessage());
  203. } finally {
  204. close(statement, resultSet);
  205. }
  206. return null;
  207. }
  208.  
  209. public List<String> getAllStrings(String colum) {
  210. return getAllStrings(colum, null, null);
  211. }
  212.  
  213. public List<String> getAllStrings(String colum, String key, String value) {
  214. List<String> results = new ArrayList<String>();
  215. PreparedStatement statement = null;
  216. ResultSet resultSet = null;
  217. try {
  218. String query = "SELECT " + colum + " FROM " + getName();
  219. if(key != null && value != null) {
  220. query += " WHERE " + key + " = '" + value + "'";
  221. }
  222. statement = getConnection().prepareStatement(query);
  223. resultSet = statement.executeQuery();
  224. while(resultSet.next()) {
  225. results.add(resultSet.getString(colum));
  226. }
  227. } catch(SQLException e) {
  228. BungeeCord.getInstance().getLogger().info(e.getMessage());
  229. } finally {
  230. close(statement, resultSet);
  231. }
  232. return results;
  233. }
  234.  
  235. public void updateString(String set, String update, String key, String value) {
  236. PreparedStatement statement = null;
  237. try {
  238. statement = getConnection().prepareStatement("UPDATE " + getName() + " SET " + set + " = '" + update + "' WHERE " + key + " = '" + value + "'");
  239. statement.execute();
  240. } catch(SQLException e) {
  241. BungeeCord.getInstance().getLogger().info(e.getMessage());
  242. } finally {
  243. close(statement);
  244. }
  245. }
  246.  
  247. public void updateString(String set, String update, String [] keys, String [] values) {
  248. PreparedStatement statement = null;
  249. try {
  250. String query = "UPDATE " + getName() + " SET " + set + " = '" + update + "' WHERE " + keys[0] + " = '" + values[0] + "'";
  251. for(int a = 1; a < keys.length; ++a) {
  252. query += " AND " + keys[a] + " = '" + values[a] + "'";
  253. }
  254. statement = getConnection().prepareStatement(query);
  255. statement.execute();
  256. } catch(SQLException e) {
  257. BungeeCord.getInstance().getLogger().info(e.getMessage());
  258. } finally {
  259. close(statement);
  260. }
  261. }
  262.  
  263. public boolean getBoolean(String key, String value, String requested) {
  264. PreparedStatement statement = null;
  265. ResultSet resultSet = null;
  266. try {
  267. statement = getConnection().prepareStatement("SELECT " + requested + " FROM " + getName() + " WHERE " + key + " = '" + value + "'");
  268. resultSet = statement.executeQuery();
  269. return resultSet.next() && resultSet.getBoolean(requested);
  270. } catch(SQLException e) {
  271. BungeeCord.getInstance().getLogger().info(e.getMessage());
  272. } finally {
  273. close(statement, resultSet);
  274. }
  275. return false;
  276. }
  277.  
  278. public int getSize() {
  279. PreparedStatement statement = null;
  280. ResultSet resultSet = null;
  281. try {
  282. statement = getConnection().prepareStatement("SELECT COUNT(*) FROM " + getName());
  283. resultSet = statement.executeQuery();
  284. if(resultSet.next()) {
  285. return resultSet.getInt(1);
  286. }
  287. } catch(SQLException e) {
  288. BungeeCord.getInstance().getLogger().info(e.getMessage());
  289. } finally {
  290. close(statement, resultSet);
  291. }
  292. return 0;
  293. }
  294.  
  295. public int getSize(String key, String value) {
  296. PreparedStatement statement = null;
  297. ResultSet resultSet = null;
  298. try {
  299. statement = getConnection().prepareStatement("SELECT COUNT(" + key + ") FROM " + getName() + " WHERE " + key + " = '" + value + "'");
  300. resultSet = statement.executeQuery();
  301. if(resultSet.next()) {
  302. return resultSet.getInt(1);
  303. }
  304. } catch(SQLException e) {
  305. BungeeCord.getInstance().getLogger().info(e.getMessage());
  306. } finally {
  307. close(statement, resultSet);
  308. }
  309. return 0;
  310. }
  311.  
  312. public int getSize(String [] keys, String [] values) {
  313. PreparedStatement statement = null;
  314. ResultSet resultSet = null;
  315. try {
  316. String query = "SELECT COUNT(" + keys[0] + ") FROM " + getName() + " WHERE " + keys[0] + " = '" + values[0] + "'";
  317. for(int a = 1; a < keys.length; ++a) {
  318. query += " AND " + keys[a] + " = '" + values[a] + "'";
  319. }
  320. statement = getConnection().prepareStatement(query);
  321. resultSet = statement.executeQuery();
  322. if(resultSet.next()) {
  323. return resultSet.getInt(1);
  324. }
  325. } catch(SQLException e) {
  326. BungeeCord.getInstance().getLogger().info(e.getMessage());
  327. } finally {
  328. close(statement, resultSet);
  329. }
  330. return 0;
  331. }
  332.  
  333. public List<String> getOrdered(String orderBy, String requested, String key, String value, int limit) {
  334. return getOrdered(orderBy, requested, key, value, limit, false);
  335. }
  336.  
  337. public List<String> getOrdered(String orderBy, String requested, String key, String value, int limit, boolean descending) {
  338. List<String> results = new ArrayList<String>();
  339. PreparedStatement statement = null;
  340. ResultSet resultSet = null;
  341. try {
  342. String desc = descending ? " DESC " : " ASC ";
  343. String max = limit > 0 ? " LIMIT " + limit : "";
  344. statement = getConnection().prepareStatement("SELECT " + requested + " FROM " + getName() + " WHERE " + key + " = '" + value + "' ORDER BY " + orderBy + desc + max);
  345. resultSet = statement.executeQuery();
  346. while(resultSet.next()) {
  347. results.add(resultSet.getString(requested));
  348. }
  349. return results;
  350. } catch(SQLException e) {
  351. BungeeCord.getInstance().getLogger().info(e.getMessage());
  352. } finally {
  353. close(statement, resultSet);
  354. }
  355. return null;
  356. }
  357.  
  358. public List<String> getOrdered(String orderBy, String requested, int limit) {
  359. return getOrdered(orderBy, requested, limit, false);
  360. }
  361.  
  362. public List<String> getOrdered(String orderBy, String requested, int limit, boolean descending) {
  363. List<String> results = new ArrayList<String>();
  364. PreparedStatement statement = null;
  365. ResultSet resultSet = null;
  366. try {
  367. String desc = descending ? " DESC " : " ASC ";
  368. String max = limit > 0 ? " LIMIT " + limit : "";
  369. statement = getConnection().prepareStatement("SELECT " + requested + " FROM " + getName() + " ORDER BY " + orderBy + desc + max);
  370. resultSet = statement.executeQuery();
  371. while(resultSet.next()) {
  372. results.add(resultSet.getString(requested));
  373. }
  374. return results;
  375. } catch(SQLException e) {
  376. BungeeCord.getInstance().getLogger().info(e.getMessage());
  377. } finally {
  378. close(statement, resultSet);
  379. }
  380. return null;
  381. }
  382.  
  383. public void delete(String key, String value) {
  384. PreparedStatement statement = null;
  385. try {
  386. statement = getConnection().prepareStatement("DELETE FROM " + getName() + " WHERE " + key + " = '" + value + "'");
  387. statement.execute();
  388. } catch(SQLException e) {
  389. BungeeCord.getInstance().getLogger().info(e.getMessage());
  390. } finally {
  391. close(statement);
  392. }
  393. }
  394.  
  395. public void delete(String [] keys, String [] values) {
  396. PreparedStatement statement = null;
  397. try {
  398. String query = "DELETE FROM " + getName() + " WHERE " + keys[0] + " = '" + values[0] + "'";
  399. for(int a = 1; a < keys.length; ++a) {
  400. query += " AND " + keys[a] + " = '" + values[a] + "'";
  401. }
  402. statement = getConnection().prepareStatement(query);
  403. statement.execute();
  404. } catch(SQLException e) {
  405. BungeeCord.getInstance().getLogger().info(e.getMessage());
  406. } finally {
  407. close(statement);
  408. }
  409. }
  410.  
  411. public void deleteUUID(UUID uuid) {
  412. deleteUUID("uuid", uuid);
  413. }
  414.  
  415. public void deleteUUID(String key, UUID uuid) {
  416. delete(key, uuid.toString());
  417. }
  418.  
  419. public void insert(String values) {
  420. PreparedStatement statement = null;
  421. try {
  422. statement = getConnection().prepareStatement("INSERT INTO " + getName() + " (" + keys + ") VALUES (" + values + ")");
  423. statement.execute();
  424. } catch(SQLException e) {
  425. BungeeCord.getInstance().getLogger().info(e.getMessage());
  426. } finally {
  427. close(statement);
  428. }
  429. }
  430.  
  431. public static void close(PreparedStatement statement, ResultSet resultSet) {
  432. close(statement);
  433. close(resultSet);
  434. }
  435.  
  436. public static void close(PreparedStatement statement) {
  437. try {
  438. if(statement != null) {
  439. statement.close();
  440. }
  441. } catch(SQLException e) {
  442. BungeeCord.getInstance().getLogger().info(e.getMessage());
  443. }
  444. }
  445.  
  446. public static void close(ResultSet resultSet) {
  447. try {
  448. if(resultSet != null) {
  449. resultSet.close();
  450. }
  451. } catch(SQLException e) {
  452. BungeeCord.getInstance().getLogger().info(e.getMessage());
  453. }
  454. }
  455.  
  456. public enum Databases {
  457. PLAYERS, NETWORK, HUB, STAFF;
  458.  
  459. private Connection connection = null;
  460.  
  461. public void connect() {
  462. if(connection == null) {
  463. try {
  464. Configuration config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File("/Users/TONYD31/Desktop/Private Server 05/root/db.yml"));
  465. String address = config.getString("address");
  466. int port = config.getInt("port");
  467. String user = config.getString("user");
  468. String password = config.getString("password");
  469. connection = DriverManager.getConnection("jdbc:mysql://" + address + ":" + port + "/" + toString().toLowerCase(), user, password);
  470. } catch(Exception e) {
  471. BungeeCord.getInstance().getLogger().info(e.getMessage());
  472. }
  473. }
  474. }
  475.  
  476. public Connection getConnection() {
  477. return this.connection;
  478. }
  479.  
  480. public void disconnect() {
  481. if(connection != null) {
  482. try {
  483. connection.close();
  484. } catch(SQLException e) {
  485. BungeeCord.getInstance().getLogger().info(e.getMessage());
  486. }
  487. }
  488. }
  489. }
  490. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement