Get data from MySQL to JTextField Java

1

I'm trying to get data from my db and display it in TextField's, my problem is that you only get the values of the first table in my list, when you select the second, they are not updated

I charge it with MouseClick

This is the first field selected, they load their full values

And this happens when trying to load the second item in the list

Only the values that are visible in the rows of the table change, the others do not, however if I have data in my db

Author Table

Table Manager

Next I insert the code of the form that I try to call the values, I would appreciate that they corrected me the correct way of doing it

private void tableMouseClicked(java.awt.event.MouseEvent evt) {                                   
    try {
        String temporalgenero = "";
        String temporaltecnica = "";
        String tempencargado = "";

        int fila = table.getSelectedRow();
        String id = (String) table.getValueAt(fila, 0);
        String rutautor = (String) table.getValueAt(fila, 1);
        String tecnica = (String) table.getValueAt(fila, 2);
        String genero = (String) table.getValueAt(fila, 3);
        String resultdate = (String) table.getValueAt(fila, 4);
        String nombrep = (String) table.getValueAt(fila, 5);
        String tamaño = (String) table.getValueAt(fila, 6);
        String ubicacion = (String) table.getValueAt(fila, 7);

        idpintura1.setText(id);
        aidi.setText(id);
        rut_a.setText(rutautor);
        año_c_arte.setText(resultdate);
        nombre_arte.setText(nombrep);
        tamaño_arte.setText(tamaño);
        nombre_sala.setText(ubicacion);

        switch (tecnica) {
            case "101":
                temporaltecnica = "Oleo";
                break;
            case "102":
                temporaltecnica = "Puntillismo";
                break;
            case "103":
                temporaltecnica = "Acuarela";
                break;
            case "104":
                temporaltecnica = "Fresco";
                break;
            case "105":
                temporaltecnica = "Temple";
                break;
            case "Null - No Definido":
                temporaltecnica = "Seleccione una opción";
                break;
        }

        tecnica_arte.setSelectedItem(temporaltecnica);

        switch (genero) {
            case "201":
                temporalgenero = "Desnudo";
                break;
            case "202":
                temporalgenero = "Retrato";
                break;
            case "203":
                temporalgenero = "Naturaleza Muerta";
                break;
            case "204":
                temporalgenero = "Pintura Paisajista";
                break;
            case "205":
                temporalgenero = "Pintura Histórica";
                break;
            case "Null - No Definido":
                temporalgenero = "Seleccione una opción";
                break;
        }

        genero_arte.setSelectedItem(temporalgenero);

        String sql1 = "Select * FROM examenjava.autor WHERE DNI = " + rutautor + ";";

        accesoADatos.Conexion cn = new accesoADatos.Conexion();
        cn.ejecuta(sql1);
        ResultSet autorrs = cn.consultaautor(sql1);


        while (autorrs.next()) {

            String nombreautor = autorrs.getString("NOMBRE");
            String apellidoautor = autorrs.getString("APELLIDO");
            String nacioautor = autorrs.getString("NACIONALIDAD");

            nombre_autor.setText(nombreautor);
            apellido_a.setText(apellidoautor);
            nacionalidad_a.setText(nacioautor);

        }


        accesoADatos.Conexion cc = new accesoADatos.Conexion();
        String sql3 = "Select * FROM examenjava.sala WHERE CODIGO_SALA = " + ubicacion + ";";
        ResultSet salars = cc.consultasala(sql3);

        while (salars.next()) {

            String clamps = salars.getString("CANTIDAD_LAMPARAS");
            String tempsala = salars.getString("TEMPERATURA");
            String cierresala = salars.getString("CIERRE_CENTRALIZADO");
            String alarmasala = salars.getString("ALARMA_INCENDIO");

            tempencargado = salars.getString("ENCARGADO");

            lamps_sala.setText(clamps);
            temp_sala.setText(tempsala);
            cierre_c_sala.setSelectedItem(cierresala);
            alarma_incendio_sala.setSelectedItem(alarmasala);

        }
        salars.close();
        accesoADatos.Conexion cx = new accesoADatos.Conexion();
        String sql2 = "Select * FROM examenjava.encargado WHERE DNI = " + tempencargado + ";";
        ResultSet encargadors = cx.consultaencargado(sql2);
        while (encargadors.next()) {

            //String dniencargado = encargadors.getString("DNI");
            String nombreencargado = encargadors.getString("NOMBRE");
            String profesionencargado = encargadors.getString("PROFESION");
            String añoencargado = encargadors.getString("AÑO_INGRESO");

            rut_encargado.setText(tempencargado);
            nombre_encargado.setText(nombreencargado);
            prof_encargado.setText(profesionencargado);
            año_in_encargado.setText(añoencargado);

        }
        encargadors.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}   

I need to add, this is the class where I manage the connections

public void ejecuta(String sql) throws SQLException {
    try {
        instruc.execute(sql);
    } catch (SQLException e) {
        System.out.println(e);
    }
}

public ResultSet consultaautor(String sql) throws SQLException {
    rs = instruc.executeQuery("SELECT * FROM  autor");
    return rs;
}

public ResultSet consultaencargado(String sql) throws SQLException {
    rs = instruc.executeQuery("SELECT * FROM  encargado");
    return rs;
}

public ResultSet consultasala(String sql) throws SQLException {
    rs = instruc.executeQuery("SELECT * FROM  sala");
    return rs;
}

public ResultSet consultaobraarte(String sql) throws SQLException {
    rs = instruc.executeQuery("SELECT * FROM  obra_arte");
    return rs;
}

public ResultSet consultaini() throws SQLException {
    rs = instruc.executeQuery("SELECT * FROM  obra_arte");
    return rs;
}

public ResultSet consultagenero() throws SQLException {
    rs = instruc.executeQuery("SELECT * FROM  genero");
    return rs;
}

public ResultSet consultatecnica() throws SQLException {
    rs = instruc.executeQuery("SELECT * FROM  tecnica");
    return rs;
}
    
asked by iFabio 08.07.2018 в 06:11
source

0 answers