9
9
import java .sql .PreparedStatement ;
10
10
import java .sql .ResultSet ;
11
11
import java .sql .SQLException ;
12
+ import java .sql .Statement ;
12
13
import java .sql .Savepoint ;
13
14
import java .util .logging .Level ;
14
15
import java .util .logging .Logger ;
@@ -163,6 +164,59 @@ public int executeUpdate(PreparedStatement statement) throws ApplicationExceptio
163
164
}
164
165
}
165
166
167
+ /**
168
+ * Execute an update query and return the generated keys.
169
+ *
170
+ * @param statement The prepared statement to execute.
171
+ * @return The generated keys as a ResultSet.
172
+ * @throws ApplicationException If an error occurs while executing the update.
173
+ */
174
+ public ResultSet executeUpdateAndGetGeneratedKeys (PreparedStatement statement ) throws ApplicationException {
175
+ try {
176
+ int effect = statement .executeUpdate ();
177
+ logger .log (Level .INFO , statement + " - Affected rows: " + effect );
178
+ if (effect > 0 ) {
179
+ return statement .getGeneratedKeys ();
180
+ }
181
+ return null ;
182
+ } catch (SQLException e ) {
183
+ throw new ApplicationException (e .getMessage (), e );
184
+ }
185
+ }
186
+
187
+ /**
188
+ * Execute an update query and return the generated ID.
189
+ *
190
+ * @param statement The prepared statement to execute.
191
+ * @return The generated ID, or null if no ID was generated.
192
+ * @throws ApplicationException If an error occurs while executing the update.
193
+ */
194
+ public Object executeUpdateAndGetGeneratedId (PreparedStatement statement ) throws ApplicationException {
195
+ ResultSet generatedKeys = null ;
196
+ try {
197
+ generatedKeys = executeUpdateAndGetGeneratedKeys (statement );
198
+ if (generatedKeys != null && generatedKeys .next ()) {
199
+ return generatedKeys .getObject (1 );
200
+ }
201
+ return null ;
202
+ } catch (SQLException e ) {
203
+ throw new ApplicationException (e .getMessage (), e );
204
+ } finally {
205
+ if (generatedKeys != null ) {
206
+ try {
207
+ generatedKeys .close ();
208
+ } catch (SQLException e ) {
209
+ logger .warning ("Error closing generated keys ResultSet: " + e .getMessage ());
210
+ }
211
+ }
212
+ try {
213
+ statement .close ();
214
+ } catch (SQLException e ) {
215
+ logger .warning ("Error closing PreparedStatement: " + e .getMessage ());
216
+ }
217
+ }
218
+ }
219
+
166
220
/**
167
221
* Execute a query without returning a result set.
168
222
*
@@ -187,6 +241,19 @@ public boolean execute(PreparedStatement statement) throws ApplicationException
187
241
* @throws ApplicationException If an error occurs while preparing the statement.
188
242
*/
189
243
public PreparedStatement createPreparedStatement (String sql , boolean scrollable ) throws ApplicationException {
244
+ return createPreparedStatement (sql , scrollable , false );
245
+ }
246
+
247
+ /**
248
+ * Create a PreparedStatement with the given SQL, scrollable option, and generated keys option.
249
+ *
250
+ * @param sql The SQL query.
251
+ * @param scrollable True if the result set should be scrollable, false otherwise.
252
+ * @param returnGeneratedKeys True if the statement should return generated keys, false otherwise.
253
+ * @return The prepared statement.
254
+ * @throws ApplicationException If an error occurs while preparing the statement.
255
+ */
256
+ public PreparedStatement createPreparedStatement (String sql , boolean scrollable , boolean returnGeneratedKeys ) throws ApplicationException {
190
257
if (connection == null ) {
191
258
connection = manager .getConnection ();
192
259
}
@@ -198,9 +265,13 @@ public PreparedStatement createPreparedStatement(String sql, boolean scrollable)
198
265
int resultSetType = scrollable ? ResultSet .TYPE_SCROLL_INSENSITIVE : ResultSet .TYPE_FORWARD_ONLY ;
199
266
int resultSetConcurrency = ResultSet .CONCUR_READ_ONLY ;
200
267
201
- return connection .prepareStatement (sql ,
202
- resultSetType ,
203
- resultSetConcurrency );
268
+ if (returnGeneratedKeys ) {
269
+ return connection .prepareStatement (sql , Statement .RETURN_GENERATED_KEYS );
270
+ } else {
271
+ return connection .prepareStatement (sql ,
272
+ resultSetType ,
273
+ resultSetConcurrency );
274
+ }
204
275
} catch (SQLException e ) {
205
276
throw new ApplicationException (e .getMessage (), e );
206
277
}
0 commit comments