· 8 years ago · Jul 10, 2018, 06:40 PM
1package il.co.play.pvp.data;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.util.UUID;
8
9import il.co.play.pvp.utills.Tags;
10
11public class DonateSQL {
12
13 final String username = "minecraft_db1";
14 final String password = "a4yq+Ea";
15 final String url = "jdbc:mysql:/37.187.121.199:3306/minecraft_db1";
16 private Connection c = null;
17
18 public DonateSQL() {
19 try {
20 Class.forName("com.mysql.jdbc.Driver");
21 } catch (Exception e) {
22 e.printStackTrace();
23 }
24
25 try {
26 c = DriverManager.getConnection(url, username, password);
27 } catch (Exception e) {
28 e.printStackTrace();
29 }
30 String q = "CREATE TABLE IF NOT EXISTS `s3123_play-il_mc`.`players` ( `uuid` TEXT NOT NULL , `pixels` BIGINT NOT NULL , `rankId` TEXT NOT NULL ) ENGINE = InnoDB;";
31 String q1 = "CREATE TABLE IF NOT EXISTS `s3123_play-il_mc`.`boosters` ( `uuid` TEXT NOT NULL , `type` TEXT NOT NULL ) ENGINE = InnoDB;";
32 try {
33 PreparedStatement stmt = c.prepareStatement(q);
34 stmt.executeUpdate();
35 stmt = c.prepareStatement(q1);
36 stmt.executeUpdate();
37 } catch (Exception e) {
38 e.printStackTrace();
39 }
40
41 }
42
43 public void addBooster(UUID u, String type) {
44 String q = "INSERT INTO boosters (uuid, type) VALUES (?, ?);";
45 try {
46 PreparedStatement stmt = c.prepareStatement(q);
47 stmt.setString(1, u.toString());
48 stmt.setString(2, type);
49 stmt.executeUpdate();
50 } catch (Exception e) {
51 e.printStackTrace();
52 }
53 }
54
55 public int getCountOfType(UUID uuid, String type) {
56 String q = "SELECT count(*) FROM boosters WHERE uuid = ? AND type = ?;";
57 try {
58 PreparedStatement stmt = c.prepareStatement(q);
59 stmt.setString(1, uuid.toString());
60 stmt.setString(2, type);
61 ResultSet results = stmt.executeQuery();
62 results.next();
63 return results.getInt(1);
64 } catch (Exception e) {
65 e.printStackTrace();
66 return 0;
67 }
68 }
69
70 public int getCountOfPlayer(UUID uuid) {
71 String q = "SELECT count(*) FROM boosters WHERE uuid = ?;";
72 try {
73 PreparedStatement stmt = c.prepareStatement(q);
74 stmt.setString(1, uuid.toString());
75 ResultSet results = stmt.executeQuery();
76 results.next();
77 return results.getInt(1);
78 } catch (Exception e) {
79 e.printStackTrace();
80 return 0;
81 }
82 }
83
84 public boolean isInDBBooster(UUID uuid, String type) {
85 String q = "SELECT * FROM boosters WHERE uuid = ? AND type = ?;";
86 try {
87 PreparedStatement stmt = c.prepareStatement(q);
88 stmt.setString(1, uuid.toString());
89 stmt.setString(2, type);
90 ResultSet results = stmt.executeQuery();
91 while (results.next()) {
92
93 return true;
94 }
95
96 return false;
97 } catch (Exception e) {
98 e.printStackTrace();
99 return false;
100 }
101 }
102
103 public void removeBooster(UUID u, String type) {
104 String q = "DELETE FROM boosters WHERE uuid = ? AND type = ? LIMIT 1;";
105 try {
106 PreparedStatement stmt = c.prepareStatement(q);
107 stmt.setString(1, u.toString());
108 stmt.setString(2, type);
109 stmt.executeUpdate();
110 } catch (Exception e) {
111 e.printStackTrace();
112 }
113 }
114
115 public void newLine(UUID uuid) {
116 String q = "INSERT INTO players (uuid, pixels, rankId) VALUES ( ? , 0, ?);";
117 try {
118 PreparedStatement stmt = c.prepareStatement(q);
119 stmt.setString(1, uuid.toString());
120 stmt.setString(2, Tags.humanTag);
121 stmt.executeUpdate();
122 } catch (Exception e) {
123 e.printStackTrace();
124 }
125 }
126
127 public long getPixels(UUID uuid) {
128 String q = "SELECT pixels FROM players WHERE uuid = ?;";
129 try {
130 PreparedStatement stmt = c.prepareStatement(q);
131 stmt.setString(1, uuid.toString());
132 ResultSet results = stmt.executeQuery();
133 while (results.next()) {
134 return results.getLong("pixels");
135 }
136 return 0;
137 } catch (Exception e) {
138 e.printStackTrace();
139 return 0;
140 }
141 }
142
143 public boolean isInDB(UUID uuid) {
144 String q = "SELECT * FROM players WHERE uuid = ?;";
145 try {
146 PreparedStatement stmt = c.prepareStatement(q);
147 stmt.setString(1, uuid.toString());
148 ResultSet results = stmt.executeQuery();
149 while (results.next()) {
150
151 return true;
152 }
153
154 return false;
155 } catch (Exception e) {
156 e.printStackTrace();
157 return false;
158 }
159 }
160
161 public void setPixels(UUID uuid, long pixels) {
162 String q = "UPDATE players SET pixels = ? WHERE uuid = ?;";
163 try {
164 PreparedStatement stmt = c.prepareStatement(q);
165 stmt.setLong(1, pixels);
166 stmt.setString(2, uuid.toString());
167 stmt.executeUpdate();
168 } catch (Exception e) {
169 e.printStackTrace();
170 }
171 }
172
173 public String getRank(UUID uuid) {
174 String q = "SELECT rankId FROM players WHERE uuid = ?;";
175 try {
176 PreparedStatement stmt = c.prepareStatement(q);
177 stmt.setString(1, uuid.toString());
178 ResultSet results = stmt.executeQuery();
179 while (results.next()) {
180 return results.getString("rankId");
181 }
182 return Tags.humanTag;
183 } catch (Exception e) {
184 e.printStackTrace();
185 return Tags.humanTag;
186 }
187 }
188
189 public void setRank(UUID uuid, String text) {
190 String q = "UPDATE players SET rankId = ? WHERE uuid = ?;";
191 try {
192 PreparedStatement stmt = c.prepareStatement(q);
193 stmt.setString(1, text);
194 stmt.setString(2, uuid.toString());
195 stmt.executeUpdate();
196 } catch (Exception e) {
197 e.printStackTrace();
198 }
199 }
200
201 public void close() {
202 try {
203 if (c != null && !c.isClosed()) {
204 c.close();
205 }
206 } catch (Exception e) {
207 e.printStackTrace();
208 }
209 }
210
211}