Skip to content
Advertisement

how to update multiple records with jdbc with oracle and spring boot

I am trying to update several records from spring jdbc but this is not working what am i doing wrong?

does not respond when I make a request, but the data in the array is arriving, try without array and the same thing happens.

I am sending an array of objects to be able to update but I get to the method cstmt.executeQuery(); it does not execute and it waits and does not go any further.

@PostMapping(path = "/updateEstadoPlanesServicios", produces = MediaType.APPLICATION_JSON)
    public String updateEstadoPlanesServicios(@RequestBody String lista)  {             
        
        ServiciosPlanesUpdateDTO[] fromJson = gson.fromJson(lista, ServiciosPlanesUpdateDTO[].class);
        
          
            return gson.toJson(consultaPlanesComisionPortal.estadoPlanesServicios(fromJson));
        
    }
    @Transactional(rollbackFor = { Exception.class })
    public replyDTO estadoPlanesServicios(ServiciosPlanesUpdateDTO[] list) {
        System.out.println("data: "+list.toString());
        replyDTO  re = new replyDTO();
        int count = 0;

        try {
            
            
            StringBuilder update = new StringBuilder();
            update.append("UPDATE Detalleproductoservicio ");
            update.append(" SET loginregistro  =  ?,  estado  =  'D',  fechasys  =  sysdate ");
            update.append(" WHERE  codigo_Servicio  =  ?  and  codigo_planproductoservicio  =  ? and  NIT  =  ?");
            
            try (Connection conexion = obtenerConexion.obtenerConexion(0);
                    PreparedStatement cstmt = conexion.prepareStatement(update.toString())) {
                conexion.setAutoCommit(false);
                
                for(ServiciosPlanesUpdateDTO elements: list) {
                    //System.out.println(elements.toString());
                    System.out.println("1 element:"+elements.getLoginRegistro());
                    System.out.println("2 element:"+elements.getCodigoServicio());
                    System.out.println("3 element:"+elements.getCodigoPlanProductoServicio());
                    System.out.println("4 element:"+elements.getNit());
                    cstmt.setString(1, elements.getLoginRegistro().trim());
                    cstmt.setInt(2,Integer.parseInt( elements.getCodigoServicio().trim()));
                    cstmt.setString(3, elements.getCodigoPlanProductoServicio().trim());
                    cstmt.setString(4, elements.getNit().trim());   
                    
                    count = cstmt.executeUpdate();
                    //cstmt.execute();
                    //count++;
                    //cstmt.addBatch();
                    //cstmt.executeBatch();
                    //
                    
                }
                
                conexion.commit();
                if(count > 0) {
                    re.setMessage("Status ok,"
                            +"count: "+count);
                    re.setExitoso(true);
                }else {
                    re.setExitoso(false);
                    re.setMessage("failed");
                }
                
            }

        } catch (Exception  e ) {
            
            re.setExitoso(false);
            re.setMessage(e.getMessage());
        }
        return re;
    }
    

Advertisement

Answer

this is working I found the problem apparently the database conflicts when I have Oracle SQL Developer open and I make the request by postman this is a little weird but I closed Oracle Developer and it worked.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement