Search code examples
mappingresultsetdropwizardjdbi

ResultSet mapping to object dynamically in dropwizard


I was trying to map ResultSet data to an object and returning it. Here is how i'm mapping data to an object. Now i'm having only 7 columns in resultset so this is working fine but what if i'm having 20 or 30 columns. How can i map dynamically those columns.

public class ProductsWrapperMapper implements ResultSetMapper<ProductsWrapper> {
    public ProductsWrapper map(int i, ResultSet resultSet,
            StatementContext statementContext) throws SQLException {
        ProductsWrapper product = new ProductsWrapper();    

        if ((isColumnPresent(resultSet,"a_productid"))) {
            product.setId(resultSet.getInt("a_productid"));

        }
        if ((isColumnPresent(resultSet,"a_productname"))) {
            product.setProductName(resultSet.getString("a_productname"));
        }
        if ((isColumnPresent(resultSet,"a_productlink"))) {
            product.setLink(resultSet.getString("a_productlink"));
        }
        if ((isColumnPresent(resultSet,"a_productimagelink"))) {
            product.setImageLink(resultSet.getString("a_productimagelink"));
        }
        if ((isColumnPresent(resultSet,"a_websiteid"))) {
            product.setWebsiteId(resultSet.getInt("a_websiteid"));
        }
        if ((isColumnPresent(resultSet,"a_productidentification"))) {
            product.setProductIdentification(resultSet
                    .getString("a_productidentification"));
        }
        if ((isColumnPresent(resultSet,"a_adddate"))) {
            product.setAddDate(resultSet.getString("a_adddate"));
        }

        return product;
    }


    public boolean isColumnPresent(ResultSet resultSet,String column) {
        try {
            @SuppressWarnings("unused")
            int index = resultSet.findColumn(column);
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            return false;
        }

    }
}

Below one is my class which i was returning the object from mapper class above.

@JsonInclude(Include.NON_NULL)
public class ProductsWrapper {

    private int id;
    private String productName;
    private String link;
    private String imageLink;
    private int websiteId;
    private String productIdentification;
    private String addDate;

    int getWebsiteId() {
        return websiteId;
    }

    public void setWebsiteId(int websiteId) {
        this.websiteId = websiteId;
    }

    public String getProductIdentification() {
        return productIdentification;
    }

    public void setProductIdentification(String productIdentification) {
        this.productIdentification = productIdentification;
    }

    public String getAddDate() {
        return addDate;
    }

    public void setAddDate(String addDate) {
        this.addDate = addDate;
    }`enter code here`

    public ProductsWrapper(int id) {
        this.setId(id);
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getImageLink() {
        return imageLink;
    }

    public void setImageLink(String imageLink) {
        this.imageLink = imageLink;
    }

    public int getId() {
        return id;
    }

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

Solution

  • You can also try Jdbi-folder. It automatically takes care of dynamic bynding and also it provides one to many mapping relationship.