Run 3 queries in JAVA

3

I want to make 3 queries within a method in JAVA in order to assign a serial number to my record that has the ID number as consecutive, this is my code.

public void guardar()
{
   String erp = jComboBox1.getSelectedItem().toString();
   String cant = jComboBox2.getSelectedItem().toString();
   String estpack = jComboBox3.getSelectedItem().toString();
   Date fecha = jDateChooser1.getDate();
   String serie;
   int aidi;

   int cantpc = Integer.parseInt(cant) * Integer.parseInt(estpack); 
       try
       {
         Connection s = Conexiones.getConexion();
        Statement st = s.createStatement();
        st.executeQuery("insert into Embarques (ERP,CantCont,EstandarPack,FechaEmbarque,CantPiezas) values ('"+erp+"','1','"+estpack+"','"+new SimpleDateFormat("yyyy-MM-dd").format(fecha)+"','"+cantpc+"')");     
        }catch(SQLException e)
        {

        }
       try{
        Connection s2 = Conexiones.getConexion(); 

        Statement st2=s2.createStatement();
        ResultSet last_id = st2.executeQuery("SELECT MAX(ID) FROM Embarques");


        while(last_id.next())
        {
            aidi = last_id.getInt("ID");
            serie = "SMX-"+new SimpleDateFormat("yyyyMMdd").format(fecha)+"-"+aidi;

        Statement st3 = s2.createStatement();
        st3.executeQuery("UPDATE Embarques SET No_Serie ='"+serie+"' WHERE id='"+aidi+"'");
          }      
        }catch(SQLException e)
        {

        }      
   }
    
asked by Ing.Solís 13.09.2018 в 01:44
source

1 answer

0

I already solved it: Let my hardware take care of security. My problem was an error in the code, which resulted in the SQL query (name assignment to the column of my query). I do not want to modify this code even though it has vulnerabilities since it subsumes them with the hardware used. I solved it in the following way:

try //EJECUTAMOS LA INCERSIÓN A LA BD
       {
        Connection s = Conexiones.getConexion();// CREAMOS LA CONEXIÓN PARA LA INSERCIÓN DE DATOS
        Statement st = s.createStatement();
        st.executeQuery("insert into Embarques (ERP,CantCont,EstandarPack,FechaEmbarque,CantPiezas) values ('"+erp+"','1','"+estpack+"','"+new SimpleDateFormat("yyyy-MM-dd").format(fecha)+"','"+estpack+"')");     
       //EJECUTAMOS LA INCERCIÓN DEL NUEVO EMBARQUE SIN EL NÚMERO DE SERIE 
       }catch(SQLException e)
        {

        }
       try{//EJECUTAMOS LA CONSULTA Y ACTUALIZACIÓN EN LA BD
        Connection s2 = Conexiones.getConexion(); // CREAMOS CONEXIÓN PARA LA CONSULTA
        Statement st2 = s2.createStatement();
        // SE CONSULTA EL VALOR MÁXIMO DE ID EN LA TABLA EMBARQUE
        // CON EL PROPOSITO DE ASIGNAR EL NÚMERO DE SERIE CORRESPONDIENTE
        // POR LÓGICA SE OBTIENE EL ULTIMO VALOR INSERTADO
        // EL CUAL SE REALIZÓ EN LA SENTENCIA ANTERIOR
        // Y SE CONCATENA EL VALOR OBTENIDO EN LA PARTE FINAL DEL NÚMERO DE SERIE GENERADO

        rs = st2.executeQuery("SELECT MAX(ID) AS ID FROM Embarques");// CONSULTA DEL MÁXIMO ID SE LE ASIGNA NOMBRE AL RESULTADO PARA PODER SER ASIGNADO A UNA VARIABLE
        while(rs.next())// ENTRA EN EL CICLO MIENTRAS SE EJECUTA LA CONSULTA
        {

            Connection s3 = Conexiones.getConexion();// CREAMOS LA CONEXIÓN PARA LA ACTUALIZACIÓN DEL REGISTRO
            Statement st3 = s3.createStatement();
            serie = "SMX-"+date_full+"-"+rs.getInt("ID");   // SE CONCATENAN LOS DATOS PARA GENERAR EL NÚMERO DE SERIE
            st3.executeQuery("UPDATE Embarques SET No_Serie ='"+serie+"' WHERE id='"+rs.getInt("ID")+"'");// SE EJECUTA LA ACTUALIZACIÓN DEL 
        }//FIN WHILE

        }catch(SQLException e)//CATCH3
        {

        }//FIN CATCH3  

Greetings friends!

    
answered by 14.09.2018 в 06:03