雪梨篮儿 2024-01-31 10:04 采纳率: 75.9%
浏览 0
已结题

BasicDAO+druid 运行异常

您好,goods商品表是mysql数据库中的一张商品信息表,GoodsDAO继承BasicDAO,末段代码为运行时报错内容,其中druid配置信息等如下,求解,非常3Q


```java
#key=value
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/lhc_db03?rewriteBatchedStatements=true
username=root
password=lhc
#initial connection Size
initialSize=10
#min idle connecton size
minIdle=5
#max active connection size
maxActive=50
#max wait time (5000 mil seconds)
maxWait=5000



```java
package dao_.dao;

import dao_.utils.JDBCUtilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BasicDAO<T> {
    private QueryRunner qr = new QueryRunner();
    public int update(String sql,Object...parameters){
        Connection connection = null;
        try {

            connection = JDBCUtilsByDruid.getConnection();
            int update = qr.update(connection,sql,parameters);
            return update;
        } catch (SQLException e) {
           throw new RuntimeException(e);
        } finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }
    }

    public List<T> queryMulti(String sql,Class<T> clazz,Object...parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return qr.query(connection,sql,new BeanListHandler<T>(clazz),parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }
    }

    public T querySingle(String sql,Class<T> clazz,Object...parameters){
        Connection connection = null;
        try {
            connection=JDBCUtilsByDruid.getConnection();
            return qr.query(connection,sql,new BeanHandler<T>(clazz),parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }
    }

    public Object queryScalar(String sql,Object...parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return qr.query(connection,sql,new ScalarHandler(),parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }

    }
}

package dao_.dao;
public class GoodsDAO extends BasicDAO{


}



```java
package dao_.domain;

public class Goods {
    private int id;
    private String goods_name;
    private double price;

    public Goods(int id, String goods_name, double price) {
        this.id = id;
        this.goods_name = goods_name;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", goods_name='" + goods_name + '\'' +
                ", price=" + price +
                '}';
    }
}



package dao_.domain;

public class Goods {
    private int id;
    private String goods_name;
    private double price;

    public Goods(int id, String goods_name, double price) {
        this.id = id;
        this.goods_name = goods_name;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", goods_name='" + goods_name + '\'' +
                ", price=" + price +
                '}';
    }
}




```java
一月 31, 2024 9:50:27 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
Exception in thread "main" java.lang.RuntimeException: java.sql.SQLException: Cannot create dao_.domain.Goods: dao_.domain.Goods Query: select * from goods where price >= ? Parameters: [2000]
    at dao_.dao.BasicDAO.queryMulti(BasicDAO.java:35)
    at dao_.test.TestGoodsDAO.main(TestGoodsDAO.java:11)
Caused by: java.sql.SQLException: Cannot create dao_.domain.Goods: dao_.domain.Goods Query: select * from goods where price >= ? Parameters: [2000]
    at org.apache.commons.dbutils.QueryRunner.rethrow(QueryRunner.java:542)
    at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:399)
    at dao_.dao.BasicDAO.queryMulti(BasicDAO.java:33)
    ... 1 more

Process finished with exit code 1


  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-01-31 10:06
    关注

    引自免费微信小程序:皆我百晓生

    问题分析: 根据报错信息,异常发生在BasicDAOqueryMulti()方法中,具体是在qr.query()方法执行时出错。报错信息为:java.sql.SQLException: Cannot create dao_.domain.Goods: dao_.domain.Goods Query: select * from goods where price >= ? Parameters: [2000]

    解决方案: 根据异常信息,无法创建Goods对象。这可能是由于Goods类缺少无参构造函数。您可以添加一个无参构造函数来解决此问题,如下所示:

    public Goods() {}
    

    请注意,当使用BeanHandlerBeanListHandler处理器时,需要保证被处理的类具有无参构造函数。

    尝试添加无参构造函数后,重新运行您的代码,看是否仍然报错。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月31日
  • 已采纳回答 1月31日
  • 修改了问题 1月31日
  • 创建了问题 1月31日

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振