JdbcTemplate没有返回正确的结果集。为什么?

huangapple go评论119阅读模式
英文:

JdbcTemplate does not return a proper resultset. Why?

问题

我正在使用Spring制作一个讨论板。

我正在使用JdbcTemplate从数据库中填充用户的文章,但是JdbcTemplatequery方法没有返回正确的ResultSet。有趣的是,当我将代码中的SQL查询复制粘贴到SQL Developer中时,它返回了正确的结果。

显示SQL查询有效的图片,
JdbcTemplate没有返回正确的结果集。为什么?

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&#39;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没有返回正确的结果集。为什么?

JdbcTemplate code

public class ForumDao {
	private JdbcTemplate template;
	
	
	public ForumDao(DataSource dataSource) {
		template = new JdbcTemplate(dataSource);
	}
	
	public Collection&lt;ForumArticle&gt; getArticleList(){
		Collection&lt;ForumArticle&gt; list = template.query(&quot;SELECT ARTICLE_ID, TITLE, NAME, VIEW_NUM, CREATED_DATE FROM MEMBER, FORUM WHERE MEMBER.ID = FORUM.MEMBER_ID&quot;, 
				new RowMapper&lt;ForumArticle&gt;() {

					@Override
					public ForumArticle mapRow(ResultSet rs, int rowNum) throws SQLException {
						
						ForumArticle article = new ForumArticle();
						System.out.println(&quot;completeeeee--------------------------------------------------------------------&quot;);
						article.setArticleID(rs.getInt(&quot;ARTICLE_ID&quot;));
						article.setTitle(rs.getString(&quot;TITLE&quot;));
						article.setName(rs.getString(&quot;NAME&quot;));
						article.setViewNum(rs.getLong(&quot;VIEW_NAME&quot;));
						article.setCreatedDate(rs.getTimestamp(&quot;CREATED_DATE&quot;).toLocalDateTime());
						
						return article;
					}			

		});
		System.out.println(&quot;-dddddddddddddddddddddddddddddddddddd &quot; + 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(&quot;VIEW_NAME&quot;));

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.

huangapple
  • 本文由 发表于 2020年10月7日 09:17:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64235860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定