Delete row from jTable with row id

0

I have a jTable I can delete the row without problem with the following function:

int fila = tabla.getSelectedRow();
        if (fila >= 0) {
            m.removeRow(fila);
        }

With that I delete the selected row there is no problem, the problem is that I do not know how to identify the row in which the record "Daniel" is located, that is I have a table with the following:

ID|Nombre|Direccion
1|Carlos|Av.las manzanas
2|Maria|Av.las100
3|Daniel|Av.Loque sea

that is I want to delete the row in which Id3 is located, but that id is the id that has the name "Daniel" in the database, that is, in the table the row number would be number 2 ,

It is possible to delete the row indicating the value, something like in sql, but I do not know how it could be done in java

Delete fila where idPersona=1; 
    
asked by jeancarlos733 02.09.2016 в 18:27
source

2 answers

1

Assuming a TableModel or derivative called m the method responsible for erasing the rows that contain a certain string would be something like this:

public void borrarFilasCon(String nombre)
 {
    for (int f = 0; f < m.getRowCount(); f++)
    {
      for(int c = 0; c < m.getColumnCount(); c++)
      {
        if (m.getValueAt(f, c).equals(nombre)) 
        {
          m.removeRow(f);

        }
      }
    }
 }
    
answered by 03.09.2016 / 00:52
source
2

First I would recommend you to implement 2 methods in your connection class that will be useful to you. The getSt () and getCon () methods as follows:

public conexion(){
    try{
        Class.forName(drv);
        ct = DriverManager.getConnection(db, user, pass);
        st = ct.createStatement();
        //System.out.println("Conexión exitosa");
    }catch(Exception e){
        System.out.println("Error de conexión");
                    e.printStackTrace();
    }
}


public Statement getSt(){
    return st;
}

    public Connection getCon(){
            return ct;
}

Then, where you have your table, you can run this code

//Se crea una instancia de la clase conexion
conexion cc = new conexion();//al inicio de la clase

public void tuMetodo(){
int filsel = tuTabla.getSelectedRow();
    if(filsel == -1){
        JOptionPane.showMessageDialog(null, mensajeTabla);
    }else{
        try {
            TableModel m = tuTabla.getModel();
            for(int i = 0; i<m.getRowCount(); i++){
                //Se toman los datos de la tabla cliente
                int id = (int) m.getValueAt(filsel, 0);//suponiendo que el id lo muestras en la primera columna
                String sql="SELECT * FROM tablaBD WHERE ID = '" + id + "'";
                ResultSet rs = cc.getSt().executeQuery(sql);
                if(rs.next()){
                    String sql2="TU CONSULTA PARA ELIMINAR WHERE ID = '" + id + "'";
                    Statement st = cc.getCon().createStatement();
                    st.executeUpdate(sql2);
                    st.close();
                }
                m.removeRow(filsel);
            }//fin del for

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}//Fin de tuMetodo

This will eliminate, from both your database and your JTable, the record you selected

    
answered by 06.09.2016 в 23:45