SQL查询不更新JTable

时间:2015-04-09 07:54:52

标签: java mysql sql jtable

我目前正在制作一款应用程序,允许客户创建帐户,并让经理注册员工(以及其他内容)。

我可以让JTable显示所有帐户但是当我点击一个按钮添加一个工作人员时我必须退出框架然后返回到它以查看更新,但我希望它更新点。

接下来的三位代码在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);

}

这是我用来在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;
}   

这是添加额外员工的查询

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();
    } 
}

最后,这是一个单独的类,我在其中创建包含所有数据的表

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);
}      
}

}

我已经查看了类似问题的其他答案,但使用setModel(),repaint()等方法似乎对我没用。

提前致谢

1 个答案:

答案 0 :(得分:0)

使jTable引用为ManageAccount类的字段而不是局部变量。

AddStaff中传递JTable但使用引入的字段。插入这样的调用之后

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

    theTableField.setModel(buildTableModel(rt));