方法查询()没有返回值
问题描述:
我有我的类,它是这样的:方法查询()没有返回值
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对象以一个列表,但是当我运行我的项目时,它返回值的数量,但全部为空。有人知道为什么吗?我有我的配置文件和一切。我认为它必须是定义query()
的问题。我正在使用Spring框架。
答
我已经弄明白了如何去做。
我是这样做的:
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”内部类这个工作。
希望它可以帮助你!