@@ -35,8 +35,7 @@ public SQLStorage(SQLDatabase db) {
3535 this .db = db ;
3636 }
3737
38- private void createTable () throws SQLException , StorageConnectionException {
39- // FIX: The Connection is now inside the try-with-resources block
38+ private void createTable () throws SQLException {
4039 try (Connection conn = db .getConnection ();
4140 PreparedStatement ps = conn .prepareStatement (
4241 "CREATE TABLE IF NOT EXISTS kits (KITID VARCHAR(100), KITDATA TEXT(15000), PRIMARY KEY (KITID))" )) {
@@ -48,7 +47,7 @@ private void createTable() throws SQLException, StorageConnectionException {
4847 public void init () throws StorageOperationException {
4948 try {
5049 createTable ();
51- } catch (SQLException | StorageConnectionException e ) { // FIX: Added StorageConnectionException
50+ } catch (SQLException e ) {
5251 throw new StorageOperationException ("Failed to initialize the database" , e );
5352 }
5453 }
@@ -78,32 +77,29 @@ public void close() throws StorageConnectionException {
7877
7978 @ Override
8079 public void keepAlive () throws StorageConnectionException {
81- // FIX: The Connection is now inside the try-with-resources block
8280 try (Connection conn = db .getConnection ();
8381 PreparedStatement ps = conn .prepareStatement ("SELECT 1" )) {
84- ps .executeQuery (); // Some drivers require executeQuery for SELECT
82+ ps .executeQuery ();
8583 } catch (SQLException e ) {
8684 throw new StorageConnectionException ("Failed to keep the connection alive" , e );
8785 }
8886 }
8987
9088 @ Override
9189 public void saveKitDataByID (String kitID , String data ) {
92- // FIX: The Connection is now inside the try-with-resources block
9390 try (Connection conn = db .getConnection ();
9491 PreparedStatement ps = conn .prepareStatement (
9592 "REPLACE INTO kits (KITID, KITDATA) VALUES (?,?)" )) {
9693 ps .setString (1 , kitID );
9794 ps .setString (2 , data );
9895 ps .executeUpdate ();
99- } catch (SQLException | StorageConnectionException e ) {
96+ } catch (SQLException e ) {
10097 e .printStackTrace ();
10198 }
10299 }
103100
104101 @ Override
105102 public String getKitDataByID (String kitID ) {
106- // FIX: The Connection is now inside the try-with-resources block
107103 try (Connection conn = db .getConnection ();
108104 PreparedStatement ps = conn .prepareStatement (
109105 "SELECT KITDATA FROM kits WHERE KITID=?" )) {
@@ -113,37 +109,35 @@ public String getKitDataByID(String kitID) {
113109 return rs .getString ("KITDATA" );
114110 }
115111 }
116- } catch (SQLException | StorageConnectionException e ) {
112+ } catch (SQLException e ) {
117113 e .printStackTrace ();
118114 }
119- return "Error" ; // Return "Error" if not found or on exception
115+ return "Error" ;
120116 }
121117
122118 @ Override
123119 public boolean doesKitExistByID (String kitID ) {
124- // FIX: The Connection is now inside the try-with-resources block
125120 try (Connection conn = db .getConnection ();
126121 PreparedStatement ps = conn .prepareStatement (
127122 "SELECT KITID FROM kits WHERE KITID=?" )) {
128123 ps .setString (1 , kitID );
129124 try (ResultSet rs = ps .executeQuery ()) {
130125 return rs .next ();
131126 }
132- } catch (SQLException | StorageConnectionException e ) {
127+ } catch (SQLException e ) {
133128 e .printStackTrace ();
134129 }
135130 return false ;
136131 }
137132
138133 @ Override
139134 public void deleteKitByID (String kitID ) {
140- // FIX: The Connection is now inside the try-with-resources block
141135 try (Connection conn = db .getConnection ();
142136 PreparedStatement ps = conn .prepareStatement (
143137 "DELETE FROM kits WHERE KITID=?" )) {
144138 ps .setString (1 , kitID );
145139 ps .executeUpdate ();
146- } catch (SQLException | StorageConnectionException e ) {
140+ } catch (SQLException e ) {
147141 e .printStackTrace ();
148142 }
149143 }
0 commit comments