2013-03-01 44 views
0

这是一个JDBC项目。来自WAMP服务器上的MySQL数据库的数据显示在JTable中。我想在我的JSpinner上输入一个用户输入的ID,从表中删除具有该ID的行。我做了一个SQL查询并且一切正常,但当我的查询执行时,我的JTable上的数据不会刷新。我点击我的JNazad按钮(我的后退按钮),然后重新进入该窗口,以便我的JTable显示刷新的数据。我在我的NapraviTablicu方法中没有执行FireTableModel,因为它的DefaultTableModel和更新会自动完成。我不知道我做错了什么:执行我的数据库查询时,JTable不刷新

public class GUIBDelete extends JFrame{ 

    private SpinnerModel SM; 
    private JSpinner Spinner; 
    private JLabel LUnos; 
    private JButton BNazad, BIzvrsi; 
    private String ID, SqlQuery; 
    private Vector NaziviKolona = new Vector(); 
    private Vector Podaci = new Vector(); 
    private JTable Tablica=new JTable(); 
    private JScrollPane ScrollPane; 
    private DefaultTableModel model; 


    private JTable NapraviTablicu(){ 
     try { 
      String SqlQuery = "SELECT * FROM `nfc_baza`"; 
      Podaci.clear(); 
      NaziviKolona.clear(); 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = DriverManager.getConnection("jdbc:mysql://" 
        + "localhost:3306/nfc", "root", ""); 

      Statement Stat = con.createStatement(); 
      ResultSet Rez = Stat.executeQuery(SqlQuery); 
      ResultSetMetaData md = Rez.getMetaData(); 
      int columns = md.getColumnCount(); 
      for (int i = 1; i <= columns; i++) { 
       NaziviKolona.addElement(md.getColumnName(i)); 
      } 
      while (Rez.next()) { 
       Vector red = new Vector(columns); 
       for (int i = 1; i <= columns; i++) { 
        red.addElement(Rez.getObject(i)); 
       } 
       Podaci.addElement(red); 
      } 
      Rez.close(); 
      Stat.close(); 
      con.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
     model = new DefaultTableModel(Podaci, NaziviKolona); 
     JTable table = new JTable(model); 

     return table; 

    } 

    ActionListener a1 = new ActionListener() { 
     public void actionPerformed(ActionEvent a) { 
      dispose(); 
      new GUIIzbornik(); 
     } 
    }; 


    ActionListener a2 = new ActionListener() { 
     public void actionPerformed(ActionEvent a) { 
      ID=null; 
      SqlQuery = "DELETE FROM `nfc`.`nfc_baza` WHERE `nfc_baza`.`ID` = "; 
      IzvrsiQuery(); 
     } 

     private void IzvrsiQuery() { 
      Object sp = Spinner.getValue(); 
      ID = sp.toString(); 
      SqlQuery=SqlQuery+ID; 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
       Connection con2 = DriverManager.getConnection(
         "jdbc:mysql://" + "localhost:3306/nfc", "root", ""); 
       Statement Stat = con2.createStatement(); 
       int Rez = Stat.executeUpdate(SqlQuery); 
       Stat.close(); 
       con2.close(); 
       JOptionPane.showMessageDialog(null, "Uspješno izvrseno!", 
         "Poruka!", JOptionPane.INFORMATION_MESSAGE); 

      } catch (Exception e) { 
       System.out.println(e); 
      } 

     } 
    }; 


    GUIBDelete(){ 
     setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     Tablica=NapraviTablicu(); 
     ScrollPane = new JScrollPane(Tablica); 
     c.fill = GridBagConstraints.BOTH; 
     c.insets = new Insets(2, 2, 2, 2); 
     c.weightx = 0.1; 
     c.weighty = 0.1; 
     c.gridwidth = 4; 
     c.gridheight = 2; 
     c.gridx = 0; 
     c.gridy = 0; 
     add(ScrollPane, c); 


     LUnos= new JLabel("<html><br>Unesite ID elementa</br> kojeg želite obrisati:<html>"); 
     c.gridx = 0; 
     c.gridy = 2; 
     c.gridwidth = 1; 
     c.gridheight = 1; 
     add(LUnos, c); 

     SM = new SpinnerNumberModel(1, 1, 1000, 1); 
     Spinner = new JSpinner(SM); 
     c.gridx = 2; 
     c.gridy = 2; 
     c.gridwidth = 2; 
     add(Spinner, c); 

     BNazad = new JButton("Nazad"); 
     c.gridx = 0; 
     c.gridy = 3; 
     c.gridwidth = 1; 
     BNazad.addActionListener(a1); 
     add(BNazad, c); 

     BIzvrsi = new JButton("Izvrši"); 
     c.gridx = 2; 
     c.gridy = 3; 
     BIzvrsi.addActionListener(a2); 
     add(BIzvrsi, c); 

     setSize(400, 500); 
     setTitle("Brisanje podataka"); 
     setVisible(true); 
     setLocationRelativeTo(null); 

    } 


    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       GUIBDelete i = new GUIBDelete(); 
      } 
     }); 
    } 
} 

回答

1

它看起来像你删除从数据库表中的一行,并且希望JTable以反映更改。在izvrsiQuery()更新您的TableModelJTable将自行更新。例如,

... 
con2.close(); 
model.removeRow(((Number)(spinner.getValue())).intValue() - 1); 
JOptionPane.showMessageDialog(...); 
... 

注意,行号在0开始,他们必须使用RowSorter当翻译。

顺便说一句,如果您使用常见的Java命名约定和因子常量,您的代码将更容易让其他人阅读。

附录:该示例仅删除一行数字。您必须搜索TableModel以查找与删除的ID对应的行。

附录:尽管不太实际,但您可能需要在使用setModel()进行DML操作后刷新数据库中的整个表格,如here所示。

+0

对不起,但你不明白我。当JSpinner更改值时,我不希望我的表数据发生更改。我希望当我的查询执行时,我的jtable会更改数据,当用户输入他想要在Jspinner上删除的行的ID时,会发生这种情况,之后他点击Jbutton“Izvrsi”(我的查询被执行),那么Jtable必须更改data ... – 2013-03-01 19:58:06

+0

好的,改变'napraviTablicu()'返回一个'TableModel'并且执行'tablica.setModel()'来刷新表格,如下所示[here](http://stackoverflow.com/a/8260663/230513)。 – trashgod 2013-03-02 00:05:11