Preencher JTable

1 resposta
A

Olá sou novo no forum e estou com um problema para inserir dados de um resultset em uma JTable.
A JTable é gerada, as colunas são criadas, as linhas, porém ocorre uma exceção na linha de atribuição da classe
do tipo de coluna… segue fagmento do código e da exeção gerada.

Se alguém pode ajudar  agradeço



@SuppressWarnings(unchecked)

public Class getColumnClass( int column )throws IllegalStateException

{

// assegura que o banco de dados conexão está disponível

if ( !connectedToDatabase )

throw new IllegalStateException( Not Connected to Database );
// determina a classe Java de coluna
      try 
      {
    	    System.out.println(column);
          [color=red]String className = metaData.getColumnClassName( column + 1);[/color]
         //System.out.println(className); :-o
         // retorna objeto Class que representa className
         return Class.forName( className );              
      } // fim do try
      catch ( Exception exception ) 
      {
         exception.printStackTrace();
      } // fim do catch
      
      return Object.class; // se ocorrerem os problemas acima, assume tipo Object
   } // fim do método getColumnClass


com.microsoft.sqlserver.jdbc.SQLServerException: O conjunto de resultados está fechado.

at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)

at com.microsoft.sqlserver.jdbc.SQLServerResultSet.checkClosed(SQLServerResultSet.java:345)

at com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData.checkClosed(SQLServerResultSetMetaData.java:56)

at com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData.getColumnClassName(SQLServerResultSetMetaData.java:235)

at modeloCheckList.ResultSetTableModel.getColumnClass(ResultSetTableModel.java:47)

at javax.swing.JTable.getColumnClass(Unknown Source)

at javax.swing.JTable.getCellRenderer(Unknown Source)

at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)

at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)

at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)

at javax.swing.plaf.ComponentUI.update(Unknown Source)

at javax.swing.JComponent.paintComponent(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JViewport.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at javax.swing.JLayeredPane.paint(Unknown Source)

at javax.swing.JComponent.paintChildren(Unknown Source)

at javax.swing.JComponent.paintToOffscreen(Unknown Source)

at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)

at javax.swing.RepaintManager.paint(Unknown Source)

at javax.swing.JComponent.paint(Unknown Source)

at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)

at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)

at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)

at java.awt.Container.paint(Unknown Source)

at java.awt.Window.paint(Unknown Source)

at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)

at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

1 Resposta

darksteel3000
Ola amigo, tente este exemplo:
public void nome_do_seu_metodo()
    {
        sua_jTable.getColumnModel().getColumn(0).setPreferredWidth(20);
        sua_jTable.getColumnModel().getColumn(1).setPreferredWidth(20);
        sua_jTable.getColumnModel().getColumn(2).setPreferredWidth(40);

        DefaultTableModel modelo = (DefaultTableModel)sua_jTable.getModel();
        modelo.setNumRows(0);
        //ResultSet rs = null;

        ResultSet resul;
        
        try {
            resul = sua_conexao.stmt.executeQuery(" select * from sua_tabela  ");
            while (resul.next()) {
            String campo1 = resul.getString(1);
            String campo2 = resul.getString(2);
            String campo3 = resul.getString(3);

            campo1 = campo1.trim() ;
            campo2 = campo2.trim() ;
            campo3 = campo3.trim() ;

            Vector vetor1 = new Vector();
            vetor1.addElement(campo1);
            vetor1.addElement(campo2);
            vetor1.addElement(campo3);

            modelo.addRow(vetor1);
        }
        } catch (SQLException ex) {
            Logger.getLogger(sua_classe.class.getName()).log(Level.SEVERE, null, ex);
        }
Criado 29 de janeiro de 2011
Ultima resposta 29 de jan. de 2011
Respostas 1
Participantes 2