ResultSet的结果不会出现在jtable中

时间:2013-11-26 04:03:51

标签: java swing jtable

尽管它显示ResultSet中有结果(行),但在将这些行带入JTable时会出现问题。当我运行程序时,从MS Access数据库检索的结果不会出现在JTable上。请帮忙 ! 我的代码如下!

final JTable tb1 = new JTable();

    retcus.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            frame3.setVisible(true);
            frame1.setEnabled(true);
            frame.setVisible(false);
            frame2.setVisible(false);

            try{

            Connection con;
            Statement stmt;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String cn = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=E:/userlogin.mdb";
            con = DriverManager.getConnection(cn,"","");
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String sql = "select * from userlogin.customers";
            ResultSet rs = stmt.executeQuery(sql); 


            Vector rows=new Vector();

            while(rs.next()){

            Vector one_row=new Vector();

            one_row.add(rs.getString("ID"));
            one_row.add(rs.getString("fname"));
            one_row.add(rs.getString("sname"));
            one_row.add(rs.getString("address"));
            one_row.add(rs.getString("city"));
            one_row.add(rs.getString("contact"));

            rows.add(one_row);

            }

            DefaultTableModel model=new DefaultTableModel();

            Iterator i=rows.iterator();
            int count=0;
            while(i.hasNext()){
            model.insertRow(count,(Vector)i.next());
            count++;
            }   

            tb1.setModel(model);

            int size = 0;
                try {
                    rs.last();
                    size = rs.getRow();
                    rs.beforeFirst();
                    }

            catch(Exception ex) {

            }
                JOptionPane.showOptionDialog(null,
                size,
                "Error !",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                new String[]{"Ok", "Cancel"}, // this is the array
                "default");

            if (rs==null)
            {
                JOptionPane.showOptionDialog(null,
                "Resultset null !",
                "Error !",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                new String[]{"Ok", "Cancel"}, // this is the array
                "default");
            }
            rs.close();
            stmt.close();  


            }

        catch (HeadlessException err) {
           JOptionPane.showOptionDialog(null,
                            "HeadlessException !",
                            "Error !",
                            JOptionPane.OK_CANCEL_OPTION,
                            JOptionPane.INFORMATION_MESSAGE,
                            null,
                            new String[]{"Ok", "Cancel"}, // this is the array
                            "default");
        }   

        catch (ClassNotFoundException err) {
        JOptionPane.showOptionDialog(null,
                "ClassNotFoundException !",
                "Error !",
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                new String[]{"Ok", "Cancel"}, // this is the array
                "default");

        } 
         catch (SQLException err) {
            JOptionPane.showOptionDialog(null,
                    err.getMessage(),
                    "Error !",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    new String[]{"Ok", "Cancel"}, // this is the array
                    "default");

        }

       }
    });



    panel3.add(tb1);
    size = new Dimension(600, 150);
    tb1.setBounds(100 + insets3.left, 140 + insets3.top, size.width, size.height);

1 个答案:

答案 0 :(得分:2)

panel3.add(tb1);
size = new Dimension(600, 150);
tb1.setBounds(100 + insets3.left, 140 + insets3.top, size.width, size.height);

a)不要使用setBounds()。 Swing旨在与布局管理器一起使用

b)应该将表添加到JScrollPane,然后将滚动窗格添加到面板

c)将组件添加到可见帧时,基本代码为:

 panel.add(scrollPane);
 panel.revalidate();
 panel.repaint();