· 9 years ago · Dec 04, 2016, 08:11 PM
1package me.MySQL;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import org.bukkit.entity.Player;
9
10public class MySQL {
11
12 String host = "localhost";
13 String database = "minecraft";
14 String user = "root";
15 String pass = "";
16 String port = "3306";
17
18 private static Connection connection;
19
20
21
22 public synchronized void openConnection() {
23 try {
24 connection = DriverManager.getConnection("jdbc:mysql://" +host + ":3306/" + database, user, pass);
25 System.out.println("§2Successfully connected to MySQL Database!");
26 } catch (SQLException e) {
27 e.printStackTrace();
28 }
29 }
30
31
32
33 public static void closeConnection(){
34 try{
35 connection.close();
36 }catch (SQLException e){
37 e.printStackTrace();
38 }
39 }
40
41
42
43 public static void createTables(){
44 try {
45 String query = "CREATE TABLE IF NOT EXISTS `nombre` (`uuid` varchar(36) UNIQUE, `coins` int(999999));";
46 PreparedStatement p = connection.prepareStatement(query);
47 p.execute();
48 p.close();
49 } catch (SQLException e) {
50 e.printStackTrace();
51 }
52 }
53
54
55
56 public static void createPlayer(Player player){
57 try {
58 String query = "INSERT IGNORE INTO pets (uuid,coins) VALUES(?, ?);";
59 PreparedStatement p = connection.prepareStatement(query);
60 p.setString(1, player.getUniqueId().toString());
61 p.setInt(2, 0);
62 p.execute();
63 p.close();
64 } catch (SQLException e) {
65 e.printStackTrace();
66 }
67 }
68
69
70
71 @SuppressWarnings("finally")
72 public static Integer getCoins(Player p){
73 Integer temp = 0;
74 String query = "SELECT * FROM nombre WHERE `uuid` = '" + p.getUniqueId() + "'";
75 try {
76 ResultSet res = connection.prepareStatement(query).executeQuery();
77 while (res.next()) {
78 temp = res.getInt("coins");
79 }
80 res.close();
81 } catch (SQLException e) {
82 e.printStackTrace();
83 } finally {
84 return temp;
85 }
86 }
87
88
89
90 public static void setCoins(Player player, int coins){
91 try {
92 String query = "UPDATE nombre SET coins=? WHERE uuid=?;";
93 PreparedStatement p = connection.prepareStatement(query);
94 p.setInt(1, coins);
95 p.setString(2, player.getUniqueId().toString());
96 p.execute();
97 p.close();
98 } catch (SQLException e) {
99 e.printStackTrace();
100 }
101 }
102
103
104
105 public static void removeCoins(Player player, int removedcoins) {
106 setCoins(getCoins(player) - removedcoins); // ESTO DA ERROR, ESTA LINEA
107 }