dynamic-query is a good open source framework for those want something between JDBC and ORM.
1 plain SQL. - It saves plain sql to external files. no redundant tags, supports comments.
/* It also supports comment.
This code is in an external file 'sample.sql', Not inisde java code.*/
listUsers : select * from user_table
where user_id= $$; /* $$ will automatically catch a parameter userId */
2 expandable SQL. -It supports parameters, including other files and sub query.
listUsers:
select
id, amount, created
@checkEmail{ ,email }
from user_table
where amount > $amt and balance < $amt
@checkDate { and created = $$ }
@checkEmail{ and email in (
select email from vip_list ) } ;
/* Above query can be four queries like below.
1. listUsers
2. listUsers.checkDate
3. listUsers.checkEmail
4. listUsers.checkDate.checkEmail
*/
-- It can include other files like below
& ../hr/additional hr.sql ;
& ../fi/additional fi.sql ;
Java example code using above. setting values to db.
QueryUtil qu = qm.createQueryUtil("selectAll");
try {
qu.setConnection(conn);
// with native jdbc
qu.setString("alpha");
qu.setDouble(10.1);
qu.executeQuery();
// or with bean
qu.executeQuery(new User("alpha", 10.1));
// or with map
Map<String, Object> map=new HashMap<String, Object>();
map.put("userName", "alpha");
map.put("amt", 10.1);
qu.executeQuery(map);
// or with array
qu.executeQueryParameters("alpha", 10.1);
Java example code using above. getting values from db.
while (qu.next()) // == qu.rs.next()
{
// native jdbc
String usreName = qu.getString("user_name");
double amt = qu.getDouble("amt");
// or bean
User user = new User();
qu.updateBean(user);
// or array
Object[] values = qu.populateArray();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
qu.closeJust();
}