I have a Java application that uses MySQL through JDBC.
Currently I have one DbFooHandler class for each table Foo and I collect all sql code related to the Foo table in this class.
It works quite well and I get the interface I want. The drawback is the ammount of boiler-plate code. I have a bunch of methods that looks roughly like this:
public int getUserPostCount(int accId) {
String sql =
"SELECT COUNT(*) AS postCount " +
"FROM ForumPosts " +
"WHERE accId = ?";
try {
PreparedStatement ps = db.prepare(sql, accId);
ResultSet rs = ps.executeQuery();
rs.first();
int postCount = rs.getInt("postCount");
ps.close();
return postCount;
} catch (SQLException sqle) {
throw new RuntimeException(sqle);
}
}
I've now looked at Grails and that kind of code is just beautiful.
My question is: Can/should grails be used for database-parts in a generic java-application?
(I have tried Cayenne and looked at Hibernate but those frameworks doesn't work well with my class-hierarchy.)