Search code examples
javajspjstljooq

Loop through Result<Record> from jOOQ in <c:forEach>


Is there a way to loop through Result<Record> from jOOQ in a <c:forEach>?

Here's the getter method:

public Vector<Map<String, String>> getUsers() {
    Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
    Result<Record> results = sql.select().from("users").fetch();

    Vector<Map<String, String>> v = new Vector<Map<String, String>>();
    for(Record item: results) {
        Map<String, String> m = new HashMap<String, String>();
        m.put("login", item.getValueAsString("login"));
        // other columns
        v.add(m);
    }
    return v;
}

Here's the view:

<c:forEach var="u" items="${users}">
   ${u.login} <br />
</c:forEach>

Is there way to make my getter method simpler like:

public Result<Record> getUsers() {
    Factory sql = new Factory(Database.getInstance().connect(), SQLDialect.MYSQL);
    reutrn sql.select().from("users").fetch();
}

But as mentioned earlier I don't know how to loop through it in <c:forEach>, or maybe is it not possible?


Solution

  • It seems that you would like to operate on records as if they were maps. Here's how to achieve this with jOOQ:

    public List<Map<String, Object>> getUsers() {
        DSLContext sql = DSL.using(Database.getInstance().connect(), SQLDialect.MYSQL);
      return sql.select().from("users").fetchMaps();
    }
    

    See the relevant Javadocs here: