I have a problem in java when executing an sql query to show records in a jtable

0

This problem occurs to me with this method to show data from a bd in java, because it tells me that the query did not generate any results when I saved in my form and I need to save and that immediately I show the record, this code shows it but I have to get out of the project and go back to see the records and what I want is that when entering is shown immediately, I would appreciate the answer

void MostrarDatos(){
    DefaultTableModel modelo=new DefaultTableModel();
    modelo.addColumn("Rif");
    modelo.addColumn("Cedula");
    modelo.addColumn("Nombre");
    modelo.addColumn("Apellido");
    modelo.addColumn("Telefono");
    modelo.addColumn("TR");
    modelo.addColumn("TC");
    modelo.addColumn("Direccion");
    datalistado.setModel(modelo);
    String []datos = new String [8];
    try {
        PreparedStatement ps = cn.prepareStatement("SELECT * FROM cliente");
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            datos[0]=rs.getString(1);
            int IDENTIFICADOR = (int)rs.getInt(2);
            String NUMERODEIDENTIFICADOR =                 String.valueOf(IDENTIFICADOR);
            datos[1]=NUMERODEIDENTIFICADOR;
            datos[2]=rs.getString(3);
            int TELEFONO = (int)rs.getInt(5);
            String NUMERODETELEFONO = String.valueOf(TELEFONO);
            datos[4]=NUMERODETELEFONO;
            datos[5]=rs.getString(6);
            datos[6]=rs.getString(7);
            datos[7]=rs.getString(8);
            modelo.addRow(datos);
        }   
        datalistado.setModel(modelo);
    } catch (SQLException ex) {
        Logger.getLogger(Clientes.class.getName()).log(Level.SEVERE, null, ex);
    }
}
    
asked by Efrainrodc 20.12.2016 в 01:10
source

2 answers

0

What you must do is, at the time of making an insertion to the BD and that is reflected in the execution time in the JTable is to warn the model of your JTable that the data is about to change.

Example:

tabla.setModel(m);    //la tabla se actualiza. m es el modelo
    
answered by 20.12.2016 / 01:46
source
1

It is necessary to tell the DefaultTableModel that changes have been made:

After doing the insertion in the database, add the following:

modelo.fireTableDataChanged();

That according to the docs :

  

Notifies all listeners that all cell values in the table may   have changed The number of rows may also have changed and the JTable   should redraw the table from scratch. The structure of the table (as   in the order of the columns) is assumed to be the same.

In Spanish:

  

Notify all event listeners that all values of   cells in the rows of the table have been able to change. The number of rows   it could also have changed and the JTable should re-draw the table   right from the start. The structure of the table (such as the order of the   columns) is assumed to remain the same.

    
answered by 20.12.2016 в 01:26