Bonjour,
Quelqu'un s'y connait en dbunit ?
Je n'arrive pas à comprendre comment on est censé l'utiliser pour récupérer une ligne dans la base de données.
J'ai écrit une petite classe qui est censée me permettre de récupérer une chaîne à la ligne xxx de la colonne columnTitle dans la table tableName.
Code :
- /**
- * Return a String stored in the given table at a given line number
- *
- * @param tableName
- * @param line
- * @param columnTitle
- * @return the String that was stored in given line/column
- * @throws SQLException
- * @throws Exception
- */
- protected String getStringFromTable(final String tableName, final int line, final String columnTitle) throws SQLException, Exception {
- // look into DB tableName
- try {
- IDataSet databaseDataSet = getConnection().createDataSet(); // récupère les tables de la session ouverte
- assertNotNull(databaseDataSet);
- ITable table = databaseDataSet.getTable(tableName); // récupère la table tableName
- assertNotNull(table);
- // get the id from row number 'line' in the column columnTitle
- String res = (String) table.getValue(line, columnTitle); // récupère mon ID dans la colonne columnTitle
- assertNotNull(res);
-
- return res;
- } catch(org.dbunit.dataset.NoSuchTableException ex) {
- System.err.println("dbunit: no table with name " + tableName + " in database." );
- ex.printStackTrace();
- return null;
- } catch(org.dbunit.dataset.NoColumnsFoundException ex) {
- System.err.println("dbunit: table " + tableName + " has no columns." );
- ex.printStackTrace();
- return null;
- } catch(org.dbunit.dataset.NoSuchColumnException ex) {
- System.err.println("dbunit: no column with name " + columnTitle
- + " in table " + tableName + "." );
- ex.printStackTrace();
- return null;
- } catch(org.dbunit.dataset.RowOutOfBoundsException ex) {
- System.err.println("dbunit: in table " + tableName + ", row "
- + line + " should be lower than." +
- getConnection().createDataSet().getTable(tableName).getRowCount());
- ex.printStackTrace();
- return null;
- } catch(org.dbunit.dataset.DataSetException ex) {
- System.err.println("dbunit: getValue operation failed." );
- ex.printStackTrace();
- return null;
- }
- }
|
Manque de bol, ce con de databaseDataSet.getTable(tableName) fait un close.connection(), ce qui fait que le getValue suivant génère une exception JDBC renvoyée par le moteur de base de donnée H2:
org.dbunit.dataset.DataSetException: org.h2.jdbc.JdbcSQLException: The object is already closed [90007-28]
Ce comportement me parait complètement idiot. Quelqu'un a sait comment s'y prendre, à part faire du SQL (ça sera mon dernier recours) ?