11package de .tallerik ;
22
33import com .mysql .cj .jdbc .MysqlDataSource ;
4- import com . sun . rowset . CachedRowSetImpl ;
4+ import de . tallerik . utils .* ;
55
66import 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 .*;
118
12- @ SuppressWarnings ("Duplicates" )
9+ @ SuppressWarnings ("Duplicates unused " )
1310public class MySQL {
1411
1512 // Vars
@@ -66,14 +63,14 @@ public boolean connect() {
6663 dataSource .setDatabaseName (db );
6764 dataSource .setUser (user );
6865 dataSource .setPassword (password );
69-
66+ dataSource . setAllowMultiQueries ( true );
7067 con = dataSource .getConnection ();
7168 System .out .println ("Connection established" );
7269 return true ;
7370 }
7471 catch (SQLException ex )
7572 {
76- System . out . println ( ex );
73+ ex . printStackTrace ( );
7774 return false ;
7875 }
7976 }
@@ -133,6 +130,43 @@ public boolean tableInsert(String table, String columns, String... data) {
133130 }
134131 return false ;
135132 }
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+
136170 public boolean rowUpdate (String table , UpdateValue value , String filter ) {
137171 String change = "" ;
138172 int i = 0 ;
@@ -162,7 +196,40 @@ public boolean rowUpdate(String table, UpdateValue value, String filter) {
162196 }
163197 return false ;
164198 }
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 ) {
166233 if (columns == null || columns .equals ("" )) {
167234 columns = "*" ;
168235 }
@@ -172,21 +239,81 @@ public CachedRowSetImpl rowSelect(String table, String columns, String filter) {
172239 }
173240 sql = sql + ";" ;
174241
175- Statement stmt = null ;
176- ResultSet res = null ;
242+ Statement stmt ;
243+ ResultSet res ;
177244 try {
178245 stmt = con .createStatement ();
179246 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 ;
184265
185266 } catch (SQLException e ) {
186267 e .printStackTrace ();
268+ return new Result ();
187269 }
188- return null ;
189270 }
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+
190317 public boolean custom (String sql ) {
191318 Statement stmt = null ;
192319 try {
0 commit comments