-
-
Save exallium/4478100 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Generic loadData method. Any parameter that matches a column will | |
* have data loaded. | |
* @param resultSet The result set from the database query | |
* @throws IllegalAccessException The requested field couldn't be accessed | |
*/ | |
private void loadData(ResultSet resultSet) throws IllegalAccessException { | |
Field[] fields = getClass().getDeclaredFields(); | |
for (Field f : fields) { | |
try { | |
String fieldName = sqlDriver.getDatabaseColumn(f.getName()); | |
f.set(this, resultSet.getObject(fieldName)); | |
} catch (SQLException e) { | |
// Not a field | |
} | |
} | |
} |
This file contains hidden or 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
/** | |
* Manipulate string from Camel Case to underscore | |
* @param fieldName The field name to translate | |
* @return The translated name | |
*/ | |
public String getDatabaseColumn(String fieldName) { | |
return fieldName.replaceAll("([A-Z])", "_$1").toLowerCase(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment