Refreshing a Jtable with the search results from a Mysql database?

4340 views java

Programming newbie here. Having trouble updating tables with MySQL search results.

I am busy learning Java 8 and I am currently coding a small database program with a GUI as practice. I am using Netbeans for this. I am not coding the GUI(though I can) but rather using the NetBeans GUI designer.

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.

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.

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.

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

public class JavaClass extends javax.swing.JFrame {

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

    public JavaClass() {

// the next line is for a Jpanel containing the search textflield and search button part of a defunct combo box selection event

    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 ( {
// 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"));

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



answered question

1 Answer


So, from the sounds of things, you need to remove all the pre-existing row from the JTable model. From memory, using a DefaultTableModel, this is very easy...

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

// Fill with results from database

posted this

Have an answer?


Please login first before posting an answer.