JdbcTemplate代码的部分使用

本文通过具体示例展示了如何使用 Spring 框架中的 JdbcTemplate 进行数据库操作,包括更新记录、查询数量、获取描述和列表数据等常见任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 查询代码 in Spring JdbcTemplate
  1. package com.cm.main.dao.impl;   
  2.   
  3. import java.sql.ResultSet;   
  4. import java.sql.SQLException;   
  5. import java.util.List;   
  6.   
  7. import org.springframework.jdbc.core.JdbcTemplate;   
  8. import org.springframework.jdbc.core.RowMapper;   
  9.   
  10. import com.cm.main.dao.AbstractDao;   
  11. import com.cm.main.dao.TestJdbcTemplateDao;   
  12. import com.cm.main.bo.User;   
  13.   
  14. public class TestJdbcTemplateDaoImpl extends AbstractDao implements  
  15.         TestJdbcTemplateDao {   
  16.   
  17.     public void updateDescription(int id, String description) {   
  18.         JdbcTemplate jt = new JdbcTemplate(getDataSource());   
  19.         jt.update("update oa_test_1 set description = ? where id = ?"new Object[] {   
  20.                 description, new Integer(id) });   
  21.     }   
  22.   
  23.     public int getCount() {   
  24.         JdbcTemplate jt = new JdbcTemplate(getDataSource());   
  25.         int count = jt.queryForInt("select count(*) from oa_test_1");   
  26.         return count;   
  27.     }   
  28.   
  29.     public String getDescription() {   
  30.         JdbcTemplate jt = new JdbcTemplate(getDataSource());   
  31.         String name = (String) jt.queryForObject(   
  32.                 "select description from oa_test_1 where id=1", String.class);   
  33.         return name;   
  34.     }   
  35.   
  36.     public List getTestList(int id) {   
  37.         String sql = "select id, description from oa_test_1 where id = ?";   
  38.         RowMapper mapper = new RowMapper() {   
  39.             public Object mapRow(ResultSet rs, int rowNum) throws SQLException {   
  40.                 User user = new User();   
  41.                 user.setUserid(new Integer(rs.getInt("id")));   
  42.                 user.setUsername(rs.getString("description"));   
  43.                 return user;   
  44.             }   
  45.         };   
  46.         JdbcTemplate jt = new JdbcTemplate(this.getDataSource());   
  47.         return jt.query(sql, new Object[] { new Integer(id) }, mapper);   
  48.     }   
  49. }  

查询代码2
public Map getSumCountAllTop48(Date beginDate, Date endDate) {
  Map map = null;
  try {
   map = (Map) this.query(strsqlByDay, new Object[] { beginDate,
     endDate }, new ResultSetExtractor() {
    public Object extractData(ResultSet rs) throws SQLException,
      DataAccessException {
     // TODO Auto-generated method stub
     Map map = new HashMap();
     if (rs != null) {
      long oldId = 0;
      long sum = 0;
      long count = 0;
      while (rs.next()) {
       long newId = rs.getLong("id");
       long value = rs.getLong("value");
       if (oldId != newId) {
        ChannelStatVO vo = new ChannelStatVO();
        vo.getReg().setId(oldId);
        vo.setSum(sum);
        vo.setCount(count);
        map.put(oldId, vo);
        sum = 0;
        count = 0;
        oldId = newId;
       }

       if (count < 48) {
        count++;
        sum = sum + value;
       } else {
        continue;
       }
      }

      ChannelStatVO vo = new ChannelStatVO();
      vo.getReg().setId(oldId);
      vo.setSum(sum);
      vo.setCount(count);
      map.put(oldId, vo);
     }
     return map;
    }
   });

  } catch (Exception e) {
   // TODO: handle exception
   map = new HashMap();
   System.out.println(e.getMessage());
   logger.error("getSumCountAllTop48:" + e.getMessage(), e);
  } finally {
  }
  return map;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值