![]() |
Beyond Java Buzz |
![]() |
| home |
below is an example of a Dao pattern implementation
private Result getResultsetData(String sql) {
Connection conn = null;
ResultSet rs = null;
int colCount = 0;
int[] colWidths = null;
long rowCount = 0;
String[] colHeaders;
Result result;
try {
conn = this.getConnection();
rs = conn.prepareStatement(sql).executeQuery();
colCount = rs.getMetaData().getColumnCount();
result = new Result(colCount);
while (rs.next()) {
//append to buffer
for (int i = 1; i < colCount + 1; i++) {
result.add(rs.getString(i));
}
}
return result;
} catch (Exception e) {
return new Result( e.getMessage());
} finally {
try {
rs.close();
} catch (Exception ee) { }
try {
conn.close();
} catch (Exception ee) { }
}
}