· 8 years ago · Feb 04, 2018, 05:06 PM
1package io.frenchdev.MySQL;
2
3import java.sql.*;
4
5public class MySQL{
6 public String table;
7 private Connection connection;
8 String url;
9 String username;
10 String password;
11 private String TABLE;
12 public String INSERT;
13 public String UPDATE;
14 public String SELECT;
15 public String SELECTALL;
16 public String DELETE_PLAYER;
17
18 public MySQL(final String table, final String s, final int i, final String s3, final String username, final String password) {
19 this.url = "jdbc:mysql://" + s + ":" + i + "/" + s3;
20 this.username = username;
21 this.password = password;
22 this.table = table;
23 this.TABLE = "CREATE TABLE IF NOT EXISTS " + table + "(id INT AUTO_INCREMENT PRIMARY KEY, Player VARCHAR(255), Kills INT(255), Deaths INT(255), Wins INT(255), Coins INT(255), WoolDestroyed INT(255), Stars INT(255), Level INT(255))";
24 this.INSERT = "insert into " + table + " (Player, Kills, Deaths, Wins, Coins, WoolDestroyed, Stars, Level) values(?, ?, ?, ?, ?, ?,?, ?, ?, ?)";
25 this.UPDATE = "update " + table + " set Kills=?,Deaths=?,Wins=?,Coins=?,WoolDestroyed=?,Stars=?,Level=? where Player=?";
26 this.SELECT = "select * from " + table + " where Player=?";
27 this.SELECTALL = "select * from " + table;
28 this.DELETE_PLAYER = "delete from " + table + " where Player=?";
29 }
30
31 public void close() throws SQLException {
32 this.connection.close();
33 }
34
35 public void connect() throws SQLException {
36 this.connection = DriverManager.getConnection(this.url, this.username, this.password);
37 }
38
39 public Connection getConnection() throws SQLException {
40 if (this.connection == null || !this.connection.isValid(5)) {
41 this.connect();
42 }
43 return this.connection;
44 }
45
46 public void setupTable() throws SQLException {
47 this.getConnection().createStatement().executeUpdate(this.TABLE);
48 }
49}