Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - get ResultsSet from query

Tags:

java

spring

jdbc

I would like to use Spring JDBCTemplate but I would like to receive a ResultSet, which is not storing full query result in memory, as you would find executing standard statement with java JDBC. The closest I found to the ResultSet was

SqlRowSet sqlRowSet = template.getJdbcOperations().queryForRowSet(query, queryParameters);

but this loads the whole DB result into memory?

like image 641
Bober02 Avatar asked Nov 26 '12 11:11

Bober02


People also ask

How do I use BeanPropertyRowMapper?

BeanPropertyRowMapper is a RowMapper implementation that converts a table row into a new instance of the specified mapped target class. The mapped target class must be a top-level class and it must have a default or no-arg constructor.

What is RS next () in Java?

The next() method of the ResultSet interface moves the pointer of the current (ResultSet) object to the next row, from the current position. Statement stmt = con. createStatement(); ResultSet rs = stmt. executeQuery("Select * from MyPlayers"); rs. next();


2 Answers

If you want to get a ResultSet object with JDBCTemplate you can retrieve the javax.sql.Connection with the following code:

Connection conn = jdbcTemplate.getDataSource().getConnection();

And you can now perform a createStatement() or preparedStatement() to get the ResultSet object. That's the only way it comes to my mind. I hope this will help you.

like image 101
ChuyAMS Avatar answered Oct 16 '22 09:10

ChuyAMS


Is it what you are looking for?

    JdbcTemplate t = new JdbcTemplate(dataSource);
    t.query("select * from t1", new ResultSetExtractor<Object>() {
        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
            ... process your rs
            return null;
        }
    });
like image 29
Evgeniy Dorofeev Avatar answered Oct 16 '22 10:10

Evgeniy Dorofeev