关于使用DBUtils查询数据库出现“cannot create query”异常
Posted web攻城狮鸭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于使用DBUtils查询数据库出现“cannot create query”异常相关的知识,希望对你有一定的参考价值。
错误: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”异常的主要内容,如果未能解决你的问题,请参考以下文章