Save in BBDD the selected row of a JTABLE

2

I have a small problem ... I'm trying to make my JButton "Save" save to BD only those selected rows of my JTable. the code that I have is the following:

    button = new JButton("Guardar");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            connect cc = new connect();
            Connection cn = cc.conectar();


            try{
                int[] filasSelec = table.getSelectedRows();
                //for(int i = 0; i <= table.getSelectedRow(); i++)
                for( int i = 0; i < filasSelec.length; i++) {
                    String id = (String) table.getValueAt(filasSelec[i], 0);
                    System.out.println(id);
                {
                    PreparedStatement pst = cn.prepareStatement("INSERT INTO persona(pk_ci,nombre,apellido,tipo,telefono,contrasena,Estado) VALUES (?,?,?,?,?,?,?)");
                    pst.setString(1, table.getValueAt(i,0).toString());
                    pst.setString(2, table.getValueAt(i,1).toString());
                    pst.setString(3, table.getValueAt(i,2).toString());
                    pst.setString(4, table.getValueAt(i,3).toString());
                    pst.setString(5, table.getValueAt(i,4).toString());
                    pst.setString(6, table.getValueAt(i,5).toString());
                    pst.setString(7, table.getValueAt(i,6).toString());
                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(null,  "dato guardado correctamente");
                }
                }}catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "Error: "+e1.getMessage());
            }
        }
    });

My jtable loads the existing users in the database, to add a new user, create a button that adds a blank row to the jtable and fill it with the new user's data. By giving the JButton "save" it does not save and returns the error of "duplicate key for (a user number) for key primary" what happens is that when you click on the save button it starts reading the JTable from the beginning, that is to say about the users that already exist in the database and not about the row that I have selected that would be the new user

    
asked by Macali 17.09.2018 в 22:39
source

1 answer

1
for( int i = 0; i < filasSelec.length; i++) {
   // Esto lo haces bien. La i-fila seleccionada no es i, es filasSelec[i]
   String id = (String) table.getValueAt(filasSelec[i], 0);
   System.out.println(id);
   // Esta llave sobra. No hace daño, pero lía al leer el código
   {  
       PreparedStatement pst = cn.prepareStatement("INSERT INTO persona(pk_ci,nombre,apellido,tipo,telefono,contrasena,Estado) VALUES (?,?,?,?,?,?,?)");
       // Aquí, lees el dato de la fila i, en vez de filasSelec[i]
       pst.setString(1, table.getValueAt(i,0).toString()); 

If you have F rows and you select S, filasSelec will be an array of S positions. But i will go from 0 to S-1; to know which are the selected rows you will have to do filasSelec[i]

    
answered by 17.09.2018 / 23:19
source