3

I have a mysql table like this:

CREATE TABLE `sezione_menu` (
 `id_sezione_menu` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `nome` varchar(256) NOT NULL DEFAULT '',
 `ordine` int(11) DEFAULT NULL,
    PRIMARY KEY (`id_sezione_menu`)
 )ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

I use apache dbutils to query my database, with these methods:

public static List<SezioneMenu> getSezioniMenu() {
    String sql = "SELECT * FROM sezione_menu";  
    try {
        QueryRunner qr = new QueryRunner(createDataSource());
        ResultSetHandler rsh = new BeanListHandler(SezioneMenu.class);
        List<SezioneMenu> sezioni = (List<SezioneMenu>)qr.query(sql, rsh);
        return sezioni;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;  
}

private static DataSource createDataSource() {
        BasicDataSource d = new BasicDataSource();
        d.setDriverClassName(DRIVER);
        d.setUsername(USERNAME);
        d.setPassword(PASSWORD);
        d.setUrl(DB_URL);
        return d;
    }

Now, if i run my application, it doesn't throw exception, but some fields (not all!) of my java bean SezioneMenu are empty (integer field equals zero and string field equals empty string). This happen also with other tables and beans. I used this method in the past in another system configuration without problems.

erhun
  • 3,549
  • 2
  • 35
  • 44
nap.gab
  • 451
  • 4
  • 19
  • 1
    When you write your queries and beans, do you ensure that either the fields in your bean are named the same as your database columns or rename your columns to match what is in the bean? You didn't say which field(s) are not filled out, but I suspect it might be the first one, which you may have, for example, named in camelCase in the Java code while using underscores in the SQL statement. – Frank Riccobono Dec 21 '14 at 13:38
  • You should share SezioneMenu.class java code to check your field name is correct or not? – erhun May 10 '15 at 19:36

2 Answers2

5

You can fix it in two ways:

As per dbutils doc,

  1. Alias the column names in the SQL so they match the Java names: select social_sec# as socialSecurityNumber from person
  2. Subclass BeanProcessor and override the mapColumnsToProperties() method to strip out the offending characters.

If you are keeping a class like this

public class SezioneMenuBean implements Serializable {

    private int idSezioneMenu;

    private String nome;

    private int ordine;

    public SezioneMenuBean() {
    }

    // Getters and setters for bean values

}

As per first solution write your queries something like this SELECT id_sezione_menu AS idSezioneMenu, name, ordine FROM sezione_menu.

Or

Based on second solution you can use GenerousBeanProcessor which is a subclass of BeanProcessor it ignores underscore & case sensitivity from column name. You don't have to implement your own custom BeanProcessor

GenerousBeanProcessor is available since version 1.6 of commons-dbutils.

Usage:

// TODO initialize
QueryRunner queryRunner = null;

ResultSetHandler<List<SezioneMenuBean>> resultSetHandler =
                new BeanListHandler<SezioneMenuBean>(SezioneMenuBean.class, new BasicRowProcessor(new GenerousBeanProcessor()));

// best practice is specifying only required columns in the query
// SELECT id_sezione_menu, name, ordine FROM sezione_menu
final List<SezioneMenuBean> sezioneMenuBeans = queryRunner.query("SELECT * FROM sezione_menu", resultSetHandler);

for (SezioneMenuBean sezioneMenuBean : sezioneMenuBeans) {
    System.out.println(sezioneMenuBean.getIdSezioneMenu());
}
Visruth
  • 3,430
  • 35
  • 48
0

I faced the same issue of BeanHandler/BeanHandlerList returning null or 0 for database columns.

As mentioned by @aelfric5578 in the comment, I have updated the Bean class with same names as Database, DBUtils returned values correctly.

Having BeanClass defined like this will solve your problem.


    public class SezioneMenuBean{

    int id_sezione_menu;
    String nome;
    int ordine;

    public SezioneMenuBean(){
    }

    // Getters and setters for bean values

    }

parishodak
  • 4,506
  • 4
  • 34
  • 48