WEB开发网
开发学院图形图像Flash 跟我StepByStep学FLEX教程------访问数据库... 阅读

跟我StepByStep学FLEX教程------访问数据库

 2009-09-09 00:00:00 来源:WEB开发网   
核心提示: 代码:--多行查询final List employees = new LinkedList();jdbc.query("select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE", new RowCallbackHandler() { p

代码:------多行查询

final List employees = new LinkedList();
jdbc.query("select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE",
     new RowCallbackHandler() {
         public void processRow(ResultSet rs) throws SQLException {
             Employee e = new Employee();
             e.setEmpNo(rs.getString(1));
            e.setFirstName(rs.getString(2));
            e.setLastName(rs.getString(3));
            employees.add(e);
        }
    }
);
employees list will be populated with Employee objects

B) RowMapper

一次要取回很多查询结果的对象,则可以先实现org.springframe- work.jdbc.core.RowMapper接口。

代码:------将数据表中的数据影射成其对应的JAVA类的对象,mapRow回调方法会被ResultSet中的每一行调用。

class UserRowMapper implements RowMapper {   
    public Object mapRow(ResultSet rs, int index) throws SQLException {   
        User user = new User();   
  
        user.setId(rs.getString("user_id"));   
        user.setName(rs.getString("name"));   
        user.setSex(rs.getString("sex").charAt(0));   
        user.setAge(rs.getInt("age"));   
  
        return user;   
    }   
}   

传回的结果已使用UserRowMapper的定义,将之封装为User对象。

//返回多行查询结果  
public List findAllByRowMapperResultReader() {   
    String sql = "SELECT * FROM USER";   
    return jdbcTemplate.query(sql, new RowMapperResultReader(new UserRowMapper()));   
}   
the return list will be populated with User objects

//返回单行查询结果

在getUser(id)里面使用UserRowMapper

代码

public User getUser(final String id) throws DataAccessException {   
    String sql = "SELECT * FROM USER WHERE user_id=?";   
    final Object[] params = new Object[] { id };   
    List list = jdbcTemplate.query(sql, params, new RowMapperResultReader(new UserRowMapper()));   
  
    return (User) list.get(0);   
}

文章来源:http://wangyisong.javaeye.com/blog/419443

上一页  1 2 3 4 5 

Tags:StepByStep FLEX 教程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接