· 9 years ago · Feb 28, 2017, 06:54 PM
1 static int host = Main.getPlugin().getConfig().getInt("mysql.host");
2 static String database = Main.getPlugin().getConfig().getString("mysql.database");
3 static String username = Main.getPlugin().getConfig().getString("mysql.user");
4 static String password = Main.getPlugin().getConfig().getString("mysql.pass");
5 static int port = Main.getPlugin().getConfig().getInt("mysql.port");
6
7 public static Connection SQLconnect() {
8 try {
9 Class.forName("com.mysql.jdbc.Driver").newInstance();
10 final Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database,
11 username, password);
12 return con;
13 } catch (final ClassNotFoundException ex) {
14 System.err.println("[Training] No MySQL driver found!");
15 } catch (final SQLException ex) {
16 System.err.println("[Training] Error while fetching MySQL connection!");
17 } catch (final Exception ex) {
18 System.err.println("[Training] Unknown error while fetchting MySQL connection.");
19 }
20 return null;
21 }
22
23 public static void SQLdisconnect(Connection con) {
24 try {
25 con.close();
26 } catch (SQLException | NullPointerException ex) {
27 System.err.println("[Training] Error while closing the connection...");
28 }
29 }
30
31 public static void createTable() {
32 if (!Training.getSql())
33 return;
34 final Connection con = SQLconnect();
35 try {
36 final Statement state = con.createStatement();
37 state.executeUpdate(
38 "CREATE TABLE IF NOT EXISTS accounts(uuid VARCHAR(255),name VARCHAR(16),rank VARCHAR(32),coins INT,level INT,exp INT,played INT,kills INT,deaths INT,kit VARCHAR(255),ks INT)");
39 state.executeUpdate(
40 "CREATE TABLE IF NOT EXISTS achievements(uuid VARCHAR(255),name VARCHAR(16),kills VARCHAR(5),blocks VARCHAR(5),pickaxe VARCHAR(5))");
41 state.executeUpdate(
42 "CREATE TABLE IF NOT EXISTS configurations(uuid VARCHAR(255),name VARCHAR(16),anim VARCHAR(5),msg VARCHAR(5),compass VARCHAR(5),title VARCHAR(5))");
43 } catch (final SQLException e) {
44 System.err.println(e);
45 e.printStackTrace();
46 }
47 SQLdisconnect(con);
48 }