2016-12-02 12 views
1

Apache라는 더비 데이터베이스에 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; 
} 

}

는 제품 행을 검색하고 제품 개체에 다음 변환 할 수있는 방법이다. 이미 수입과 연결을 설정하고 쿼리를 수행하는 데 필요한 모든 구성 요소 만든 (= 새로운 QueryRunner()를 개인 QueryRunner queryRunner처럼, 개인 정적 최종 목록 EMPTY_PRODUCT_LIST = 새로운 ArrayList를 <>(); 등)

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

시도를 기대하고있다. –

+0

자세한 예외 추적 스택을 포함 시켰습니다. @BryanPendleton – geobudex

답변

3

적절한 설정 메소드를 사용하여 Product 클래스의 인수가없는 생성자가 필요하다고 생각합니다.

BeanProcessorResultset을 빈으로 변환하는 책임이 있으며 아래는 빈의 새 인스턴스를 생성하는 코드입니다.

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

그것은 나를 위해 작동합니다. 감사 :-) –