How to refresh jtable when database has changed

Hi everybody,

this is the first time I write here. I磎 not sure is this the right topic or not.

Anyway I have a problem with my code.

I have created jtable which displays mysql database and I have textfield where user inputs new data that goes to database by pressing button.

Problem is that new data goes to database

but ...jtable does not show it unless I close the program entirely and open it up again.

Here磗 a part of the code I磎 using. Any suggestions are appreciated.

package levyarkisto07;

import javax.swing.table.DefaultTableModel;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Connection;

class CTauluPohjaextends DefaultTableModel{

public CTauluPohja(){

try{

jbInit();

}

catch (Exception ex){

ex.printStackTrace();

}

}

private CTauluPohja model;

private Connection con;

publicvoid teeTaulu(ResultSet results){

try{

ResultSetMetaData metadata = results.getMetaData();

int columns = metadata.getColumnCount();

// Get the column names and set header names

for(int i = 0; i < columns; i++){

addColumn(metadata.getColumnLabel(i+1));

}

// Get all rows

while(results.next()){

String[] rowData =new String[columns];// Create array to hold the data

for(int i = 0; i < columns; i++){// For each column

rowData[ i ] = results.getString(i+1);// retrieve the data item

}

addRow(rowData);// Add a row

}

fireTableChanged(null);// Signal the table there is new model data

}catch (SQLException sqle){

System.err.println(sqle.getMessage());

}

}

privatevoid jbInit()throws Exception{

}

}

package levyarkisto07;

import java.sql.ResultSet;

import java.sql.*;

class CTietohaku{

private CTauluPohja model;

public CTietohaku(){

String url ="jdbc:mysql://localhost/levyarkisto";

try{

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection(url," "," ");

Statement stmt = con.createStatement();

String sql ="select tiedotid as ID, artistinimi as Artisti, levynimi as Levy, raitanro as Nro, kappalenimi as Kappale, levymuoto as T from levytiedot join levy using(levyid) join artisti using (artistiid) join raita using (raitaid) join kappale using (kappaleid) join levymuodot using (lmuotoid) order by artistinimi,levyid,raitaid";

ResultSet rs = stmt.executeQuery(sql);

haeTaulu(rs);

rs.close();

stmt.close();

con.close();

}

catch (Exception exc){

System.err.println(exc.getMessage());

}

try{

jbInit();

}

catch (Exception ex){

ex.printStackTrace();

}

}

public CTauluPohja getSanasto(){

return model;

}

void haeTaulu(ResultSet rs){

model =new CTauluPohja();// Create a table model

model.teeTaulu(rs);

}

privatevoid jbInit()throws Exception{

}

}

jTaTietokanta.setModel(hae.getSanasto());

Thank you

[6192 byte] By [Zarimaa] at [2007-11-26 22:25:01]
# 1

Hi, I also had that problem once.

To fix it I just add a listener to the button, so that each time that the button is pressed, the tabled is refreshed.

To refresh the table you can use the setTableModel() method and build the tableModel from the database. You have to do that each time the button is pressed.

Hope this can help you.

melqkiadesa at 2007-7-10 11:25:38 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Thank you for your reply melqkiades .

I have a button listener and I have tried to call the table with this code ( the same that I used to create the table in the first place):

jTaTietokanta.setModel(hae.getSanasto());

but it always gives me the same table and same data.

Nothing changes.

What is the difference with setTableModel() and setModel()?

Or do you mean that I have to make a new method that only gets the data and puts it in the table that has already been done with this?

jTaTietokanta.setModel(hae.getSanasto());

What you wrote sounds just like how I need this to work but I磎 quite new with Java and I still get mixed up with these methods :) .

Zarimaa at 2007-7-10 11:25:38 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Sorry, I was wrong, the method's name is setModel() as you have it

Here is a small example:

JTable myTable = new Table();

JButton refreshTableButton = new JButton("Refresh Table");

refreshTableButton. addActionListener(this);

public void actionPerformed(ActionEvent e) {

if (e.getSource() == refresTableButton) {

myTable.setModel(yourTableModel);

}

}

Remember that you have to build yourTableModel each time the database is modified

That's what I have been using so far

It worked for me, hope it works for you too ;)

melqkiadesa at 2007-7-10 11:25:38 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

I feel like I磎 banging my head to a wall. I just can磘 get this working.

What ever I do the resut is the same. Table that is first created has the first value no matter what?

How do I get updated value to my table? Is the problem in my sql connection or what.

I made table 2 and tested it like this:

1. I opend table 1

2. I changed database

3. Cheked from mysql that there was new data

4. Opened table 2

And still the result with table 1 and 2 was the same.

Any suggestions are appreciated.

Zarimaa at 2007-7-10 11:25:38 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Then I think your problem may be with the ResultSet, not with the JTable
melqkiadesa at 2007-7-10 11:25:38 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...