how could you reuse this code to insert data into two different tables

0

I have two tables with the same fields and I'm using two equal methods for each insert in each table the only thing that changes is the name of the table. I would like to reuse the code just by changing the name of the table through a combobox. Is there any way?

 public int insertarHerra(String nombreHerra, String descripcionHerra, int stockHerra, String fechaAdquisicion) {
    conectando = Conectar.getConnection();
    //System.out.println(nombre);
    try {
        String sql = "insert into herramientasalmacen(nombreHerra, descripcionHerra, stockHerra,fechaAdquisicion) values(?,?,?,?)";
        preparando = conectando.prepareStatement(sql);
        preparando.setString(1, nombreHerra);
        preparando.setString(2, descripcionHerra);
        preparando.setInt(3, stockHerra);
        preparando.setString(4, fechaAdquisicion);

        int dato = preparando.executeUpdate();
        return dato;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Ocurrio un error al registrar usuario.");
    }
    return 0;
}
/////////////////////////////////////INSERTAR CONSUMIBLES////////////////////////////////////////////////////////
public int insertarConsu(String nombreConsu, String descripcionConsu, int stockConsu, String fechaAdquisicion) {
    conectando = Conectar.getConnection();
    //System.out.println(nombre);
    try {
        String sql = "insert into consumiblesalmacen(nombreConsu, descripcionConsu, stockConsu,fechaAdquisicion) values(?,?,?,?)";
        preparando = conectando.prepareStatement(sql);
        preparando.setString(1, nombreConsu);
        preparando.setString(2, descripcionConsu);
        preparando.setInt(3, stockConsu);
        preparando.setString(4, fechaAdquisicion);

        int dato = preparando.executeUpdate();
        return dato;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Ocurrio un error al registrar usuario.");
    }
    return 0;
}
    
asked by Damii 20.02.2018 в 17:03
source

1 answer

0

To answer your question specifically, you could add a parameter to the method that indicates which table the INSERT should be made.

public int insertarHerraOConsu(String tabla, String nombre, String descripcion, int stock, String fechaAdquisicion) {
        conectando = Conectar.getConnection();

        try {
            String sql = "insert into " + tabla + "(nombreHerra, descripcionHerra, stockHerra,fechaAdquisicion) values(?,?,?,?)";
            preparando = conectando.prepareStatement(sql);
            preparando.setString(1, nombreHerra);
            preparando.setString(2, descripcionHerra);
            preparando.setInt(3, stockHerra);
            preparando.setString(4, fechaAdquisicion);

            int dato = preparando.executeUpdate();
            return dato;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Ocurrio un error al registrar usuario.");
        }
        return 0;
    }

If you use this solution, you should be careful what the user selects in the combo is not the same as you pass the method, because you could do SQL injection. In this case, the convenience is that you use constants for the selection of the user and you validate it and transform the name of the table.

Now, maybe you can be more convenient to use a single table and add a column indicating the type: tool or consumable.

Greetings

    
answered by 20.02.2018 в 19:15