· 7 years ago · Dec 04, 2018, 03:58 PM
1public class MySQL {
2 private Connection connection;
3 private String url;
4 private String username;
5 private String password;
6 private static final String TABLE = "CREATE TABLE IF NOT EXISTS `bedwars` (`id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, `mode` ENUM('solo', 'double', 'team', 'rush'),`kills` INT(255), `deaths` INT(255),`wins` INT(255), `bedbreak` INT(255),`finalkills` INT(255), PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8;" ;
7 private static final String INSERT = "INSERT INTO `bedwars` (`name`, `mode`, `kills`, `deaths`, `wins`, `bedbreak`, `finalkills`) VALUES(?, ?, ?, ?, ?, ?);";
8 public static final String UPDATE = "UPDATE `bedwars` SET `kills`=`kills`+?,`deaths`=`deaths`+?,`wins`=`winds`+?,`bedbreak`=`bedbreak`+?,`finalkills`=`finalkills`+? WHERE name=?";;
9 public static final String SELECTALL = "SELECT * FROM `bedwars`";//*
10 public String SELECT;
11 public static final String DELETE_PLAYER = "SELECT * FROM `bedwars` WHERE name=?";//*
12
13 public MySQL(String url, String username, String password) {
14 this.url = url;
15 this.username = username;
16 this.password = password;
17 SELECT = "SELECT `name` FROM `bedwars` WHERE name=? and mode="+Core.mode;//*
18 }
19 public final void openConnection() {
20 Bukkit.getScheduler().runTaskAsynchronously(Core.getInstance(), () -> {
21 try {
22 if (connection != null && !connection.isClosed()) {
23 return;
24 }
25 MysqlDataSource dataSource = new MysqlDataSource();
26 dataSource.setUrl(url + "?useUnicode=true&characterEncoding=UTF-8");
27 dataSource.setUser(username);
28 dataSource.setPassword(password);
29 dataSource.setAutoReconnect(true);
30 connection = dataSource.getConnection();
31 try (Statement st = connection.createStatement()) {
32 st.executeUpdate(TABLE);
33 }
34 } catch (SQLException e) {
35 e.printStackTrace();
36 }
37 });
38 }
39
40 public void savePlayer(String name, BedPlayer data) {
41 Bukkit.getScheduler().runTaskAsynchronously(Core.getInstance(), () -> {
42 try(PreparedStatement ps = connection.prepareStatement(SELECT)) {
43 ps.setString(1, name);
44 try(ResultSet rs = ps.executeQuery()) {
45 if (rs.next()) {
46 try(PreparedStatement ps1 = connection.prepareStatement(UPDATE)){
47 //Я еще н е заполнил
48 ps1.setInt(1, data.getKill());
49 ps1.setInt(2, data.getDeath());
50 ps1.setInt(3, this.deaths);
51 ps1.setInt(4, this.wins);
52 ps1.setInt(5, this.coins);
53 ps1.setInt(6, this.beddestroyed);
54 ps1.setInt(7, this.finalkills);
55 ps1.setInt(8, this.exp);
56 ps1.setInt(9, this.level);
57 ps1.setString(10, this.favorite);
58 ps1.setString(11, this.p.toString());
59 }
60 } else {
61 try(PreparedStatement ps2 = connection.prepareStatement(INSERT)) {
62 //Я еще н е заполнил
63 ps2.setString(1, this.p.toString());
64 ps2.setString(2, this.kits);
65 ps2.setInt(3, this.kills);
66 ps2.setInt(4, this.deaths);
67 ps2.setInt(5, this.wins);
68 ps2.setInt(6, this.coins);
69 ps2.setInt(7, this.beddestroyed);
70 ps2.setInt(8, this.finalkills);
71 ps2.setInt(9, this.exp);
72 ps2.setInt(10, this.level);
73 ps2.setString(11, this.favorite);
74 }
75 }
76 }
77 } catch (SQLException e) {
78 e.printStackTrace();
79 }
80 });
81 }
82}