2012-12-29 57 views
0

的JTable我想在我的jtable這是在NetBeans IDE中的Java語言來顯示我的SQL數據庫表bk_det的所有5個列,但我只能夠顯示4列,而不是5.什麼問題任何一個可以幫助米張貼代碼如下:無法在我的NetBeans中顯示所有行從SQL

private void btviewbkdetActionPerformed(java.awt.event.ActionEvent evt) { 
if(evt.getSource()==btviewbkdet){ 
    try { 
      DBUtil util = new DBUtil(); 
      Connection con = util.getConnection(); 
      Statement stmt = con.createStatement(); 
      ResultSet rs = stmt.executeQuery("select * from bk_det"); 
      ResultSetMetaData rsmetadata = rs.getMetaData(); 
      int columns = rsmetadata.getColumnCount(); 
      DefaultTableModel dtm = new DefaultTableModel(); 
      Vector columns_name = new Vector(); 
      Vector data_rows = new Vector(); 
      for(int i=1; i< columns; i++){ 
      columns_name.addElement(rsmetadata.getColumnName(i)); 
      } 
      dtm.setColumnIdentifiers(columns_name); 

      while(rs.next()){ 
      data_rows = new Vector(); 
      for(int j=1; j< columns; j++){ 
      data_rows.addElement(rs.getString(j)); 
      } 
      dtm.addRow(data_rows); 
      } 
      tblbnkdet.setModel(dtm); 
     } catch (Exception ex) { 
      JOptionPane.showMessageDialog(null, ex.getMessage()); 
      Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex); 
     }  // TODO add your handling code here: 
} 
    } 

    private void btbkrepviewActionPerformed(java.awt.event.ActionEvent evt) { 
if(evt.getSource()==btviewbkdet){ 
    try { 
      DBUtil util = new DBUtil(); 
      Connection con = util.getConnection(); 
      Statement stmt = con.createStatement(); 
      ResultSet rs = stmt.executeQuery("select * from dbo.bk_rep"); 
      ResultSetMetaData rsmetadata = rs.getMetaData(); 
      int columns = rsmetadata.getColumnCount(); 
      DefaultTableModel dtm = new DefaultTableModel(); 
      Vector columns_name = new Vector(); 
      Vector data_rows = new Vector(); 
      for(int i=1; i< columns; i++){ 
      columns_name.addElement(rsmetadata.getColumnName(i)); 
      } 
      dtm.setColumnIdentifiers(columns_name); 

      while(rs.next()){ 
      data_rows = new Vector(); 
      for(int j=1; j< columns; j++){ 
      data_rows.addElement(rs.getString(j)); 
      } 
      dtm.addRow(data_rows); 
      } 
      tblbkrep.setModel(dtm); 
     } catch (Exception ex) { 
      JOptionPane.showMessageDialog(null, ex.getMessage()); 
      Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex); 
     }  // TODO add your handling code here: 
}  // TODO add your handling code here: 
    } 
+0

不應'對(INT I = 1; I <列;我++) '是'for(int i = 1; i <= columns; i ++)'? –

回答

1

這裏是變化

for(int i=1; i<= columns; i++){ 
    columns_name.addElement(rsmetadata.getColumnName(i)); 
} 
相關問題