2013-11-26 24 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();