JdbcTemplate代码的部分使用

本文提供了使用Spring框架下的JdbcTemplate进行数据库操作的具体示例代码,包括更新记录、获取记录数量、查询特定描述及按ID获取列表等常见操作。

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

查询代码 in Spring JdbcTemplate
  1. packagecom.cm.main.dao.impl;
  2. importjava.sql.ResultSet;
  3. importjava.sql.SQLException;
  4. importjava.util.List;
  5. importorg.springframework.jdbc.core.JdbcTemplate;
  6. importorg.springframework.jdbc.core.RowMapper;
  7. importcom.cm.main.dao.AbstractDao;
  8. importcom.cm.main.dao.TestJdbcTemplateDao;
  9. importcom.cm.main.bo.User;
  10. publicclassTestJdbcTemplateDaoImplextendsAbstractDaoimplements
  11. TestJdbcTemplateDao{
  12. publicvoidupdateDescription(intid,Stringdescription){
  13. JdbcTemplatejt=newJdbcTemplate(getDataSource());
  14. jt.update("updateoa_test_1setdescription=?whereid=?",newObject[]{
  15. description,newInteger(id)});
  16. }
  17. publicintgetCount(){
  18. JdbcTemplatejt=newJdbcTemplate(getDataSource());
  19. intcount=jt.queryForInt("selectcount(*)fromoa_test_1");
  20. returncount;
  21. }
  22. publicStringgetDescription(){
  23. JdbcTemplatejt=newJdbcTemplate(getDataSource());
  24. Stringname=(String)jt.queryForObject(
  25. "selectdescriptionfromoa_test_1whereid=1",String.class);
  26. returnname;
  27. }
  28. publicListgetTestList(intid){
  29. Stringsql="selectid,descriptionfromoa_test_1whereid=?";
  30. RowMappermapper=newRowMapper(){
  31. publicObjectmapRow(ResultSetrs,introwNum)throwsSQLException{
  32. Useruser=newUser();
  33. user.setUserid(newInteger(rs.getInt("id")));
  34. user.setUsername(rs.getString("description"));
  35. returnuser;
  36. }
  37. };
  38. JdbcTemplatejt=newJdbcTemplate(this.getDataSource());
  39. returnjt.query(sql,newObject[]{newInteger(id)},mapper);
  40. }
  41. }

查询代码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、付费专栏及课程。

余额充值