Spring 2.5中JdbcTemplate类query方法的三种回调接口
来源:互联网 发布:js获取焦点和失去焦点 编辑:程序博客网 时间:2024/04/29 08:40
-
-
-
-
-
-
-
-
- public class SpringTest {
-
-
-
-
- public void getListRowMapperResultSetExtractor() {
- ApplicationContext context = new FileSystemXmlApplicationContext(
- "src/database_config.xml");
-
- JdbcTemplate jt = new JdbcTemplate((DataSource) context
- .getBean("oracleDataSourceTest"));
- Object[] arg = new Object[] { 10 };
- List list = (ArrayList) jt.query("select * from region where rownum<?",
- arg, new RowMapperResultSetExtractor(new RowMapper() {
- public Object mapRow(ResultSet rs, int index)
- throws SQLException {
- Map u = new HashMap();
- u.put("region_id", rs.getString("region_id"));
- u.put("region_name", rs.getString("region_name"));
- return u;
- }
- }));
- Iterator it = list.iterator();
- while (it.hasNext()) {
- Map map = (Map) it.next();
- System.out.println(map.toString());
- }
- }
-
-
-
-
-
- public void getListRowMapper() {
- ApplicationContext context = new FileSystemXmlApplicationContext(
- "src/database_config.xml");
- JdbcTemplate jt = new JdbcTemplate((DataSource) context
- .getBean("oracleDataSourceTest"));
- List list = jt.query(
- "select * from region where rownum<10", new RowMapper() {
- public Object mapRow(ResultSet rs, int index)
- throws SQLException {
- Map u = new HashMap();
- u.put("region_id", rs.getString("region_id"));
- u.put("region_name", rs.getString("region_name"));
- return u;
- }
- });
- Iterator it = list.iterator();
- while (it.hasNext()) {
- Map map = (Map) it.next();
- System.out.println(map.toString());
- }
- }
-
-
-
-
-
-
- public void getListRowCallbackHandler() {
- ApplicationContext context = new FileSystemXmlApplicationContext(
- "src/database_config.xml");
- JdbcTemplate jt = new JdbcTemplate((DataSource) context
- .getBean("oracleDataSourceTest"));
- String sql = "select * from region where region_id>?";
- final List<Map> list=new ArrayList<Map>();
- Object[] params = new Object[] { 0 };
- jt.query(sql, params, new RowCallbackHandler() {
- public void processRow(ResultSet rs) throws SQLException {
- Map u = new HashMap();
- u.put("region_id", rs.getString("region_id"));
- u.put("region_name", rs.getString("region_name"));
- list.add(u);
- }
- });
-
- Iterator it = list.iterator();
- while (it.hasNext()) {
- Map map = (Map) it.next();
- System.out.println(map.toString());
- }
- }
- 转自:http://blog.csdn.net/taozi165/article/details/6578031