Spring - get ResultsSet from query

2019-06-25 11:48发布

问题:

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?

回答1:

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.



回答2:

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;
        }
    });


标签: java spring jdbc