/
SendDatabase.java
230 lines (214 loc) · 6.69 KB
/
SendDatabase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package me.ibhh.xpShop.send.sql;
/**
*
*/
import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import me.ibhh.xpShop.xpShop;
/**
* @author ibhh
*
*/
public class SendDatabase {
private Connection cn;
private xpShop plugin;
public SendDatabase(xpShop plugin) {
this.plugin = plugin;
cn = null;
}
public boolean deleteDB() {
Statement st = null;
boolean temp = false;
long time = 0;
plugin.Logger("deleting table!", "Debug");
time = System.nanoTime();
try {
String sql = "drop table if exists xpShop;";
st = cn.createStatement();
if (plugin.getConfig().getBoolean("SQL")) {
st.executeUpdate(sql);
plugin.Logger("Table deleted!", "Debug");
temp = true;
} else {
st.executeUpdate("drop table if exists xpShop;");
plugin.Logger("Table deleted!", "Debug");
temp = true;
}
cn.commit();
st.close();
} catch (SQLException e) {
plugin.Logger("Error while creating tables! - " + e.getMessage(), "Error");
SQLErrorHandler(e);
}
time = (System.nanoTime() - time) / 1000000;
plugin.Logger("DELETED in " + time + " ms!", "Debug");
// UpdateDB();
return temp;
}
public void PrepareDB() {
Statement st = null;
long time = 0;
plugin.Logger("Creaeting table!", "Debug");
time = System.nanoTime();
try {
String sql = "CREATE TABLE IF NOT EXISTS xpShopSend (ID INT PRIMARY KEY AUTOINCREMENT, Name VARCHAR(30), Sender VARCHAR(30), Message VARCHAR(100), XP INT, Status INT);";
st = cn.createStatement();
if (plugin.getConfig().getBoolean("SQL")) {
st.executeUpdate(sql);
plugin.Logger("Table created!", "Debug");
} else {
st.executeUpdate("CREATE TABLE IF NOT EXISTS xpShopSend (ID INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR, Sender VARCHAR, Message VARCHAR, XP INT, Status INT);");
plugin.Logger("Table created!", "Debug");
}
cn.commit();
st.close();
} catch (SQLException e) {
plugin.Logger("Error while creating tables! - " + e.getMessage(), "Error");
SQLErrorHandler(e);
}
time = (System.nanoTime() - time) / 1000000;
plugin.Logger("Created in " + time + " ms!", "Debug");
// UpdateDB();
}
public boolean InsertSend(XPSend send) {
return InsertSend(send.getPlayer(), send.getSender(), send.getSendedXP(), send.getMessage(), send.getStatus());
}
public boolean InsertSend(String player, String sender, int sendedXP, String message, int status) {
long time = 0;
plugin.Logger("Insert new XPSend into table!", "Debug");
time = System.nanoTime();
try {
PreparedStatement ps = cn.prepareStatement("INSERT INTO xpShopSend (Name, Sender, Message, XP, Status) VALUES (?,?,?,?,?)");
ps.setString(1, player.toLowerCase());
ps.setString(2, sender.toLowerCase());
ps.setString(3, message);
ps.setInt(4, sendedXP);
ps.setInt(5, status);
ps.execute();
cn.commit();
ps.close();
} catch (SQLException e) {
plugin.Logger("Error while inserting XP into DB! - " + e.getMessage(), "Error");
SQLErrorHandler(e);
return false;
}
time = (System.nanoTime() - time) / 1000000;
plugin.Logger("Finished in " + time + " ms!", "Debug");
return true;
}
public boolean setStatus(final String player,final int id, final int status) {
long time = 0;
plugin.Logger("Updating XP in DB!", "Debug");
time = System.nanoTime();
try {
PreparedStatement ps = cn.prepareStatement("UPDATE xpShopSend SET Status='" + status + "' WHERE Name='" + player.toLowerCase() + "' AND ID=" + id + ";");
ps.executeUpdate();
cn.commit();
ps.close();
} catch (SQLException e) {
plugin.Logger("Error while inserting XP into DB! - " + e.getMessage(), "Error");
SQLErrorHandler(e);
return false;
}
time = (System.nanoTime() - time) / 1000000;
plugin.Logger("Finished in " + time + " ms!", "Debug");
return true;
}
public Connection createConnection() {
if (plugin.config.useMySQL) {
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://" + plugin.config.dbPath, plugin.config.dbUser, plugin.config.dbPassword);
cn.setAutoCommit(false);
return cn;
} catch (SQLException e) {
plugin.Logger("could not be enabled: Exception occured while trying to connect to DB", "Error");
SQLErrorHandler(e);
if (cn != null) {
plugin.Logger("Old Connection still activated", "Error");
try {
cn.close();
plugin.Logger("Old connection that was still activated has been successfully closed", "Error");
} catch (SQLException e2) {
plugin.Logger("Failed to close old connection that was still activated", "Error");
SQLErrorHandler(e2);
}
}
return null;
} catch (ClassNotFoundException e) {
ErrorLogger(e.getMessage());
return null;
}
} else if (!plugin.config.useMySQL) {
try {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException cs) {
ErrorLogger(cs.getMessage());
}
cn = DriverManager.getConnection("jdbc:sqlite:plugins" + File.separator + "xpShop" + File.separator + "xpShop.sqlite");
cn.setAutoCommit(false);
return cn;
} catch (SQLException e) {
SQLErrorHandler(e);
}
}
return null;
}
private void ErrorLogger(String Error) {
plugin.Logger(Error, "Error");
}
private void SQLErrorHandler(SQLException ex) {
do {
try {
ErrorLogger("Exception Message: " + ex.getMessage());
ErrorLogger("DBMS Code: " + ex.getErrorCode());
ex.printStackTrace();
} catch (Exception ne) {
ErrorLogger(ne.getMessage());
}
} while ((ex = ex.getNextException()) != null);
}
public boolean CloseCon() {
try {
cn.close();
return true;
} catch (SQLException e) {
plugin.Logger("Failed to close connection to DB!", "Error");
SQLErrorHandler(e);
return false;
}
}
public ArrayList<XPSend> getOpenTransactions(final String name) throws SQLException {
long time = 0;
plugin.Logger("getting Transactions!", "Debug");
time = System.nanoTime();
Statement st = null;
String sql;
ResultSet result;
try {
st = cn.createStatement();
} catch (SQLException e) {
SQLErrorHandler(e);
}
sql = "SELECT * from xpShopSend WHERE Name='" + name.toLowerCase() + "' AND Status=0;";
result = st.executeQuery(sql);
ArrayList<XPSend> xpSends = new ArrayList<XPSend>();
try {
while (result.next() == true) {
XPSend temp = new XPSend(result.getString("Name").toLowerCase(), result.getString("Sender").toLowerCase(), result.getInt("XP"), result.getString("Message"), result.getInt("Status"));
temp.setId(result.getInt("ID"));
xpSends.add(temp);
}
cn.commit();
st.close();
result.close();
} catch (SQLException e2) {
SQLErrorHandler(e2);
}
time = (System.nanoTime() - time) / 1000000;
plugin.Logger("Finished in " + time + " ms!", "Debug");
return xpSends;
}
}