Problem with mysql query from java

0

First I make a record to the database from a Frame and without leaving the application I enter another Frame where I consult the data but the data that I just entered from the previous Frame does not appear, if I close the program and I'll run it again and enter the Frame of the queries directly if I see the record, this is the code I use to make the query.

 try {
    DefaultTableModel modelo = new DefaultTableModel();
    final JTable table = new JTable(modelo);
    TableColumnModel columnModel = table.getColumnModel();

    for(int i=0;i<4;i++) {
        modelo.addColumn(columnas[i]);
    }
    columnModel.getColumn(0).setPreferredWidth(70);
    columnModel.getColumn(1).setPreferredWidth(120);
    columnModel.getColumn(2).setPreferredWidth(30);
    columnModel.getColumn(3).setPreferredWidth(30);

    DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
    tcr.setHorizontalAlignment(SwingConstants.CENTER);
    table.getColumnModel().getColumn(0).setCellRenderer(tcr);
    table.getColumnModel().getColumn(1).setCellRenderer(tcr);
    table.getColumnModel().getColumn(2).setCellRenderer(tcr);
    table.getColumnModel().getColumn(3).setCellRenderer(tcr);

    table.setPreferredScrollableViewportSize(new Dimension(400,300));
    JScrollPane scrollpane = new JScrollPane(table);

    Connection con = getConection();
    String sql = "SELECT num_control,Nombre,fecha_inicio,fecha_termino FROM alumno";
    ps = con.prepareStatement(sql);
    rs = ps.executeQuery();

    ResultSetMetaData rsMd = rs.getMetaData();
    int cantidadColumnas = rsMd.getColumnCount();

    while(rs.next()) {
        Object[] filas = new Object[cantidadColumnas];
        for(int i=0; i<cantidadColumnas;i++) {
            filas[i]=rs.getObject(i+1); 
        }
        modelo.addRow(filas);
    }
    
asked by Luis Daniel Interiano Yañez 25.11.2018 в 07:58
source

1 answer

0

When changes are made to the data of the model of the table you must shoot that the change was given so that the User Interface Reflects those changes. This is achieved by using the following method of the Table model: modelo.fireTableDataChanged();

This method should be called when the data is finished updating the table. watch: Javadoc of AbstractTableModel

    
answered by 25.11.2018 в 08:21