Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1733 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Pb avec dbunit

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Pb avec dbunit

n°1511910
el muchach​o
Comfortably Numb
Posté le 07-02-2007 à 17:47:01  profilanswer
 

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 :
  1. /**
  2.      * Return a String stored in the given table at a given line number  
  3.      *  
  4.      * @param tableName
  5.      * @param line
  6.      * @param columnTitle
  7.      * @return the String that was stored in given line/column
  8.      * @throws SQLException
  9.      * @throws Exception
  10.      */
  11.     protected String getStringFromTable(final String tableName, final int line, final String columnTitle) throws SQLException, Exception {
  12.         // look into DB tableName
  13.         try {
  14.             IDataSet databaseDataSet = getConnection().createDataSet(); // récupère les tables de la session ouverte
  15.             assertNotNull(databaseDataSet);
  16.             ITable table = databaseDataSet.getTable(tableName); // récupère la table tableName
  17.             assertNotNull(table);
  18.             // get the id from row number 'line' in the column columnTitle
  19.             String res = (String) table.getValue(line, columnTitle); // récupère mon ID dans la colonne columnTitle
  20.             assertNotNull(res);
  21.            
  22.             return res;
  23.         } catch(org.dbunit.dataset.NoSuchTableException ex) {
  24.             System.err.println("dbunit: no table with name " + tableName + " in database." );
  25.             ex.printStackTrace();
  26.             return null;
  27.         } catch(org.dbunit.dataset.NoColumnsFoundException ex) {
  28.             System.err.println("dbunit: table "  + tableName + " has no columns." );
  29.             ex.printStackTrace();
  30.             return null;
  31.         } catch(org.dbunit.dataset.NoSuchColumnException ex) {
  32.             System.err.println("dbunit: no column with name " + columnTitle
  33.                     + " in table " + tableName + "." );
  34.             ex.printStackTrace();
  35.             return null;
  36.         } catch(org.dbunit.dataset.RowOutOfBoundsException ex) {
  37.             System.err.println("dbunit: in table " + tableName + ", row "
  38.                     + line + " should be lower than." +
  39.                     getConnection().createDataSet().getTable(tableName).getRowCount());
  40.             ex.printStackTrace();
  41.             return null;
  42.         } catch(org.dbunit.dataset.DataSetException ex) {
  43.             System.err.println("dbunit: getValue operation failed." );
  44.             ex.printStackTrace();
  45.             return null;
  46.         }
  47.     }


 
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) ?

mood
Publicité
Posté le 07-02-2007 à 17:47:01  profilanswer
 

n°1511933
el muchach​o
Comfortably Numb
Posté le 07-02-2007 à 18:30:55  profilanswer
 

Ok, c'est bon, j'ai résolu mon pb. [:dawao]

 

Le code qui fonctionne est:

Code :
  1. protected String getIdFromTable(final String tableName, final int line, final String columnTitle) throws SQLException, Exception {
  2.         // look into DB tableName
  3.         try {
  4.             ITable table = this.getDataSet().getTable(tableName);
  5.             assertNotNull(table);
  6.             String res = new BigInteger(table.getValue(line, columnTitle).toString());
  7.             assertNotNull(id);
  8.             return res;
  9.         } catch(org.dbunit.dataset.NoSuchTableException ex) {
  10.         ...etc
  11.         }
  12.        ...etc


Message édité par el muchacho le 07-02-2007 à 18:32:57
n°1512002
the real m​oins moins
Posté le 07-02-2007 à 21:26:08  profilanswer
 

donc tu réutilises le dataSet courant plutot que d'en créer un, quoi.
magnifique gestion d'exception, au passage [:xp1700]


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Pb avec dbunit

 

Sujets relatifs
[Fixed!][HTML] (Form) 'action' avec variables en GET ? (Pb) 
Plus de sujets relatifs à : Pb avec dbunit


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)