0

The other day I've found out about DbUtils, so I jumped in a project to test it out. I've created a class called Tag that looks like this.

package io.sharpsoft.hooke.rest.models;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Tag implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -8040690224162074105L;
    private int id;
    private String name;

    public Tag() {}

    public Tag(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "ID: " + this.getId() + ", Name: " + this.getName();
    }
}

I have a table called TAGS in my database and all I wanted to do was to create a function that fetches all these TAGS. This is the function that I wrote:

public static List<Tag> getAllTags() throws SQLException {
        Connection conn = null;
        QueryRunner runner = new QueryRunner();
        try {
            DbUtils.loadDriver(Constants.JDBC_DRIVER);
            conn = DriverManager.getConnection(ConfUtils.getDbUrl(), ConfUtils.getDbUser(), ConfUtils.getDbPass());

            ResultSetHandler<List<Tag>> resultHandler = new BeanListHandler<>(Tag.class);

            List<Tag> tags = runner.query(conn, "select * from TAGS", resultHandler);
            System.out.println("Tags");
            for (Tag tag : tags) {
                System.out.println("ID: " + tag.getId() + ", Name: " + tag.getName());
            }

            return tags;
        } finally {
            DbUtils.close(conn);
        }
    }

The problem is that the BeanListHandler does not map the result set entries into my object correctly. This is what I get:

ID: 0, Name: null
ID: 0, Name: null
ID: 0, Name: null
ID: 0, Name: null
ID: 0, Name: null
ID: 0, Name: null
ID: 0, Name: null
ID: 0, Name: null
ID: 0, Name: null

Any help would be very much appreciated! Thank you!

Cosmin Stoian
  • 757
  • 10
  • 15

0 Answers0