来自dbutils库的BeanListHandler无法从数据库结果集创建java类对象(BeanListHandler from dbutils library cannot create java

编程入门 行业动态 更新时间:2024-10-24 17:22:53
来自dbutils库的BeanListHandler无法从数据库结果集创建java类对象(BeanListHandler from dbutils library cannot create java class objects from a database resultset)

我在apache derby数据库中创建了一个名为Product的类和一个具有相同名称(Product)的表。 现在,每当我使用BeanListHandler从数据库中检索行时,我想获取相应的Product对象,但总是会出错。 我几乎到处寻找解决方案,之后我仍然没有看到我的代码出错了。 有人可以告诉我我错在哪里。 我的代码如下所示。

public class Product { private long uniqueId; //Auto increment private String productCode; private String productName; private String productCategory; private boolean available; private double productPrice; private int quantityOnHand; public Product(long uniqueId, String productCode, String productName, String productCategory, boolean available, double productPrice, int quantityOnHand) { this.uniqueId = uniqueId; //Auto increment this.productCode = productCode; this.productName = productName; this.productCategory = productCategory; this.available = available; this.productPrice = productPrice; this.quantityOnHand = quantityOnHand; } @Override public String toString() { return "Product{" + "uniqueId=" + uniqueId + ", productCode=" + productCode + ", productName=" + productName + ", productCategory=" + productCategory + ", available=" + available + ", productPrice=" + productPrice + ", quantityOnHand=" + quantityOnHand + '}'; } public long getUniqueId() { return uniqueId; } public String getProductCode() { return productCode; } public String getProductName() { return productName; } public String getProductCategory() { return productCategory; } public boolean isAvailable() { return available; } public double getProductPrice() { return productPrice; } public int getQuantityOnHand() { return quantityOnHand; } public void setUniqueId(long uniqueId) { this.uniqueId = uniqueId; } public void setProductCode(String productCode) { this.productCode = productCode; } public void setProductName(String productName) { this.productName = productName; } public void setProductCategory(String productCategory) { this.productCategory = productCategory; } public void setAvailable(boolean available) { this.available = available; } public void setProductPrice(double productPrice) { this.productPrice = productPrice; } public void setQuantityOnHand(int quantityOnHand) { this.quantityOnHand = quantityOnHand; } @Override public int hashCode() { int hash = 5; hash = 53 * hash + (int) (this.uniqueId ^ (this.uniqueId >>> 32)); hash = 53 * hash + Objects.hashCode(this.productCode); hash = 53 * hash + Objects.hashCode(this.productName); hash = 53 * hash + Objects.hashCode(this.productCategory); hash = 53 * hash + (this.available ? 1 : 0); hash = 53 * hash + (int) (Double.doubleToLongBits(this.productPrice) ^ (Double.doubleToLongBits(this.productPrice) >>> 32)); hash = 53 * hash + this.quantityOnHand; return hash; } @Override } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Product other = (Product) obj; if (!Objects.equals(this.productCode, other.productCode)) { return false; } if (!Objects.equals(this.productName, other.productName)) { return false; } if (!Objects.equals(this.productCategory, other.productCategory)) { return false; } return true; }

}

下面是检索Product行并将其转换为Product Objects的方法。 我已经导入并创建了建立连接和执行查询所需的每个组件(如私有QueryRunner queryRunner = new QueryRunner(); private static final List EMPTY_PRODUCT_LIST = new ArrayList <>(); etc)

public List<Product> searchAllProducts() { ResultSetHandler<List<Product>> p = new BeanListHandler<>(Product.class); try{ return (List<Product>) queryRunner.query(connection, "SELECT * FROM PRODUCT", p); } catch(SQLException e){ e.printStackTrace(); } finally{ try { DbUtils.close(connection); } catch (SQLException ex) { Logger.getLogger(ProductDatabaseHandler.class.getName()).log(Level.SEVERE, null, ex); } } return EMPTY_PRODUCT_LIST; }

以下是我得到的错误。

Fri Dec 02 20:05:35 EAT 2016 : Apache Derby Network Server - 10.11.1.2 - (1629631) started and ready to accept connections on port 1555 java.sql.SQLException: Cannot create main.java.models.Product: main.java.models.Product Query: SELECT * FROM PRODUCT Parameters: [] at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:392) at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:351) at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:226) at main.java.database.ProductDatabaseHandler.searchAllProducts(ProductDatabaseHandler.java:226)

I have created a class called Product and a table having the same name (Product) in apache derby database. Now i want to get the corresponding Product objects whenever i retrieve the rows from the database using BeanListHandler but always get an error. I have searched almost everywhere for solutions after which i still don't see where i went wrong in my code. Can someone please tell me where i am wrong. my code is shown below.

