0

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;
    }
}
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Nike
  • 5
  • 6
  • Hi - for auto mapping, you would usually use frameworks like hibernate to do that for you. Dropwizard offers a hibernate extension too. – pandaadb Mar 11 '16 at 15:14
  • fine, is there any other way to do that without using hibernate because i'm not using hibernate in my project. – Nike Mar 11 '16 at 15:23
  • Hm. Of the top of my head, if your column names are the same as your field names, you can use reflection for this. Or you could use a Hashmap to store your field values and have the column name as a key. – pandaadb Mar 11 '16 at 15:33
  • Thanks for the reply. I'l try to implement it. – Nike Mar 11 '16 at 15:35
  • @pandaadb i wasn't able to get how to implement the methods which you suggested. will you please explain how to implement those methods. – Nike Mar 12 '16 at 11:30

2 Answers2

0

You can add Rosetta as a mapper for your JDBI result sets (it also works for bindings). Have a look at the advanced features to map column names with underscores to snake snake case java names.

Beware that there is no warning message if Rosetta is unable to map a value: any missed property in the target bean will just be empty. I found that my database returned column names in capital letters, therefore the LowerCaseWithUnderscoresStrategy in the example didn't work for me. I created a UpperCaseWithUnderscoresStrategy.

To skip writing getters and setters in ProductsWrapper have a look at Lombok's @Data annotation.

ahus1
  • 5,782
  • 24
  • 42
  • i have checked it by adding dependency to my project. After adding that and running the project i'm getting NoSuchMethodFound exception. – Nike Mar 14 '16 at 11:46
0

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

Manikandan
  • 3,025
  • 2
  • 19
  • 28
  • Thanks a lot Manikandan. I got the result. – Nike Mar 14 '16 at 11:44
  • How can i write dynamic sql query in jbdi. like in my project customer will ask some details like "firstname,lastname,mobile...". so i will read those values into string and my idea was directly append that to sql query like "select firstname,lastname,mobile from customer", but i'm not getting it. – Nike Mar 15 '16 at 06:01