2013-02-27 9 views
11

La radice del mio problema è che ho un metodo che gestisce le query JDBC e rilascia tutte le connessioni dopo la query. Un "ResultSet" viene passato al metodo di chiamata.ClassCastException: java.lang.Object non può essere lanciato su java.lang.Integer

Ho scoperto che non posso semplicemente passare il ResultSet al metodo di chiamata, perché con il ResultSet chiuso, quindi qualsiasi tentativo di utilizzarlo ottiene un errore Già chiuso.

Quindi prima di chiudere le risorse, eseguo il loop del ResultSet e lo memorizzo in ArrayList.

Poiché il metodo gestisce qualsiasi query, non so quale tipo di tipi vengono restituiti. Pertanto ArrayList memorizza i messaggi generici.

Questo funziona tranne un campo in una tabella .. in un database, che è un campo Integer [].

Quello che ottengo è un oggetto JDBC4Array, e ho molto tempo per farlo in un Integer [] per l'archiviazione in ArrayList. Ho bisogno che sia un intero [].

Questo è quello che ho adesso ... È dopo un sacco di frustrato banjaxxing.

Mentre scorrendo il ResultSet, prima che la connessione è chiusa, faccio questo:

  // For every row in the ResultSet 
      while (rs.next()) { 
       // Initialize a ITILRow for this ResultSet row 
       ITILRow row = new ITILRow(); 

       // For each column in this row, add that object to the ITILRow 
       for (int colNum=1; colNum<=numCols; colNum++) { 
        Object o = rs.getObject(colNum); 

        // JDBC4Array is a real pain in the butt 
        ArrayList<Integer> tmpList = new ArrayList<Integer>(); 
        if (o != null) { 
         if (o.getClass().getSimpleName().endsWith("Array")) { 
          // At least at this time, these Arrays are all Integer[] 
          Array a = (Array) o; 
          Integer[] ints = (Integer[]) a.getArray(); 
          for (Integer i : ints) { 
           tmpList.add(i); 
          } 
          o = tmpList; 
         } 
        } 

        row.add(o); 
       } 

       // Add the ITILRow to allRows 
       allRows.add(row); 
      } 

Poi, nel metodo chiamante ...

for (ITILRow row : allRows) { 
     ... 
     ArrayList comps = (ArrayList) row.getObject(5); 
     Integer[] argh = (Integer[]) ((ArrayList<Integer>) comps).toArray(); 

     ... 
    } 

E ottengo:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; 

L'aiuto sarebbe apprezzato. Ho ottenuto il mio cervello legato a questo nodo.

Grazie,

risposta

32

List#toArray() restituisce un array Object. Utilizzare invece List#toArray(T[]).

Integer[] arg = (Integer[]) comps.toArray(new Integer[comps.size()]); 
+2

Holy smokes sei stato veloce. Grazie. Sìì! Sto andando avanti di nuovo !! Grazie, grazie, grazie. – Lurk21

+2

Penso che il parametro dovrebbe essere 'new Integer [0]' - non hai effettivamente bisogno di spazio nell'oggetto passato, è solo usato per ottenere le informazioni sulla classe, ma devi istanziarlo. – AgilePro

+0

@AgilePro: se l'array passato è sufficientemente grande (almeno gli elementi comps.size()), l'array passato verrà riempito e restituito dal metodo toArray. Rendere sufficientemente grande l'array impedirà la creazione di un array addizionale dello stesso tipo. – jarnbjo

Problemi correlati