简体   繁体   English

用来自 Mysql 数据库的搜索结果刷新 Jtable?

[英]Refreshing a Jtable with the search results from a Mysql database?

Programming newbie here.编程新手在这里。 Having trouble updating tables with MySQL search results.使用 MySQL 搜索结果更新表时遇到问题。

I am busy learning Java 8 and I am currently coding a small database program with a GUI as practice.我正在忙于学习 Java 8,目前正在编写一个带有 GUI 的小型数据库程序作为练习。 I am using Netbeans for this.我为此使用 Netbeans。 I am not coding the GUI(though I can) but rather using the NetBeans GUI designer.我没有编写 GUI(尽管我可以),而是使用 NetBeans GUI 设计器。

When I run the program a window pops up containing a text field and search button with a Jtable with 4 columns already populated with database entries.当我运行程序时,会弹出一个窗口,其中包含一个文本字段和一个带有 Jtable 的搜索按钮,其中 4 列已经填充了数据库条目。

I want to be able to run partial searches using the text field and search button and have the Jtabel refresh to display only the search results when I click the search button.我希望能够使用文本字段和搜索按钮运行部分搜索,并在单击搜索按钮时刷新 Jtabel 以仅显示搜索结果。

So far I have either append the search results as more rows onto the table.到目前为止,我已将搜索结果作为更多行附加到表中。 Append the entire table as more rows onto the table, or having nothing happen at all.将整个表作为更多行附加到表中,或者什么也不发生。

Here is the Java class the I use to set up an array and getters.这是我用来设置数组和 getter 的 Java 类。

class array {

    private String Column_One;
    private String Column_Two;
    private String Column_Three;
    private String Column_Four;

    public User(String Column_One, String Column_Two, String Column_Three, String Column_Four) {
        this.Column_One = Column_One;
        this.Column_Two = Column_Two;
        this.Column_Three = Column_Three;
        this.Column_Four = Column_Four;
    }

    public String get Column_One() {
        return Column_One;
    }

    public String getColumn_Two() {
        return Column_Two;
    }

    public String getColumn_Three() {
        return Column_Three;
    }

    public String getColumn_Four() {
        return Column_Four;
    }

}

Here is the code that used to populate the Jtable when I start up the program这是我启动程序时用来填充 Jtable 的代码


public class JavaClass extends javax.swing.JFrame {

    public ArrayList<array> List = new ArrayList<array>();

    public JavaClass() {
        initComponents();
        populate_Table();

// the next line is for a Jpanel containing the search textflield and search button part of a defunct combo box selection event
        JPanel.setVisible(true);



    public ArrayList<array> arrayLists() {
        {

            String Url = "Url";

            try {
                Connection DbCon = DriverManager.getConnection(Url, "username", "password");
                String sql = "SELECT  ColumnOne, ColumnTwo, ColumnThree, ColumnFour FROM mysqldatabasetable ";
                Statement pst = DbCon.createStatement();
                ResultSet rs = pst.executeQuery(sql);
                User user;
                while (rs.next()) {
// The next line is what the columns will be called in Mysql
                    user = new User(rs.getString("sqlColumnOne"), rs.getString("sqlColumnTwo"), rs.getString("sqlColumnThree"), rs.getString("sqlColumnFour"));
                    List.add(array);
                }

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex);
            }

        }
        return List;
    }

    public void populate_Table() {
        ArrayList<array> JtableList = arrayLists();
        DefaultTableModel model = (DefaultTableModel) Guidatabasetable.getModel();

        Object[] row = new Object[4];
        for (int i = 0; i < JtableList.size(); i++) {
            row[0] = JtableList.get(i).getColumn_One();
            row[1] = JtableList.get(i).getColumn_Two();
            row[2] = JtableList.get(i).getColumn_Three();
            row[3] = JtableList.get(i).Column_Four();
            model.addRow(row);

        }
        Guidatabasetable.setModel(model);


}

So, from the sounds of things, you need to remove all the pre-existing row from the JTable model.因此,从表面上看,您需要从JTable模型中删除所有预先存在的行。 From memory, using a DefaultTableModel , this is very easy...从内存中,使用DefaultTableModel ,这很容易......

DefaultTableModel model = (DefaultTableModel) Guidatabasetable.getModel();
model.setRowCount(0);

// Fill with results from database

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

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