[英]Empty ResultSet from query
I'm trying to query for a set and return it as a hash map object with Spring JdbcTemplate. 我正在尝试查询一个集合并将其作为带有Spring JdbcTemplate的哈希映射对象返回。 But some reasons I'm getting empty result set from server. 但是有些原因我从服务器上得到了空的结果集。 It is possible that I have a problem in the overall configuration but the rest of the queries are working without problems. 我可能在整体配置中遇到问题,但其余查询都没有问题。
This is how I query 这是我查询的方式
public Map<Integer, String> getCompanyDataservers() {
return getTemplate().queryForObject("select id, dataserver from company", new RowMapper<Map<Integer,String>>() {
@Override
public Map<Integer, String> mapRow(ResultSet rs, int rowNum)
throws SQLException {
HashMap<Integer, String> toReturn = new HashMap<Integer, String>();
while(rs.next()) {
int id = rs.getInt("id");
toReturn.put(id, dataserver);
}
return toReturn;
}});
}
After some debugging and logging statements I concluded that my resultset seems to be empty of any rows. 在一些调试和日志记录语句之后,我得出结论,我的结果集似乎没有任何行。 When I query the same ("select id, dataserver from company") manually directly from DB I get the desired result. 当我直接从DB手动查询相同的(“select id,dataserver from company”)时,我得到了所需的结果。 Yet this way I get a resultset with 0 rows. 但是这样我得到一个0行的结果集。
One of my theories is that can't get get this kind of set when querying for a object this way. 我的一个理论是,在以这种方式查询对象时无法获得这种设置。 But isn't there a possibility to be free at you queries and construct a more elaborate object as a query result, or I have to create a purpose built class to be used by "queryForList" to get the desired data and convert it afterwards? 但是,有没有可能在你的查询中自由,并构建一个更复杂的对象作为查询结果,或者我必须创建一个专用的类,由“queryForList”用来获取所需的数据并在之后进行转换?
Or am I just missing something? 或者我只是错过了什么?
You are not supposed to invoke rs.next()
inside mapRow
. 你不应该在mapRow
调用rs.next()
。 JdbcTemplate will invoke mapRow
for you for every row in the resultset JdbcTemplate将为结果mapRow
的每一行调用mapRow
All you need to do on mapRow
is just return a HashMap which represent one single database row , spring will do the resultset iteration for you 你需要在mapRow
上做的只是返回一个代表一个数据库行的HashMap,spring将为你做结果集迭代
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.