Some time back, I was in same problem to deal with. After some pondering over this design we decided to do it like below.
public static Properties execute(String string, String[] columnames) throws Exception {
Properties resulProperties = er.executeQuery(string, columnames);
return resulProperties;
}
For some specific reason, I created a field in my class as given below
private static ExecuteRequest er = new ExecuteRequest();
In ExecuteRequest class below code is used.
public Properties executeQuery(String sqlstatement, String[] columnNames) throws Exception {
Properties prop = new Properties();
try {
prop = creteProperty(sqlstatement, columnNames);
} catch (Exception e) {
mlogger.report("Error executing sql statement");
throw (e);
}
return prop;
}
public Properties creteProperty(String sqlstatement, String[] columnNames) throws Exception {
Properties prop = new Properties();
try {
PreparedStatement stmt = ConnectionManager.getInstance().prepareStatement(sqlstatement);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
for (int i = 0; i < columnNames.length; i++) {
String key = columnNames[i];
if (rs.getObject(key) != null) {
String value = (rs.getObject(key).toString());
prop.setProperty(key, value);
} else {
String value = "";
prop.setProperty(key, value);
}
}
}
rs.close();
} catch (Exception e) {
mlogger.report("Error executing sql statement");
throw (e);
}
return prop;
}
You can use this approach as a solution.