simpleJdbcTemplate中的方法query()不返回值

时间:2012-04-24 21:44:38

标签: java templates jdbc methods

我的班级是这样的:

public class DBConection extends SimpleJdbcDaoSupport implements ElectionsDao{
public List<String> getDates(){
        try{
            String sql = "SELECT electiondate FROM electiondate";
            List<String> dates = new ArrayList<String>();
            dates = getSimpleJdbcTemplate().query(sql,  
                    ParameterizedBeanPropertyRowMapper.newInstance(String.class));

            System.out.println(dates.size());
            System.out.println(dates.get(0));

            return dates;
        }catch(DataAccessException ex){
            throw new RuntimeException(ex);
        }
    }
}

我正在尝试从SQL语句中获取值,然后将它们作为String对象添加到List中,但是当我运行我的项目时,它返回值的数量,但全部为空。有人知道为什么吗?我有我的配置文件和一切。我认为它必须是定义query()的问题。我正在使用Spring框架。

1 个答案:

答案 0 :(得分:0)

我已经弄明白该怎么做了。

我是这样做的:

public class DBConection extends SimpleJdbcDaoSupport implements ElectionsDao{
    public List<String> getDates(){
        List<String> dates = new ArrayList<String>();
        try {
            dates = getSimpleJdbcTemplate().query("SELECT electiondate FROM electiondate";, new StringRowMapper());
        } catch (DataAccessException ex){
        throw new RuntimeException(ex);
        }
        return dates;
    }

    protected static final class StringRowMapper implements ParameterizedRowMapper<String> {
        public String mapRow(ResultSet rs, int line) throws SQLException {
            String string = new String(rs.getString("electiondate"));
            return string;
        }
    }
}

我必须做一个“StringRowMapper”内部类才能使它工作。

希望它可以帮到你!