簡體   English   中英

用來自 Mysql 數據庫的搜索結果刷新 Jtable?

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

編程新手在這里。 使用 MySQL 搜索結果更新表時遇到問題。

我正在忙於學習 Java 8,目前正在編寫一個帶有 GUI 的小型數據庫程序作為練習。 我為此使用 Netbeans。 我沒有編寫 GUI(盡管我可以),而是使用 NetBeans GUI 設計器。

當我運行程序時,會彈出一個窗口,其中包含一個文本字段和一個帶有 Jtable 的搜索按鈕,其中 4 列已經填充了數據庫條目。

我希望能夠使用文本字段和搜索按鈕運行部分搜索,並在單擊搜索按鈕時刷新 Jtabel 以僅顯示搜索結果。

到目前為止,我已將搜索結果作為更多行附加到表中。 將整個表作為更多行附加到表中,或者什么也不發生。

這是我用來設置數組和 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;
    }

}

這是我啟動程序時用來填充 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);


}

因此,從表面上看,您需要從JTable模型中刪除所有預先存在的行。 從內存中,使用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