· 9 years ago · Feb 28, 2017, 06:54 PM
1package fr.ComputerDev.training.utils;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import java.sql.Statement;
9
10import org.bukkit.entity.Player;
11
12import fr.ComputerDev.Main;
13import fr.ComputerDev.training.Training;
14
15public class DatabaseUtils {
16
17 static int host = Main.getPlugin().getConfig().getInt("mysql.host");
18 static String database = Main.getPlugin().getConfig().getString("mysql.database");
19 static String username = Main.getPlugin().getConfig().getString("mysql.user");
20 static String password = Main.getPlugin().getConfig().getString("mysql.pass");
21 static int port = Main.getPlugin().getConfig().getInt("mysql.port");
22
23 public static Connection SQLconnect() {
24 try {
25 Class.forName("com.mysql.jdbc.Driver").newInstance();
26 final Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database,
27 username, password);
28 return con;
29 } catch (final ClassNotFoundException ex) {
30 System.err.println("[Training] No MySQL driver found!");
31 } catch (final SQLException ex) {
32 System.err.println("[Training] Error while fetching MySQL connection!");
33 } catch (final Exception ex) {
34 System.err.println("[Training] Unknown error while fetchting MySQL connection.");
35 }
36 return null;
37 }
38
39 public static void SQLdisconnect(Connection con) {
40 try {
41 con.close();
42 } catch (SQLException | NullPointerException ex) {
43 System.err.println("[Training] Error while closing the connection...");
44 }
45 }
46
47 public static void createTable() {
48 if (!Training.getSql())
49 return;
50 final Connection con = SQLconnect();
51 try {
52 final Statement state = con.createStatement();
53 state.executeUpdate(
54 "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)");
55 state.executeUpdate(
56 "CREATE TABLE IF NOT EXISTS achievements(uuid VARCHAR(255),name VARCHAR(16),kills VARCHAR(5),blocks VARCHAR(5),pickaxe VARCHAR(5))");
57 state.executeUpdate(
58 "CREATE TABLE IF NOT EXISTS configurations(uuid VARCHAR(255),name VARCHAR(16),anim VARCHAR(5),msg VARCHAR(5),compass VARCHAR(5),title VARCHAR(5))");
59 } catch (final SQLException e) {
60 System.err.println(e);
61 e.printStackTrace();
62 }
63 SQLdisconnect(con);
64 }