Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:30:22+00:00 2026-05-27T18:30:22+00:00

I created a JTable and defined MyTableModel in order to populate this table from

  • 0

I created a JTable and defined MyTableModel in order to populate this table from MySQL DB. However, the problem is that when I execute UPDATE statement, then an additional row is included in JTable (but not in MySQL DB).

How should I update my code to be able to properly update JTable without re-opening the application? (I’m using fireTableChanged(null))

 public class QueryTableModel extends AbstractTableModel {
      Vector cache;
      int colCount;
      String[] headers;

      String db;
      String dbHost;
      String dbLogin;
      String dbPassw;     
      Statement statement;

      public QueryTableModel() {
        cache = new Vector();
      }

      public String getColumnName(int i) {
        return headers[i];
      }

      public int getColumnCount() {
        return colCount;
      }

      public int getRowCount() {
        return cache.size();
      }

      public Object getValueAt(int row, int col) {
        return ((String[]) cache.elementAt(row))[col];
      }

      public Connection getConnection()
                throws Exception {
            String dbURL = "";
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                System.out.println("Cannot find the MySQL JDBC driver. ");
                throw (e);
            }
            try {
                dbURL = "jdbc:mysql://" + dbHost + "/" + db;
                // WINDOWS
                Connection con = DriverManager.getConnection(dbURL,dbLogin,dbPassw);
                // LINUX
                // Connection con = DriverManager.getConnection(url);
                return con;
            } catch (SQLException e) {
                System.out.println("No connection with " + dbURL);
                throw (e);
            }
        }

      public void setQuery(String query) {
        cache = new Vector();
        try {
          // Execute the query and store the result set and its metadata
          Connection con = getConnection();
          Statement statement = con.createStatement();
          ResultSet rs = statement.executeQuery(query);
          ResultSetMetaData meta = rs.getMetaData();
          colCount = meta.getColumnCount();

          // Rebuild the headers array with the new column names
          headers = new String[colCount];
          for (int h = 1; h <= colCount; h++) {
            headers[h - 1] = meta.getColumnName(h);
          }

          while (rs.next()) {
            String[] record = new String[colCount];
            for (int i = 0; i < colCount; i++) {
              record[i] = rs.getString(i + 1);
            }
            cache.addElement(record);
          }
          fireTableChanged(null);

          rs.close();
          if (con.getAutoCommit() != false) {
            con.close();
          }

        } catch (Exception e) {
          cache = new Vector(); // blank it out and keep going.
          e.printStackTrace();
        }
      }

      public void setQueryUpdate(String[] data, String q) {
        try {
          Connection con = getConnection();
          Statement statement = con.createStatement();
          int rs = statement.executeUpdate(q);
          String[] record = new String[data.length];
          for (int i = 0; i < data.length; i++) {
            record[i] = data[i];
          }
          cache.addElement(record);
          fireTableChanged(null);
          if (con.getAutoCommit() != false) {
            con.close();
          }
        } catch (Exception e) {
          cache = new Vector(); // blank it out and keep going.
          System.out.println(e.getMessage());
        }
      }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:30:23+00:00Added an answer on May 27, 2026 at 6:30 pm

    Now I see what you’re trying here. You need to call:

    fireTableDataChanged();
    

    If you call fireTableChange(null) all the listeners will be called with Event equal to null. Depending on the implementation it will a) throw a NPE or b) do nothing. The fireTableDataChanged(); tells listeners that data has changed and to update accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a JTable, the table contains 4 rows, and for a specific
I have a JTable that is populated using a custom TableModel I created. I
In Java Swing I have created a JTable which uses a table model class
This function returns ArrayList that contains some data from DB: public ArrayList<ArrayList<Object>> executeQuery(String host,
I created a JTable with a table model . Now based on an input
I have a JTable displaying rows from an SQL database. The table is relatively
I'm trying to create a jTable that, once a button is clicked, adds rows
Created .NET WCF service, tested it - works. Generated schemas from Data and service
In Netbeans, I created a JTable and bound it's values to a JPA result
Good evening I have a JTable that I built with a TableModel how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.