Update Jtable after Inserting a Data into the Database

1

I have a problem when I want to update a Jtable, when I click on the insert button, the entered data should appear in the table but nothing appears.

this what I have in the insert button Detail:

insertar ins = new insertar();  
ins.insdetfactcompra(Integer.parseInt(txtFactura.getText()),txtDescripcion.getText(),Float.parseFloat(txtPrecio.getText()));
float precio = Float.parseFloat(txtPrecio.getText());
txtDescripcion.setText("sin descripcion");
txtPrecio.setText("0.00");  
total = total + precio;
txtTotal.setText(Float.toString(total));
modeloTabla.setRowCount(0);   

try{
    String sql = "Select descripcion,precio from detalle_factura_compra where factura="+Integer.parseInt(txtFactura.getText());
    PreparedStatement ps = con.conexion().prepareStatement(sql);
    try (ResultSet res = ps.executeQuery()) {
        Object datos[]= new Object[2];
        while(res.next()){
            for (int i = 0;i<2;i++){
                datos[i]= res.getObject(i+1);
            }
            modeloTabla.addRow(datos);
        }
    }
} catch (SQLException ex) {
    Logger.getLogger(Consulta_Cliente.class.getName()).log(Level.SEVERE, null, ex);
    ex.printStackTrace();
}
    
asked by Jeredick Escobar 16.10.2018 в 19:14
source

2 answers

1

When inserting a jTable, the entered data will not be shown at the moment, what you can do is create a method that makes a selection to the table that you entered the data pointing to the jtable, so, you can do as a refresh to the table, so:

[ CODE OF YOUR INSERT ]

[ FLAMES TO THE METHOD ]

And ready: 3, I'm waiting for your feedback

    
answered by 17.10.2018 / 16:23
source
0

Solution

public class Registro_Factura_Compra extends javax.swing.JFrame {
    float total = 0;
    int factura_detalle=0;
    conectar con = new conectar();
    Connection cc = con.conexion();
    DefaultTableModel modelo = new DefaultTableModel();
    JTable tabla = new JTable(modelo);
public Registro_Factura_Compra() {


    initComponents();        
}
//clase que llamo para llenar la tabla despues de insertar datos
private void LLenarTabla(int fd){
    modelo.addColumn("descripcion");
    modelo.addColumn("precio");
    modelo.setRowCount(0);

        try {
            Statement s = cc.createStatement();
            ResultSet rs = s.executeQuery("SELECT descripcion,precio FROM sgc_contabilidad.detalle_factura_compra where factura="+fd);
            while (rs.next())
            {
                // Se crea un array que será una de las filas de la tabla.
                Object [] fila = new Object[2]; // Hay tres columnas en la tabla

                // Se rellena cada posición del array con una de las columnas de la tabla en base de datos.
                for (int i=0;i<2;i++)
                fila[i] = rs.getObject(i+1); // El primer indice en rs es el 1, no el cero, por eso se suma 1.

                // Se añade al modelo la fila completa.
                modelo.addRow(fila);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Registro_Factura_Compra.class.getName()).log(Level.SEVERE, null, ex);
        }

}

//boton con el que inserto datos

private void btnRDetalleActionPerformed(java.awt.event.ActionEvent evt) {                                            
    insertar ins = new insertar();    

    ins.insdetfactcompra(factura_detalle,txtDescripcion.getText(),Float.parseFloat(txtPrecio.getText()));
    float precio = Float.parseFloat(txtPrecio.getText());
    txtDescripcion.setText("sin descripcion");
    txtPrecio.setText("0.00");  
    total = total + precio;
    txtTotal.setText(Float.toString(total));

    LLenarTabla(factura_detalle);

}

in jtable properties in model - > custom code - > [name of the assigned model]

    
answered by 18.10.2018 в 20:32