无法从sql显示我的netbeans jtable中的所有行

时间:2012-12-29 19:15:37

标签: java swing jdbc jtable actionlistener

我想在我的bk_det中显示我的SQL数据库表jtable的所有5列,这些列在java语言的netbeans ide中,但我只能显示4列而不是5列。任何人都可以帮助发布以下代码:

 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:
    }

1 个答案:

答案 0 :(得分:1)

这是更改

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