Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82a82cb

Browse files
committed
Add two new methods to support generated key to be retreived.
1 parent a15a7b6 commit 82a82cb

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

src/main/java/org/tinystruct/data/DatabaseOperator.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.sql.PreparedStatement;
1010
import java.sql.ResultSet;
1111
import java.sql.SQLException;
12+
import java.sql.Statement;
1213
import java.sql.Savepoint;
1314
import java.util.logging.Level;
1415
import java.util.logging.Logger;
@@ -163,6 +164,59 @@ public int executeUpdate(PreparedStatement statement) throws ApplicationExceptio
163164
}
164165
}
165166

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+
166220
/**
167221
* Execute a query without returning a result set.
168222
*
@@ -187,6 +241,19 @@ public boolean execute(PreparedStatement statement) throws ApplicationException
187241
* @throws ApplicationException If an error occurs while preparing the statement.
188242
*/
189243
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 {
190257
if (connection == null) {
191258
connection = manager.getConnection();
192259
}
@@ -198,9 +265,13 @@ public PreparedStatement createPreparedStatement(String sql, boolean scrollable)
198265
int resultSetType = scrollable ? ResultSet.TYPE_SCROLL_INSENSITIVE : ResultSet.TYPE_FORWARD_ONLY;
199266
int resultSetConcurrency = ResultSet.CONCUR_READ_ONLY;
200267

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+
}
204275
} catch (SQLException e) {
205276
throw new ApplicationException(e.getMessage(), e);
206277
}

0 commit comments

Comments
 (0)