英文:
JdbcTemplate does not return a proper resultset. Why?
问题
我正在使用Spring制作一个讨论板。
我正在使用JdbcTemplate
从数据库中填充用户的文章,但是JdbcTemplate
的query
方法没有返回正确的ResultSet
。有趣的是,当我将代码中的SQL查询复制粘贴到SQL Developer中时,它返回了正确的结果。
JdbcTemplate代码
public class ForumDao {
private JdbcTemplate template;
public ForumDao(DataSource dataSource) {
template = new JdbcTemplate(dataSource);
}
public Collection<ForumArticle> getArticleList() {
Collection<ForumArticle> list = template.query(
"SELECT ARTICLE_ID, TITLE, NAME, VIEW_NUM, CREATED_DATE FROM MEMBER, FORUM WHERE MEMBER.ID = FORUM.MEMBER_ID",
new RowMapper<ForumArticle>() {
@Override
public ForumArticle mapRow(ResultSet rs, int rowNum) throws SQLException {
ForumArticle article = new ForumArticle();
System.out.println("completeeeee--------------------------------------------------------------------");
article.setArticleID(rs.getInt("ARTICLE_ID"));
article.setTitle(rs.getString("TITLE"));
article.setName(rs.getString("NAME"));
article.setViewNum(rs.getLong("VIEW_NAME"));
article.setCreatedDate(rs.getTimestamp("CREATED_DATE").toLocalDateTime());
return article;
}
}
);
System.out.println("-dddddddddddddddddddddddddddddddddddd " + list.size());
return list;
}
}
所有的配置设置都已正确完成,我正在使用Oracle DB。我还有另一个用于用户数据的DAO类,它的JdbcTemplate
工作正常。
当我运行代码时,list.size()
返回的是0,而不是4。它没有抛出任何异常。
这个问题可能的解决方案是什么?
英文:
I am making a discussion board with Spring.
I am using JdbcTemplate
to populate the articles of the users from the database, but the JdbcTemplate's
query
method does not return the proper ResultSet
. Interestingly, when I copy and paste the SQL query from the code to SQL Developer, it returns the proper results.
The photo that shows the SQL query works,
JdbcTemplate code
public class ForumDao {
private JdbcTemplate template;
public ForumDao(DataSource dataSource) {
template = new JdbcTemplate(dataSource);
}
public Collection<ForumArticle> getArticleList(){
Collection<ForumArticle> list = template.query("SELECT ARTICLE_ID, TITLE, NAME, VIEW_NUM, CREATED_DATE FROM MEMBER, FORUM WHERE MEMBER.ID = FORUM.MEMBER_ID",
new RowMapper<ForumArticle>() {
@Override
public ForumArticle mapRow(ResultSet rs, int rowNum) throws SQLException {
ForumArticle article = new ForumArticle();
System.out.println("completeeeee--------------------------------------------------------------------");
article.setArticleID(rs.getInt("ARTICLE_ID"));
article.setTitle(rs.getString("TITLE"));
article.setName(rs.getString("NAME"));
article.setViewNum(rs.getLong("VIEW_NAME"));
article.setCreatedDate(rs.getTimestamp("CREATED_DATE").toLocalDateTime());
return article;
}
});
System.out.println("-dddddddddddddddddddddddddddddddddddd " + list.size());
return list;
}
}
All the configuration set-up is done properly and I am using Oracle DB. I have another DAO class for user data and its JdbcTemplate
works perfectly.
When I run my code, the list.size() returns 0 instead of 4. It does not throw any exception.
What can be the possible solution for this issue?
答案1
得分: 0
以下是翻译好的部分:
以下行看起来有问题:
article.setViewNum(rs.getLong("VIEW_NAME"));
VIEW_NAME
应该是 VIEW_NUM
,对吧?
可能发生的情况是,当上述行执行时,代码会因结果集中的未知列而抛出 SQLException,从而终止处理过程并给你一个空结果。
英文:
The following line looks wrong:
article.setViewNum(rs.getLong("VIEW_NAME"));
VIEW_NAME
should be VIEW_NUM
, no?
What's probably happening is that when the above line executes, the code throws a SQLException due to an unknown column in the result set, which terminates the processing and gives you an empty result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论