最近在学着做一个jsp+servlet的商城项目,在做的过程中使用到了一个Order实体类用于对应我数据库中的orders表,可是在查询正确的情况下报了这个错误 java.sql.SQLException: Cannot set uid: incompatible types. Query: select * from orders where uid = ? Parameters: [f164918b-ffda-4baf-809a-158203477228],思考了很久一直无法解决,Order实体类如下:
public class Order { private String oid; private String ordertime; private double total; private int state; private String address; private String name; private String telephone; private User uid; List
orderItems = new ArrayList (); ---get、set方法-----
以下是我的orders表
以下是我的dao查询语句
public List findAllOrders(String uid) {
// 订单查询
QueryRunner runner = new QueryRunner(JDBCUtils_V3.getDataSource());
String sql = "select * from orders where uid = ?";
List query = null;
try {
query = runner.query(sql, new BeanListHandler(Order.class),uid);
} catch (SQLException e) {
e.printStackTrace();
}
return query;
}
写完之后,我尝试着跑了起来,出现以下错误
java.sql.SQLException: Cannot set uid: incompatible types. Query: select * from orders where uid = ? Parameters: [f164918b-ffda-4baf-809a-158203477228]
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:320)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:349)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:287)
at com.iteason.dao.ProductDao.findAllOrders(ProductDao.java:108)
at com.iteason.service.ProductService.findAllOrders(ProductService.java:107)
at com.iteason.web.servlet.MyOrdersServlet.doGet(MyOrdersServlet.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
很明显的错误,我把它给圈红了,提示的是uid这个类型出错,无法把查询的结果存储在BeanListHandler结果集里,接下来我又进行了调试,一开始我以为我的uid是无效的,orders表里找不到数据,所以我在orders表里c了一个uid去作为参数,还是一样的结果,那么说明我的查询是有结果的,是封不进结果集的原因,封装类型出错。
我回忆起前几天写过的Product实体类,里面也有封装着一个Category实体类,如图
public class Product {
private String pid;
private String pname;
private double market_price;
private double shop_price;
private String pimage;
private String pdate;
private int is_hot;
private String pdesc;
private int pflag;
private Category category;
--------get、set方法-----
我将dao层用于查询List
public List getProductByCategory(String cid, int index, int currentCount) throws SQLException {
// 获得分页的数据
QueryRunner runner = new QueryRunner(JDBCUtils_V3.getDataSource());
String sql = "select * from product where cid = ? limit ?,?";
List list = runner.query(sql, new BeanListHandler(Product.class),cid,index,currentCount);
for(Product product:list){
System.out.println(product.getCategory());
}
return list;
}
运行结果如图所示
这说明我从数据库查询到的数据也并没有完全封装到Product实体类中,因为数据库中查出来的category字段是varchar类型的,而Product实体类中的private Category category类型的,所以打印这个Product类中的category,结果都是null,无法封装,但是在正常情况下不会报错,而是不封装这个部分。
所以接下来我的解决方法是,在Order实体类中将private User uid改成private User user,多添加一行private String uid(对应名称封装的原则),顺利完成了封装查询,不报错!
问题虽然解决了,可是我还是不知道为什么在查询List