跟我StepByStep学FLEX教程------访问数据库
2009-09-09 00:00:00 来源:WEB开发网• Queries, using convenience methods
代码:int count = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM USER");
代码:String name = (String) jdbcTemplate.queryForObject("SELECT name FROM USER WHERE user_id = ?", new Object[] {id}, java.lang.String.class);
代码:List rows = jdbcTemplate.queryForList("SELECT * FROM USER");
Returns an ArrayList (one entry for each row) of HashMaps (one entry for each column using the column name as the key)
代码:
List rows = jdbcTemplate.queryForList("SELECT * FROM USER");
Iterator it = rows.iterator();
while(it.hasNext()) {
Map userMap = (Map) it.next();
System.out.print(userMap.get("user_id") + "t");
System.out.print(userMap.get("name") + "t");
System.out.print(userMap.get("sex") + "t");
System.out.println(userMap.get("age") + "t");
}
2)JDBC的callback方式
• Queries, using callback method
A)processRow
在查询到数据之后先作一些处理再传回。可以实现org.springframework.jdbc.core.RowCallbackHandler接口
代码:------单行查询
final User user = new User();
jdbcTemplate.query("SELECT * FROM USER WHERE user_id = ?",
new Object[] {id},
new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException { //需要注意: 匿名内部类 只能访问外部最终局部变量
user.setId(rs.getString("user_id"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex").charAt(0));
user.setAge(rs.getInt("age"));
}
});
Tags:StepByStep FLEX 教程
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接