2016-12-02 74 views
1

我已經在apache derby數據庫中創建了一個名爲Product的表和一個具有相同名稱(Product)的表。現在,當我使用BeanListHandler從數據庫中檢索行時,我想獲取相應的Product對象,但始終會出錯。我幾乎在任何地方都搜索瞭解決方案,之後我仍然沒有看到我的代碼中出錯的地方。有人能告訴我我錯在哪裏嗎?我的代碼如下所示。dbutils庫中的BeanListHandler無法從數據庫結果集創建java類對象

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; 
} 

}

下面是檢索產品行,然後轉換成產品對象的方法。我已經導入和創建建立連接並執行查詢所需的每一個組件

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; } 

(如私人QueryRunner queryRunner =新QueryRunner();等; 私有靜態最終名單EMPTY_PRODUCT_LIST =新的ArrayList <>())以下是我得到的錯誤。

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) 
+0

嘗試,包括在你的問題全異常堆棧跟蹤。 –

+0

我現在已經包含了詳細的異常堆棧跟蹤@BryanPendleton – geobudex

回答

3

我認爲你需要一個無參數的構造函數來爲你的Product類提供適當的setter方法。

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()); 

    } 

顯然,這需要一個無參數的構造

+0

謝謝Justin Jose。這解決了我的問題。 – geobudex

+0

它適合我。謝謝 :-) –