简体   繁体   English

sql查询不会更新JTable

[英]sql query doesn't update JTable

I am currently making an app that allows customers to make accounts and for managers to sign up staff (among other things). 我目前正在开发一个应用程序,该程序允许客户进行帐户结算,并允许经理签署人员(除其他外)。

I can get the JTable to display all the accounts but when I click on a button to add a staff member I have to exit the frame and then go back into it to see the update, but I want it to update on the spot. 我可以让JTable显示所有帐户,但是当我单击添加工作人员的按钮时,我必须退出框架,然后再回到框架中以查看更新,但是我希望它立即进行更新。

The next three bits of code are in the DbConnect class and the final one is in the ManageAccount class. 接下来的三位代码在DbConnect类中,最后一位在ManageAccount类中。

 //This gets the data and creates the tablemodel
 public static DefaultTableModel buildTableModel(ResultSet rs)
        throws SQLException {

    ResultSetMetaData metaData = rs.getMetaData();

    // names of columns
    Vector<String> columnNames = new Vector<String>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }

    // data of the table
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    while (rs.next()) {
        Vector<Object> vector = new Vector<Object>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(rs.getObject(columnIndex));
        }
        data.add(vector);
    }

    return new DefaultTableModel(data, columnNames);

}

This is the query I use to display the accounts in the JTable 这是我用来在JTable中显示帐户的查询

 public JTable ManageAcc2(JTable t) {
    try {
        String query = ("SELECT * FROM staff");
        rt = st.executeQuery(query);

        t = new JTable(buildTableModel(rt));

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

 return t;
}   

This is the query that adds an extra staff member 这是添加额外工作人员的查询

public void AddStaff(JTable t) {

    try {

        String query = "INSERT INTO staff VALUES('" + NULL + "','" + NULL + "','" + NULL + "','" + NULL + "','" + NULL + "','staff')";
        st.executeUpdate(query);

    } catch (Exception ex) {
        ex.printStackTrace();
    } 
}

Finally this is a separate class where I create the table that contains all the data 最后,这是一个单独的类,在其中创建包含所有数据的表

public class ManageAccount extends JPanel{

JPanel jNorth = new JPanel();
JPanel jCenter = new JPanel();
JPanel jSouth = new JPanel();
    JTable t = new JTable();

JScrollPane listScroll = new JScrollPane();
    JScrollPane listScroll2 = new JScrollPane();
    JScrollPane listScroll3 = new JScrollPane();
    JButton AddStaff = new JButton("Add Staff");
JButton goBack = new JButton("Back");
    ManagerDashboard md;
    DbConnect dbc = new DbConnect();  

ManageAccount(ManagerDashboard m){
      try{  


            this.md = m;
            this.setPreferredSize(new Dimension(600, 400));
    this.setVisible(true);

    this.setLayout(new BorderLayout());
    this.add(jNorth, BorderLayout.NORTH);
    this.add(jCenter, BorderLayout.CENTER);
    this.add(jSouth, BorderLayout.SOUTH);                       

            listScroll = new JScrollPane(dbc.ManageAcc(t));
            listScroll.setPreferredSize(new Dimension(600, 116));

            listScroll2 = new JScrollPane(dbc.ManageAcc2(t));
            listScroll2.setPreferredSize(new Dimension(600, 116));

    jNorth.setLayout(new BorderLayout());
    jNorth.add(goBack, BorderLayout.WEST);
    goBack.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

     md.removeManageAccount();

        }
    }); 

            jCenter.add(listScroll);  
            jCenter.add(listScroll2);

            jSouth.setLayout(new FlowLayout());
    jSouth.add(AddStaff, BorderLayout.SOUTH);
            AddStaff.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e){
                dbc.AddStaff(t);

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

}

I have looked at other answers to similar questions but using methods like setModel(), repaint(),etc doesn't seem to work for me. 我已经看过类似问题的其他答案,但使用setModel(),repaint()等方法似乎对我不起作用。

Thanks in advance 提前致谢

Make the jTable reference the ManageAccount class' field rather than local variable. 使jTable引用ManageAccount类的字段,而不是局部变量。

In your AddStaff you pass the JTable but use the introduced field instead. 在您的AddStaff您传递JTable,但改用引入的字段。 After inserting call something like this 插入电话后,像这样

    String query = ("SELECT * FROM staff");
    rt = st.executeQuery(query);

    theTableField.setModel(buildTableModel(rt));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM