spring的jdbcTemplate查询不到数据
2010-02-05 00:11
495 查看
搞了一个晚上,未果。
测试代码如下:
主程序代码
很奇怪的是使用getAllGoods的时候能够将结果正确的返回,而是用id来查询的时候却根本查不到值。难道是bug?使用sql在mysql里面查询是正常的,bean配置都正确。
测试代码如下:
主程序代码
package org.xiziyin.shop.dal.dao.jdbcimpl; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; import org.xiziyin.shop.dal.dao.GoodsDAO; import org.xiziyin.shop.dal.dao.exception.DAOException; import org.xiziyin.shop.dal.dataobject.Goods; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * Class JdbcTemplateGoodsDAO ... * * @author <a href="mailto:czy88840616@163.com">czy</a> * Created on 2010-2-4 22:14:41 */ public class JdbcTemplateGoodsDAO implements GoodsDAO { private JdbcTemplate jdbcTemplate; /** * Method getJdbcTemplate returns the jdbcTemplate of this JdbcTemplateGoodsDAO object. * * @return the jdbcTemplate (type JdbcTemplate) of this JdbcTemplateGoodsDAO object. */ public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } /** * Method setJdbcTemplate sets the jdbcTemplate of this JdbcTemplateGoodsDAO object. * * @param jdbcTemplate the jdbcTemplate of this JdbcTemplateGoodsDAO object. */ public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } /** * Method getGoodsById ... * * @param id of type Long * * @return Goods * * @throws DAOException when */ @Override public Goods getGoodsById(Long id) throws DAOException { final Goods goods = new Goods(); this.jdbcTemplate.query("select * from goods where id = ?", new Object[]{id}, new RowCallbackHandler(){ /** @see org.springframework.jdbc.core.RowCallbackHandler#processRow(ResultSet) */ @Override public void processRow(ResultSet rs) throws SQLException { if(rs.next()) { goods.setId(rs.getLong(1)); goods.setName(rs.getString(2)); goods.setQuantity(rs.getInt(3)); goods.setPrice(rs.getDouble(4)); goods.setDescribe(rs.getString(5)); } } }); // Goods goods = new Goods(); // goods = (Goods) this.jdbcTemplate.queryForObject("select * from goods where id=?", new Object[]{id}, new RowMapper() { // // @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { // Goods g = new Goods(); // if (rs.next()) { // g.setId(rs.getLong(1)); // g.setName(rs.getString(2)); // g.setQuantity(rs.getInt(3)); // g.setPrice(rs.getDouble(4)); // g.setDescribe(rs.getString(5)); // } // return g; // } // }); return goods; } /** * Method getGoodsList returns the goodsList of this JdbcTemplateGoodsDAO object. * * @return the goodsList (type List<Goods>) of this JdbcTemplateGoodsDAO object. * * @throws DAOException when */ @Override public List<Goods> getGoodsList() throws DAOException { final List<Goods> list = new ArrayList<Goods>(); this.jdbcTemplate.query("select * from goods", new Object[]{}, new RowCallbackHandler() { /** @see org.springframework.jdbc.core.RowCallbackHandler#processRow(ResultSet) */ @Override public void processRow(ResultSet rs) throws SQLException { while (rs.next()) { Goods goods = new Goods(); goods.setId(rs.getLong(1)); goods.setName(rs.getString(2)); goods.setQuantity(rs.getInt(3)); goods.setPrice(rs.getDouble(4)); goods.setDescribe(rs.getString(5)); list.add(goods); } } }); return list; } }
很奇怪的是使用getAllGoods的时候能够将结果正确的返回,而是用id来查询的时候却根本查不到值。难道是bug?使用sql在mysql里面查询是正常的,bean配置都正确。
相关文章推荐
- Spring中使用JdbcTemplate的queryForObject方法,当查不到数据时会抛出如下异常:
- Spring ------查询数据之后转为对象 jdbcTemplate1
- Spring jdbcTemplate查不到数据时抛出EmptyResultDataAccessException
- Spring jdbcTemplate查不到数据时抛出EmptyResultDataAccessException
- Spring-JDBC:在使用JdbcTemplate进行条件查询时没有返回数据,但是sql直接在数据库中执行是有数据返回的。
- Spring入门之JdbcTemplate【二】查询数据库表数据
- Spring中使用JdbcTemplate的queryForObject方法,当查不到数据时会抛出如下异常:
- 以中文为查询条件查询mysql数据库时有数据却查不到数据或者中文乱码的问题
- GreenDao查询,有时会查不到数据,解决方法。
- JdbcTemplate查询数据 三种callback之间的区别(ResultSetExtractor,RowMapperRowCallbackHandler)
- 利用Spring MVC+Mybatis实现Mysql分页数据查询的过程详解
- spring boot + JdbcTemplate 使用druid数据源
- JdbcTemplate查询数据 三种callback之间的区别
- java jdbc连接mysql 查询数据查找不到
- java 用properties文件配置spring数据源,用spring的JdbcTemplate的queryForList查数据
- Hibernate 查询不到数据
- mybatis执行查询语句查询不到结果,控制台输出sql语句复制到数据库中能查到数据
- java 用properties文件配置spring数据源,用spring的JdbcTemplate的queryForList查数据
- spring多数据源的处理 mybatis实现跨库查询 实现Myibatis动态sql跨数据库的处理 Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样
- 【Android问题记录】为什么在终端直接查询数据库可以查到数据但是用代码总是查不到