zoukankan      html  css  js  c++  java
  • 今日总结

    2020年11月20日:

    关于jdbctemplate剩余的查询方式

    JdbcTemplate查询-queryForInt返回一个int整数

    org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

    API介绍

    public int queryForInt(String sql)
    执行查询语句,返回一个int类型的值。

    使用步骤

    1. 创建JdbcTemplate对象
    2. 编写查询的SQL语句
    3. 使用JdbcTemplate对象的queryForInt方法
    4. 输出结果

    案例代码

    // queryForInt返回一个整数
    public static void test01() throws Exception {
       // String sql = "SELECT COUNT(*) FROM product;";
       String sql = "SELECT pid FROM product WHERE price=18888;";
       JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
       int forInt = jdbcTemplate.queryForInt(sql);
       System.out.println(forInt);
    }

    JdbcTemplate查询-queryForMap返回一个Map集合

    API介绍

    public Map<String, Object> queryForMap(String sql)
    执行查询语句,将一条记录放到一个Map中。

    使用步骤

    1. 创建JdbcTemplate对象
    2. 编写查询的SQL语句
    3. 使用JdbcTemplate对象的queryForMap方法
    4. 处理结果
    public static void test04() throws Exception {
       String sql = "SELECT * FROM product WHERE pid=?;";
       JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
       Map<String, Object> map = jdbcTemplate.queryForMap(sql, 6);
       System.out.println(map);
    }

    JdbcTemplate查询-queryForList返回一个List集合

    org.springframework.jdbc.core.JdbcTemplate类方便执行SQL语句

    API介绍

    public List<Map<String, Object>> queryForList(String sql)
    执行查询语句,返回一个List集合,List中存放的是Map类型的数据。

    使用步骤

    1. 创建JdbcTemplate对象
    2. 编写查询的SQL语句
    3. 使用JdbcTemplate对象的queryForList方法
    4. 处理结果
    public static void test05() throws Exception {
       String sql = "SELECT * FROM product WHERE pid<?;";
       JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
       List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, 8);
       for (Map<String, Object> map : list) {
          System.out.println(map);
       }
    }
  • 相关阅读:
    原生AJAX基础讲解及兼容处理
    JS子元素oumouseover触发父元素onmouseout
    IE6常见bug
    让IE6支持position:fixed的方法,CSS expression与JavaScript eval讲解
    Alpha通道
    网络游戏开发前的pixel像素画习作
    网络游戏开发其一(游戏美工)
    周内琐记
    地图重置与各项绘图优化
    四足机器人搭建尝试
  • 原文地址:https://www.cnblogs.com/yitiaokuailedexiaojingyu/p/14126361.html
Copyright © 2011-2022 走看看