Created
November 24, 2017 00:19
-
-
Save bartenbach/5668b0b885a8c4c6150724ea7d54369c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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