Skip to content

Instantly share code, notes, and snippets.

@bartenbach
Created November 24, 2017 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartenbach/5668b0b885a8c4c6150724ea7d54369c to your computer and use it in GitHub Desktop.
Save bartenbach/5668b0b885a8c4c6150724ea7d54369c to your computer and use it in GitHub Desktop.
/**
* This method prepares a statement regardless of the type of data. I thought this
* was rather clever. If the object is of type String, the function will set all of
* the data passed in, into the appropriate slots of the PreparedStatement object.
* The same will happen if the passed data happens to be of type Integer.
* @param ps The PreparedStatement to populate with data
* @param objects An array of String or Integer objects to put into the PreparedStatement.
*/
public void prepareStatement(final PreparedStatement ps, final Object... objects) {
checkConnection();
try {
for (int i = 0; i < objects.length; i++) {
if (objects[i] instanceof String) {
ps.setString((i + 1), (String) objects[i]);
} else {
ps.setInt((i + 1), Integer.parseInt(String.valueOf(objects[i])));
}
}
} catch (SQLException ex) {
log.error("Failed to set parameter in PreparedStatement. StackTrace: ", ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment