question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ResultSetMapper for List<Map<String, Object>> for SqlQuery

See original GitHub issue

Hi, I’m trying to create mapper for regular SqlQuery in the same manner like handle.select that is returning List of Map String, Object

Code example:

public interface SomeQueries
{
    @SqlQuery("select * from SOMETABLE")
    @Mapper(MapMappper.class)
    public List<Map<String, Object>> sqlQuery();
}

public class MapMappper implements ResultSetMapper<List<Map<String, Object>>> {

    @Override
    public List<Map<String, Object>> map(int index, ResultSet r, StatementContext ctx) throws SQLException {
        System.out.println(r);
        Map<String, Object> obj = new HashMap<String, Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        list.add(obj);
        return list;
    }
}

But I’m getting exception: unable to access mapper

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
qualidafialcommented, Jan 2, 2017

Try this:

public class MapMapper implements ResultSetMapper<Map<String, Object>> {
    @Override
    public Map<String, Object> map(int index, ResultSet r, StatementContext ctx) throws SQLException {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 1; i <= r.getMetaData().getColumnCount(); i++) {
            map.put(r.getMetaData().getColumnLabel(), r.getObject(i));
        }
        return map;
    }
}

I should point out that the default mapper used by Query is already available as public API: org.skife.jdbi.v2.DefaultMapper. You can just use it directly if you want.

0reactions
michael-newsrxcommented, Nov 22, 2022

How to do this in JDBI3?

@Override
	@SqlQuery("select * from "+table+" where articleId = :id")
	@RegisterRowMapper(MapMapper.class)
	Map<String, Object> rowAsMap(@Bind("id") int id);

@Override
	@SqlQuery("select * from "+table+" where articleId>=:from AND articleId<:before")
	@RegisterRowMapper(MapMapper.class)
	List<Map<String, Object>> rowsAsMapList(@Bind("from") int from, @Bind("before") int before);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make Mapper to sql query - Google Groups
public class MapMappper implements ResultSetMapper<List<Map<String, Object>>> { > > @Override > public List<Map<String, Object>> map(int ...
Read more >
Cannot get all rows when using ResultSetMapper to parse ...
Im using Java 8 and Jdbi version 2.78 to query a database. I have a table of key-value configurations for merchants. enter image...
Read more >
Jdbi 3 Developer Guide
A Query is a result-bearing SQL statement that returns a result set from the database. List<Map<String, Object>> users = handle.createQuery( ...
Read more >
Spring JdbcTemplate Querying Examples - Mkyong.com
In Spring, we can use jdbcTemplate.queryForObject() to query a single row record from database, and convert the row into an object via row ......
Read more >
Java JDBC How to - Create Utility for Working with ResultSets
//from w w w. jav a2 s .co m import java.sql. ... static List<Map<String, Object>> map(ResultSet rs) throws SQLException { List<Map<String, Object>> results ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found