码迷,mamicode.com
首页 > 数据库 > 详细

关于使用DBUtils查询数据库出现“cannot create query”异常

时间:2019-04-09 13:53:04      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:tor   param   方法   错误   语句   vat   设置   mod   查询   

错误:QueryRunner类的query方法的时候出现了“cannor create query   select * from student Parameters: []” 异常

出现错误的代码:

public List<OrderItem> selectAllItem(int orderid) throws SQLException{
QueryRunner r = new QueryRunner(DButils.getDataSoure());
String sql = "select i.id,i.price,i.amount,g.name from orderitem i,goods g where order_id=? and i.goods_id=g.id";
return r.query(sql, new BeanListHandler<OrderItem>(OrderItem.class),orderid);
}

 

原因: r.query(sql, new BeanListHandler<OrderItem>(OrderItem.class),orderid);

使用 new BeanHandler   还有 new BeanListHandler  时 ,(xxx.class)里面的xxx类  (1)里面的属性要有set方法,(2)类里面如果有带参数的改造方法,必须添加一个没有参数的构造方法(2)查询语句sql = "select i.id,i.price,i.amount,g.name from orderitem i,goods g where order_id=? and i.goods_id=g.id";里面的参数保证类里面有同名的构造方法;

 

正确:

package com.czc.Model;

public class OrderItem {
private int id;
private float price;//购买时的价格
private int amount;
private String goodsName;
private Goods goods;
private Order order;

                                             //里面虽然没有name属性

public void setName(String name) {   // 但是要有setName方法
this.goodsName = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}

public OrderItem(float price, int amount, Goods goods, Order order) {   //里面设置了一个有参数的构造方法
super();
this.price = price;
this.amount = amount;
this.goods = goods;
this.order = order;
}
public OrderItem() { //必须再设置一个没有参数的构造方法
super();
}


}

关于使用DBUtils查询数据库出现“cannot create query”异常

标签:tor   param   方法   错误   语句   vat   设置   mod   查询   

原文地址:https://www.cnblogs.com/czc268/p/10676356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!