public class Product { private long uniqueId; //Auto increment private String productCode; private String productName; private String productCategory; private boolean available; private double productPrice; private int quantityOnHand; public Product(long uniqueId, String productCode, String productName, String productCategory, boolean available, double productPrice, int quantityOnHand) { this.uniqueId = uniqueId; //Auto increment this.productCode = productCode; this.productName = productName; this.productCategory = productCategory; this.available = available; this.productPrice = productPrice; this.quantityOnHand = quantityOnHand; } @Override public String toString() { return "Product{" + "uniqueId=" + uniqueId + ", productCode=" + productCode + ", productName=" + productName + ", productCategory=" + productCategory + ", available=" + available + ", productPrice=" + productPrice + ", quantityOnHand=" + quantityOnHand + '}'; } public long getUniqueId() { return uniqueId; } public String getProductCode() { return productCode; } public String getProductName() { return productName; } public String getProductCategory() { return productCategory; } public boolean isAvailable() { return available; } public double getProductPrice() { return productPrice; } public int getQuantityOnHand() { return quantityOnHand; } public void setUniqueId(long uniqueId) { this.uniqueId = uniqueId; } public void setProductCode(String productCode) { this.productCode = productCode; } public void setProductName(String productName) { this.productName = productName; } public void setProductCategory(String productCategory) { this.productCategory = productCategory; } public void setAvailable(boolean available) { this.available = available; } public void setProductPrice(double productPrice) { this.productPrice = productPrice; } public void setQuantityOnHand(int quantityOnHand) { this.quantityOnHand = quantityOnHand; } @Override public int hashCode() { int hash = 5; hash = 53 * hash + (int) (this.uniqueId ^ (this.uniqueId >>> 32)); hash = 53 * hash + Objects.hashCode(this.productCode); hash = 53 * hash + Objects.hashCode(this.productName); hash = 53 * hash + Objects.hashCode(this.productCategory); hash = 53 * hash + (this.available ? 1 : 0); hash = 53 * hash + (int) (Double.doubleToLongBits(this.productPrice) ^ (Double.doubleToLongBits(this.productPrice) >>> 32)); hash = 53 * hash + this.quantityOnHand; return hash; } @Override } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Product other = (Product) obj; if (!Objects.equals(this.productCode, other.productCode)) { return false; } if (!Objects.equals(this.productName, other.productName)) { return false; } if (!Objects.equals(this.productCategory, other.productCategory)) { return false; } return true; }

}

Below is the method to retrieve the Product rows and convert then into Product Objects. I have already imported and created every component needed to establish connection and perform the query(like private QueryRunner queryRunner=new QueryRunner(); private static final List EMPTY_PRODUCT_LIST=new ArrayList<>(); etc)

public List<Product> searchAllProducts() { ResultSetHandler<List<Product>> p = new BeanListHandler<>(Product.class); try{ return (List<Product>) queryRunner.query(connection, "SELECT * FROM PRODUCT", p); } catch(SQLException e){ e.printStackTrace(); } finally{ try { DbUtils.close(connection); } catch (SQLException ex) { Logger.getLogger(ProductDatabaseHandler.class.getName()).log(Level.SEVERE, null, ex); } } return EMPTY_PRODUCT_LIST; }

And below is the error that i get.

Fri Dec 02 20:05:35 EAT 2016 : Apache Derby Network Server - 10.11.1.2 - (1629631) started and ready to accept connections on port 1555 java.sql.SQLException: Cannot create main.java.models.Product: main.java.models.Product Query: SELECT * FROM PRODUCT Parameters: [] at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:392) at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:351) at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:226) at main.java.database.ProductDatabaseHandler.searchAllProducts(ProductDatabaseHandler.java:226)

最满意答案

我认为你需要使用适当的setter方法为你的Product类设置一个no-arg构造函数。

BeanProcessor负责将Resultset转换为bean,下面是创建bean新实例的代码。

protected <T> T newInstance(Class<T> c) throws SQLException { try { return c.newInstance(); } catch (InstantiationException e) { throw new SQLException( "Cannot create " + c.getName() + ": " + e.getMessage()); }

显然,它期望一个无参数的构造函数

I think you need to have a no-arg constructor for your Product class with appropriate setter methods.

BeanProcessor is responsible for converting Resultset into bean and below is the code where it creates the new instance of bean.

protected <T> T newInstance(Class<T> c) throws SQLException { try { return c.newInstance(); } catch (InstantiationException e) { throw new SQLException( "Cannot create " + c.getName() + ": " + e.getMessage()); }

Clearly it expects a no-arg constructor

更多推荐

本文发布于:2023-04-27 15:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327146.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   数据库   dbutils   java   BeanListHandler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!