Search code examples
javaspringoracle-databasejdbctemplate

JdbcTemplate does not return a proper resultset. Why?


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, enter image description here

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?


Solution

  • 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.