jdbcTemplate query() guaranteed to maintain resultset order?

╄→尐↘猪︶ㄣ 提交于 2020-07-05 09:14:20

问题


My question is similar to the one asked here: http://forum.springsource.org/showthread.php?84508-jdbctemplate.query()-sorted-result-set but a clear answer was not provided - an ArrayList does not guarantee order.

Basically, I would like to know if the call returned by jdbcTemplate.query() guarantees the order of the resultset and if I can dump it into a LinkedList and pass it along :)

Thanks!

Edit: I should clarify that the query does include an order by clause, hence my requirements for a resultset that guarantees order. I was incorrect regarding the ArrayList not doing so. Since the jdbcTemplate is an interface the implementation would depend upon the db library. Should I make the assumption that an ArrayList will be used or sort it again to be on the safe side?


回答1:


Does this answer your question?

public List<T> extractData(ResultSet rs) throws SQLException {
    List<T> results = (this.rowsExpected > 0 ? new ArrayList<T>(this.rowsExpected) : new ArrayList<T>());
    int rowNum = 0;
    while (rs.next()) {
        results.add(this.rowMapper.mapRow(rs, rowNum++));
    }
    return results;
}

From RowMapperResultSetExtractor. This class is used in in several query() methods in JdbcTemplate.

No place for result set records mixing. In fact, result set is more like an iterator or stream rather than a full collection. This means that it would be very inefficient to change the order because you would have to load all the records from result set in advance.




回答2:


an ArrayList does not guarantee order.

This is just wrong. An ArrayList does guarantee order. It has a get(int index) method which can be used to retrieve an element from the specified 0-based index. The add(T item) method will add them in sequence to the list. You may be confusing the List collection type with the Set interface, which is similar to a List except for it does not guarantee order.

That said, it does not actually answer your question...

The ResultSet, without a specified ordering, will return the data in the table's natural order. This is usually based of of the PK field(s) but this is not true for all DBMSs.

If order is important, specify it to be certain. Even if it comes back in the desired order now, changes to the DB schema might affect this later and break assumptions made by your code. Being explicit is better, and will express the code's intent clearer.

Update from edit to question: If an order by is specified in the query, you can rely 100% on the order of the result set. No need to sort it again.




回答3:


Its not required to sort records again if you are using order by condtion in sql, check your stored procedure cursor output query " OPEN o_provisionalOrders FOR SELECT cols from table order by condtion " is proper.



来源:https://stackoverflow.com/questions/6270213/jdbctemplate-query-guaranteed-to-maintain-resultset-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!