· 5 years ago · Mar 29, 2020, 01:56 PM
1package me.joseph.levels.sql;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.SQLException;
6import java.sql.Statement;
7
8public class MySQL extends Database {
9 private final String user;
10
11 private final String database;
12
13 private final String password;
14
15 private final String port;
16
17 private final String hostname;
18
19 public MySQL(String hostname, String port, String username, String password) {
20 this(hostname, port, (String)null, username, password);
21 }
22
23 public MySQL(String hostname, String port, String database, String username, String password) {
24 this.hostname = hostname;
25 this.port = port;
26 this.database = database;
27 this.user = username;
28 this.password = password;
29 try {
30 openConnection();
31 } catch (ClassNotFoundException e) {
32 e.printStackTrace();
33 } catch (SQLException e) {
34 e.printStackTrace();
35 }
36 }
37
38 public Connection openConnection() throws SQLException, ClassNotFoundException {
39 if (checkConnection())
40 return this.connection;
41 String connectionURL = "jdbc:mysql://" +
42 this.hostname + ":" + this.port;
43 if (this.database != null)
44 connectionURL = String.valueOf(connectionURL) + "/" + this.database;
45 Class.forName("com.mysql.jdbc.Driver");
46 this.connection = DriverManager.getConnection(connectionURL,
47 this.user, this.password);
48 createTable();
49 return this.connection;
50 }
51
52 public void createTable() {
53 try {
54 Statement statement = getConnection().createStatement();
55 statement.executeUpdate(
56 "CREATE TABLE IF NOT EXISTS NetworkLevelsData (playeruuid char(36), level INT(10), xp INT(10), rewards VARCHAR(5000));");
57 } catch (SQLException e) {
58 e.printStackTrace();
59 }
60 }
61}