query(String sql,RowCallbackHandler rch)
引數說明:public TbUser getUser(int id){ final TbUser user = null; String sql = "select*from tb_user where id="+id; //建立一條SQL語句 getJdbcTemplate().query(sql,new RowCallbackHandler(){ public void processRow(ResultSet rs)throws SQLException{ user.setAge(rs.getInt("age")); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setSex(rs.getString("sex")); } }); return user; }
public List getAllUsers(){ final List list = new ArrayList(); String sql = "select*from tb_user"; //建立一條SQL語句 getJdbcTemplate().query(sql,new RowCallbackHandler(){ public void processRow(ResultSet rs)throws SQLException{ TbUser user = new TbUser(); user.setAge(rs.getInt("age")); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setSex(rs.getString("sex")); list.add(user); } }); return list; }
query(String sql,Object[] args,RowCallbackHandler rch)
引數說明:public TbUser getUser(int id,int age){ final TbUser user = null; String sql = "select*from tb_user where id=?and age=?"; //建立一條SQL語句 Integer[] args = {id,age}; getJdbcTemplate().query(sql,args,new RowCallbackHandler(){ public void processRow(ResultSet rs)throws SQLException{ user.setAge(rs.getInt("age")); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setSex(rs.getString("sex")); } }); return user; }
query(String sql,RowMapper rowMapper)
引數說明:public List getAllUsers(){ String sql = "select*from tb_user"; //建立一條SQL語句 List list = getJdbcTemplate().query(sql,new RowMapper(){ public Object mapRow(ResultSet rs,int rowNum)throws SQLException{ TbUser user = new TbUser(); user.setAge(rs.getInt("age")); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setSex(rs.getString("sex")); return user; } }); return list; }
query(String sql,Object[] args,RowMapper rowMapper)
引數說明:public List getUsers(int minAge,int maxAge){ String sql = "select*from tb_user where age>?&&age<?"; //建立一條SQL語句 Integer[] args = {minAge,maxAge}; List list = getJdbcTemplate().query(sql,args,new RowMapper(){ public Object mapRow(ResultSet rs,int rowNum)throws SQLException{ TbUser user = new TbUser(); user.setAge(rs.getInt("age")); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setSex(rs.getString("sex")); return user; } }); return list; }