1
1
package de .tallerik ;
2
2
3
3
import com .mysql .cj .jdbc .MysqlDataSource ;
4
- import com . sun . rowset . CachedRowSetImpl ;
4
+ import de . tallerik . utils .* ;
5
5
6
6
import javax .sql .DataSource ;
7
- import java .sql .Connection ;
8
- import java .sql .ResultSet ;
9
- import java .sql .SQLException ;
10
- import java .sql .Statement ;
7
+ import java .sql .*;
11
8
12
- @ SuppressWarnings ("Duplicates" )
9
+ @ SuppressWarnings ("Duplicates unused " )
13
10
public class MySQL {
14
11
15
12
// Vars
@@ -66,14 +63,14 @@ public boolean connect() {
66
63
dataSource .setDatabaseName (db );
67
64
dataSource .setUser (user );
68
65
dataSource .setPassword (password );
69
-
66
+ dataSource . setAllowMultiQueries ( true );
70
67
con = dataSource .getConnection ();
71
68
System .out .println ("Connection established" );
72
69
return true ;
73
70
}
74
71
catch (SQLException ex )
75
72
{
76
- System . out . println ( ex );
73
+ ex . printStackTrace ( );
77
74
return false ;
78
75
}
79
76
}
@@ -133,6 +130,43 @@ public boolean tableInsert(String table, String columns, String... data) {
133
130
}
134
131
return false ;
135
132
}
133
+ public boolean tableInsert (Insert ... builders ) {
134
+ String sql = "" ;
135
+ for (Insert b : builders ) {
136
+ String sqldata = "" ;
137
+ int i = 0 ;
138
+ for (String d : b .getData ()) {
139
+ sqldata = sqldata + "'" + d + "'" ;
140
+ i ++;
141
+ if (i != b .getData ().length ) {
142
+ sqldata = sqldata + ", " ;
143
+ }
144
+ }
145
+
146
+
147
+ sql = sql + "INSERT INTO " + b .getTable () + " (" + b .getColumns () + ") VALUES (" + sqldata + "); " ;
148
+
149
+ }
150
+ Statement stmt = null ;
151
+ try {
152
+ stmt = con .createStatement ();
153
+ stmt .execute (sql );
154
+
155
+ } catch (SQLException e ) {
156
+ e .printStackTrace ();
157
+ } finally {
158
+ if (stmt != null ) {
159
+ try {
160
+ stmt .close ();
161
+ return true ;
162
+ } catch (SQLException e ) {
163
+ e .printStackTrace ();
164
+ }
165
+ }
166
+ }
167
+ return false ;
168
+ }
169
+
136
170
public boolean rowUpdate (String table , UpdateValue value , String filter ) {
137
171
String change = "" ;
138
172
int i = 0 ;
@@ -162,7 +196,40 @@ public boolean rowUpdate(String table, UpdateValue value, String filter) {
162
196
}
163
197
return false ;
164
198
}
165
- public CachedRowSetImpl rowSelect (String table , String columns , String filter ) {
199
+ public boolean rowUpdate (Update ... builders ) {
200
+ String sql = "" ;
201
+ for (Update u : builders ) {
202
+ String change = "" ;
203
+ int i = 0 ;
204
+ for (String key : u .getValue ().getKeys ()) {
205
+ change = change + key + " = '" + u .getValue ().get (key ) + "'" ;
206
+ i ++;
207
+ if (i != u .getValue ().getKeys ().size ()) {
208
+ change = change + ", " ;
209
+ }
210
+ }
211
+ sql = sql + "UPDATE " + u .getTable () + " SET " + change + " WHERE " + u .getFilter () + "; " ;
212
+ }
213
+ Statement stmt = null ;
214
+ try {
215
+ stmt = con .createStatement ();
216
+ stmt .execute (sql );
217
+ } catch (SQLException e ) {
218
+ e .printStackTrace ();
219
+ } finally {
220
+ if (stmt != null ) {
221
+ try {
222
+ stmt .close ();
223
+ return true ;
224
+ } catch (SQLException e ) {
225
+ e .printStackTrace ();
226
+ }
227
+ }
228
+ }
229
+ return false ;
230
+ }
231
+
232
+ public Result rowSelect (String table , String columns , String filter ) {
166
233
if (columns == null || columns .equals ("" )) {
167
234
columns = "*" ;
168
235
}
@@ -172,21 +239,81 @@ public CachedRowSetImpl rowSelect(String table, String columns, String filter) {
172
239
}
173
240
sql = sql + ";" ;
174
241
175
- Statement stmt = null ;
176
- ResultSet res = null ;
242
+ Statement stmt ;
243
+ ResultSet res ;
177
244
try {
178
245
stmt = con .createStatement ();
179
246
res = stmt .executeQuery (sql );
180
- CachedRowSetImpl crs = new CachedRowSetImpl ();
181
- crs .populate (res );
182
- stmt .close ();
183
- return crs ;
247
+ ResultSetMetaData resmeta = res .getMetaData ();
248
+ Result result = new Result ();
249
+ while (res .next ()) {
250
+ Row row = new Row ();
251
+ int i = 1 ;
252
+ boolean bound = true ;
253
+ while (bound ) {
254
+ try {
255
+ row .addcolumn (resmeta .getColumnName (i ), res .getObject (i ));
256
+ } catch (SQLException e ) {
257
+ bound = false ;
258
+ }
259
+
260
+ i ++;
261
+ }
262
+ result .addrow (row );
263
+ }
264
+ return result ;
184
265
185
266
} catch (SQLException e ) {
186
267
e .printStackTrace ();
268
+ return new Result ();
187
269
}
188
- return null ;
189
270
}
271
+ public Result rowSelect (Select s ) {
272
+ String sql = "" ;
273
+ String columns ;
274
+ String lsql ;
275
+ if (s .getColumns () == null || s .getColumns ().equals ("" )) {
276
+ columns = "*" ;
277
+ } else {
278
+ columns = s .getColumns ();
279
+ }
280
+ lsql = "# noinspection SqlResolveForFile
281
+
282
+ SELECT " + columns + " FROM " + s .getTable ();
283
+ if (s .getFilter () != null && !s .getFilter ().equals ("" )) {
284
+ lsql = lsql + " WHERE " + s .getFilter ();
285
+ }
286
+ lsql = lsql + "; " ;
287
+ sql = sql + lsql ;
288
+
289
+ Statement stmt ;
290
+ ResultSet res ;
291
+ try {
292
+ stmt = con .createStatement ();
293
+ res = stmt .executeQuery (sql );
294
+ ResultSetMetaData resmeta = res .getMetaData ();
295
+ Result result = new Result ();
296
+ while (res .next ()) {
297
+ Row row = new Row ();
298
+ int i = 1 ;
299
+ boolean bound = true ;
300
+ while (bound ) {
301
+ try {
302
+ row .addcolumn (resmeta .getColumnName (i ), res .getObject (i ));
303
+ } catch (SQLException e ) {
304
+ bound = false ;
305
+ }
306
+ i ++;
307
+ }
308
+ result .addrow (row );
309
+ }
310
+ return result ;
311
+ } catch (SQLException e ) {
312
+ e .printStackTrace ();
313
+ return new Result ();
314
+ }
315
+ }
316
+
190
317
public boolean custom (String sql ) {
191
318
Statement stmt = null ;
192
319
try {
0 commit comments