我要做的是使用queryForObject从studentstable中选择用户名,密码和角色。
我的JdbcTemplate语法是
public static Object queryForObject(String sql,RowMAPPER mapper,Object ...args)
问题在于我的JdbcStudentDAO
public class JdbcStudentDAO implements StudentDAO{
public String getLogin(StudentTO sto) {
String sql="select username,password,role from studentstable";
System.out.println(sql);
这里我不知道下面有什么问题
Object obj=JdbcTemplate.queryForObject(sql,new StudentRowMapper(),sto.getUsername(),sto.getPassword(),sto.getRole());
StudentTO sto1=(StudentTO)obj;
System.out.println(sto1);
return sto1.toString();
}
}
这是我的RowMapper,我将获取数据库的所有行,如下所示
public class StudentRowMapper implements RowMapper{
public Object mapRow(ResultSet rs) throws SQLException {
StudentTO sto=new StudentTO();
sto.setSid(rs.getInt(1));
sto.setName(rs.getString(2));
sto.setUsername(rs.getString(3));
sto.setPassword(rs.getString(4));
sto.setEmail(rs.getString(5));
sto.setPhone(rs.getLong(6));
sto.setRole(rs.getString(7));
return sto;
}
}
这是StudentDAO中的抽象方法
public interface StudentDAO {
public String getLogin(StudentTO sto);
}
答案 0 :(得分:0)
这是您的查询
select username,password,role from studentstable
您只有3列,并且它们不是您的提取所定义的顺序...
sto.setSid(rs.getInt(1)); sto.setName(rs.getString(2)); sto.setUsername(rs.getString(3)); // This is actually index 1 sto.setPassword(rs.getString(4)); // This is actually index 2 sto.setEmail(rs.getString(5)); sto.setPhone(rs.getLong(6)); sto.setRole(rs.getString(7)); // This is actually index 3
因此,你需要这个
sto.setUsername(rs.getString(1));
sto.setPassword(rs.getString(2));
sto.setRole(rs.getString(3));
其他属性是默认值