11package awana .database ;
22
3- import java .io .IOException ;
43import java .sql .*;
54import java .util .ArrayList ;
65import java .util .logging .Level ;
76import java .util .logging .Logger ;
87import javax .swing .DefaultListModel ;
8+ import javax .swing .JFrame ;
9+ import javax .swing .JOptionPane ;
910import org .h2 .jdbcx .JdbcDataSource ;
10- import java .nio .file .*;
1111//TODO: rewrite wrapper using http://iciql.com/ to simplify database connections.
1212
1313/**
1414 *
15- * @author Renlar
15+ * @author Renlar <liddev.com>
1616 */
1717public class DatabaseWrapper {
1818
19+ public static final String databaseLocked = "Awana Database can not start because the database is locked.\n Either the program is already running, or it did not shutdown properly last time it was run." ;
1920 public static final String altar = "ALTAR" ;
2021 public static final String create = "CREATE " ;
2122 public static final String delete = "DELETE " ;
@@ -50,15 +51,15 @@ public void deleteRecord(Record r) {
5051 try {
5152 runStatement (delete + from + dataTable + " " + where + "ID = \' " + r .getID () + "\' ;" );
5253 } catch (Exception ex ) {
53- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
54+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
5455 }
5556 }
5657
5758 public void deleteListing (Listing l ) {
5859 try {
5960 runStatement (delete + from + dataTable + " " + where + "ID = '" + l .getID () + "';" );
6061 } catch (Exception ex ) {
61- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
62+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
6263 }
6364 }
6465
@@ -70,7 +71,7 @@ public Record getRecord(int ID) {
7071 result .first ();
7172 s = loadRecordData (result , ID );
7273 } catch (Exception ex ) {
73- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
74+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
7475 return null ;
7576 }
7677 return s ;
@@ -98,7 +99,7 @@ public void saveRecord(Record record) {
9899 try {
99100 runStatement (builder .toString ());
100101 } catch (Exception ex ) {
101- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
102+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
102103 }
103104 }
104105
@@ -110,7 +111,7 @@ public Record newRecord() {
110111 ID = runStatement (builder .toString ());
111112 return new Record (ID );
112113 } catch (Exception ex ) {
113- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
114+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
114115 return null ;
115116 }
116117 }
@@ -131,7 +132,7 @@ public DefaultListModel<Listing> getRecordListingsAsDefaultListModel() {
131132 listModel .addElement (r );
132133 }
133134 } catch (Exception ex ) {
134- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
135+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
135136 }
136137 return listModel ;
137138 }
@@ -155,15 +156,19 @@ private void connectToDatabase(JdbcDataSource ds) {
155156 try {
156157 h2DatabaseConnection = ds .getConnection ();
157158 } catch (SQLException ex ) {
158- Logger .getLogger (DatabaseWrapper .class .getName ()).log (Level .SEVERE , null , ex );
159+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
160+ JOptionPane .showMessageDialog (new JFrame (), databaseLocked ,
161+ "Database Locked" ,
162+ JOptionPane .ERROR_MESSAGE );
163+ System .exit (-2000 );
159164 }
160165 }
161166
162167 public int runStatement (String sqlstmt ) {
163168 int ID = 0 ;
164- System .out .println (sqlstmt );
169+
170+ Logger .getLogger ("global" ).log (Level .INFO , null , sqlstmt );
165171 PreparedStatement stmt ;
166- System .out .print ("Status: " );
167172 try {
168173 stmt = h2DatabaseConnection .prepareStatement (sqlstmt , Statement .RETURN_GENERATED_KEYS );
169174 stmt .executeUpdate ();
@@ -172,26 +177,24 @@ public int runStatement(String sqlstmt) {
172177 ID = keys .getInt (1 );
173178 }
174179 stmt .close ();
175- System . out . println ( " Success" );
180+ Logger . getLogger ( "global" ). log ( Level . INFO , "Status: Success" );
176181 return ID ;
177182 } catch (SQLException ex ) {
178- System .out .println ("Failed" );
179- //System.err.println("SQLException: " + ex.getMessage());
180- Logger .getLogger (DatabaseWrapper .class .getName ()).log (Level .SEVERE , null , ex );
183+ Logger .getLogger ("global" ).log (Level .INFO , "Status: Failed" );
184+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
181185 return ID ;
182186 }
183187 }
184188
185189 public ResultSet executeQuery (String sqlstmt ) throws Exception {
186- System .out .println (sqlstmt );
187- System .out .print ("Status: " );
190+ Logger .getLogger ("global" ).log (Level .INFO , sqlstmt );
188191 try {
189192 ResultSet result = queryDatabase (sqlstmt );
190- System . out . println ( " Success" );
193+ Logger . getLogger ( "global" ). log ( Level . INFO , "Status: Success" );
191194 return result ;
192195 } catch (Exception ex ) {
193- System . out . println ( " Failed" );
194- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
196+ Logger . getLogger ( "global" ). log ( Level . INFO , "Status: Failed" );
197+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
195198 throw ex ;
196199 }
197200 }
@@ -207,7 +210,7 @@ public void printResultSet(ResultSet result) {
207210 printQueryResults (result );
208211 result .first ();
209212 } catch (SQLException ex ) {
210- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
213+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
211214 }
212215 }
213216
@@ -234,7 +237,7 @@ public void closeDatabase() {
234237 h2DatabaseConnection .close ();
235238 h2DatabaseConnection = null ;
236239 } catch (SQLException ex ) {
237- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
240+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
238241 }
239242 }
240243
@@ -344,7 +347,7 @@ public boolean tableExists(String table) {
344347 return true ;
345348 }
346349 } catch (Exception ex ) {
347- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
350+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
348351 }
349352 return false ;
350353 }
@@ -355,7 +358,7 @@ public ResultSet getColumnProperties(String table) {
355358 ResultSet r = h2DatabaseConnection .getMetaData ().getColumns (null , null , dataTable , null );
356359 return r ;
357360 } catch (Exception ex ) {
358- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
361+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
359362 return null ;
360363 }
361364 }
@@ -371,7 +374,7 @@ public String getDefaultValue(int rowNumber, ResultSet results) {
371374 s = results .getString (13 );
372375 return s ;
373376 } catch (SQLException ex ) {
374- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
377+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
375378 return null ;
376379 }
377380 }
@@ -387,7 +390,7 @@ public int resultSetContainsNameDataTypeAndDefaultValue(String fieldName, String
387390 return containsInRow ;
388391 }
389392 } catch (SQLException ex ) {
390- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
393+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
391394 }
392395 }
393396 return -1 ;
@@ -404,7 +407,7 @@ public int resultSetContainsNameAndDataType(String fieldName, String fieldDataTy
404407 return containsInRow ;
405408 }
406409 } catch (SQLException ex ) {
407- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
410+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
408411 }
409412 }
410413 return -1 ;
@@ -421,7 +424,7 @@ public int resultSetContainsName(String fieldName, ResultSet results) {
421424 }
422425 }
423426 } catch (SQLException ex ) {
424- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
427+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
425428 }
426429 return contains ;
427430 }
@@ -440,7 +443,7 @@ private Record loadRecordData(ResultSet result, int ID) {
440443 }
441444 result .first ();
442445 } catch (SQLException ex ) {
443- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
446+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
444447 return null ;
445448 }
446449 s .setFieldList (fields );
@@ -465,15 +468,15 @@ private ArrayList<Book> loadBookDataAsArrayList(ResultSet result) {
465468 section = new Section (Book .bookSections [i ][j ], isCompleted , date );
466469 sections .add (section );
467470 } catch (SQLException ex ) {
468- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
471+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
469472 return null ;
470473 }
471474 }
472475 try {
473476 isCompleted = result .getBoolean (bookPrefix + Book .bookNames [i ] + completedPostfix );
474477 date = result .getString (bookPrefix + Book .bookNames [i ] + datePostfix );
475478 } catch (SQLException ex ) {
476- Logger .getLogger (DatabaseWrapper . class . getName () ).log (Level .SEVERE , null , ex );
479+ Logger .getLogger ("global" ).log (Level .SEVERE , null , ex );
477480 return null ;
478481 }
479482 book = new Book (Book .bookNames [i ], sections , isCompleted , date );
0 commit comments