· 8 years ago · May 10, 2018, 01:40 PM
1public class MySqlConnectionManager extends BukkitRunnable {
2 public static final Map<UUID, Integer> INDEXS = new HashMap<>();
3
4 private BlazeGadget gadget;
5 private Table table = null;
6 private HikariDataSource ds;
7 Connection co;
8 private SqlUtils sqlUtils;
9
10 public MySqlConnectionManager(BlazeGadget gadget) {
11 this.gadget = gadget;
12 this.sqlUtils = new SqlUtils(this);
13 }
14
15 public void create() {
16 ds = new HikariDataSource();
17 ds.setMaximumPoolSize(10);
18 ds.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
19 ds.addDataSourceProperty("serverName", "localhost");
20 ds.addDataSourceProperty("port", "3306");
21 ds.addDataSourceProperty("databaseName", "BlazeGadgets");
22 ds.addDataSourceProperty("user", "root");
23 ds.addDataSourceProperty("password", "password");
24 }
25
26 public void start() {
27 runTaskTimerAsynchronously(ultraCosmetics, 0, 2400);
28 }
29 @Override
30 public void run() {
31 try {
32 Bukkit.getServer().getConsoleSender().sendMessage("§cМЫ ПОДКЛЮЧИЛИСЬ К MYSQL by HikariCp");
33 if (co != null)
34 co.close();
35 //MySqlConnection sql1 = new MySqlConnection(hostname, database, username, password, dataSource);
36 co = ds.getConnection();
37 Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD
38 + "BlazeGadget-> Successfully connected to MySQL server! :)");
39 PreparedStatement sql = co.prepareStatement(
40 "CREATE TABLE IF NOT EXISTS BlazeData (" + "id INTEGER not NULL AUTO_INCREMENT,"
41 + " IIIDIuuid VARCHAR(255)," + " username VARCHAR(255)," + " PRIMARY KEY ( id ))");
42 sql.executeUpdate();
43 for (GadgetType gadgetType : GadgetType.values()) {
44 DatabaseMetaData md = co.getMetaData();
45 ResultSet rs = md.getColumns(null, null, "BlazeData ",
46 gadgetType.toString().replace("_", "").toLowerCase());
47 if (!rs.next()) {
48 PreparedStatement statement = co.prepareStatement(
49 "ALTER TABLE BlazeData ADD " + gadgetType.toString().replace("_", "").toLowerCase()
50 + " INTEGER DEFAULT 0 not NULL");
51 statement.executeUpdate();
52 statement.close();
53 }
54 rs.close();
55 }
56 INDEXS.putAll(sqlUtils.getIds());
57 } catch (Exception e) {
58 e.printStackTrace();
59 }
60 }
61
62 Table getTable() {
63 return table;
64 }
65
66 public SqlUtils getSqlUtils() {
67 return sqlUtils;
68 }
69}