2013-11-26 52 views
0

中即使它显示ResultSet中有结果(行),但在将这些行带入JTable时出现问题。当我运行该程序时,从MS Access数据库中检索到的结果不会出现在JTable上。请帮忙 ! 我的代码如下!ResultSet的结果未显示在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); 
+0

添加表创建GUI时,调整DB访问后的模型。为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 –

回答

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

一)不使用的setBounds()。摆动被设计为与布局管理器

b)一个表应该被添加到一个JScrollPane,然后滚动面板添加到面板

c)在添加组分的可见帧的基本码是被用于:

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