· 8 years ago · May 21, 2018, 10:30 PM
1package main;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.SQLException;
6
7public class MySQL {
8
9 public String table;
10 private String url;
11 private String username;
12 private String password;
13 public Connection connection;
14 private String TABLE;
15 public String INSERT;
16 public String UPDATE;
17 public String UPDATE_COINS;
18 public String UPDATE_NAME;
19 public String UPDATE_STATS;
20 public String SELECT;
21 public String SELECT_COINS;
22 public String SELECT_ORDER;
23 public String SELECTALL;
24 public String DELETE_PLAYER;
25
26 public MySQL(String host, int port, String database, String username, String password, String table){
27 this.url = ("jdbc:mysql://" + host + ":" + port + "/" + database);
28 this.username = username;
29 this.password = password;
30 this.table = table;
31 this.TABLE = ("CREATE TABLE IF NOT EXISTS " + table + "(uuid char(32), name char(16), unranked_wins int DEFAULT 0, ranked_wins int DEFAULT 0, premium_wins int DEFAULT 0, games_played int DEFAULT 0, play_time int DEFAULT 0, PRIMARY KEY(uuid))");
32 this.INSERT = ("INSERT IGNORE INTO " + table + " values(?, ?, ?, ?, ?, ?, ?)");
33 this.UPDATE = ("UPDATE " + table + " SET name=?, unranked_wins=?, ranked_wins=?, premium_wins=?, games_played=?, play_time=? where uuid=?");
34 this.UPDATE_COINS = ("UPDATE coins SET coins=? where uuid=?");
35 this.UPDATE_NAME = ("UPDATE " + table + " SET name=? where uuid=?");
36 this.SELECT = ("SELECT * FROM " + table + " WHERE uuid=?");
37 this.SELECT_COINS = ("SELECT coins FROM coins WHERE uuid=?");
38 this.SELECT_ORDER = ("SELECT * FROM " + table + " ORDER BY %s DESC LIMIT 0,?");
39 this.SELECTALL = ("SELECT * FROM " + table);
40 this.DELETE_PLAYER = ("SELECT FROM " + table + " WHERE uuid=?");
41 }
42
43 public void close(){
44 try {
45 if (this.connection != null)
46 this.connection.close();
47 } catch (SQLException e) {
48 e.printStackTrace();
49 }
50 }
51
52 public void connect(){
53 try {
54 this.connection = DriverManager.getConnection(this.url, this.username, this.password);
55 } catch (Exception e) {
56 e.printStackTrace();
57 }
58 }
59
60 public Connection getConnection(){
61 try {
62 if (this.connection == null || (!this.connection.isValid(5))) connect();
63 } catch (SQLException e) {
64 e.printStackTrace();
65 }
66 return this.connection;
67 }
68
69 public void setupTable(){
70 Connection localConnection = getConnection();
71 try {
72 localConnection.createStatement().executeUpdate(this.TABLE);
73 } catch (SQLException e) {
74 e.printStackTrace();
75 }
76 }
77
78}