0

我想显示我的 SQL 数据库表bk_det的所有 5 列,jtable该表是用 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:
    }
4

1 回答 1

1

这里是变化

 for(int i=1; i<= columns; i++){
    columns_name.addElement(rsmetadata.getColumnName(i));
 }
于 2012-12-29T19:20:15.863 回答