error in java when saving image in mysql

0

in the following code, the insert dataData works perfectly, but the editData method is giving me a headache, I do not find it back

 public int insertDatos(String nom,String apell,String com,String
     pos,String emai,String tele,String not, FileInputStream[] fot)throws FileNotFoundException{

   int res = 0;
   File file = new File(ruta);
   // FileInputStream fis = new FileInputStream(file);
    try{
        PS = CN.getConnection().prepareStatement(QUERY_INSERT);
        PS.setString(1, nom);
        PS.setString(2, apell);
        PS.setString(3, com);
        PS.setString(4, pos);
        PS.setString(5, emai);
        PS.setString(6, tele);
        PS.setString(7, not);


        byte[] icono = new byte[(int) file.length()];
        InputStream input = new FileInputStream(file);
        input.read(icono);
        //PS.setFoto(icono);
        PS.setBytes(8, icono);



        res =PS.executeUpdate();


        if(res > 0){
            JOptionPane.showMessageDialog(null, "Registro guardado exitosamente");
        }
    }catch(Exception e){
        System.out.println("Error al guardar los datos en la BD" + e.getMessage());
    }finally{
        PS=null;
        CN.close();
    }
    return res;

}

public int editarDatos(String id,String nom,String apell,String com,String pos,String emai,String tele,String not, FileInputStream[] fot) throws FileNotFoundException{
    String SQL = "update cliente set nombre='"+nom+"', apellido='"+apell+"', compañia='"+com
            +"', posicion='"+pos+"', email='"+emai+"', telefono='"+tele+"', notas='"+not+"', foto='"+fot+"' where id="+id;
   File file = new File(ruta);
    int res = 0;
    try{
       byte[] icono = new byte[(int) file.length()];
        InputStream input = new FileInputStream(file);
        input.read(icono);
        //PS.setFoto(icono);
        //PS.setBytes(8, icono);


        PS = CN.getConnection().prepareStatement(SQL);
        res =PS.executeUpdate();
        if(res > 0){
            JOptionPane.showMessageDialog(null, "Registro editado exitosamente");
        }
    }catch(Exception e){
        System.out.println("Error al editar los datos en la BD" + e.getMessage());
    }finally{
        PS=null;
        CN.close();
    }
    return res;

}

Do I have to match the icon variable to fot? so that I can save the image in mysql. The insertData method even saves me a file which I am able to open in phpmyadmin, but, editing data keeps a rare file which is not equal in size to the original

All the other registers are fine, but, fot I get it wrong

Finally I leave the code of the edit method, in which I call a variable num which if it is equal to zero is a new entry and if it is distant to zero it is an entry to edit

 private void editar() throws IOException{

   // File file = new File(ruta);
    //FileInputStream fis = new FileInputStream(file);

    String id = jtf_id.getText();
    String nom = jtf_nombre.getText();
    String apell = jtf_apellido.getText();
    String com = jtf_compañia.getText();
    String pos = jtf_posicion.getText();
    String emai = jtf_email.getText();
    String tele = jtf_telefono.getText();
    String not = jta_notas.getText();
    FileInputStream[] fot = new FileInputStream[(jtf_foto.getText().getBytes()).length];

    if (num == 0){

        int resp = insertDatos(nom, apell, com, pos, emai, tele, not, fot);
        if (resp > 0){
        verContactos();
        limpiarTF();
        }

    }else{
        int resp = editarDatos(id, nom, apell, com, pos, emai, tele, not, fot);
    if (resp > 0){
        verContactos();
        limpiarTF();
        num=0;

    }
    }
}        
    
asked by Barly Espinal 07.08.2018 в 20:27
source

1 answer

3

In the editarDatos method, you are not using the prepared query correctly. I do not understand why you do not implement the same procedure as in insertDatos , unlike that you must pass as last parameter the id because the last value is used in where :

public int editarDatos(String id,String nom,String apell,String com,String pos,String emai,String tele,String not, FileInputStream[] fot) throws FileNotFoundException{
    String SQL = "update cliente set nombre=?, apellido=?, compañia=?, posicion=?, email=?, telefono=?, notas=?, foto=? where id=?";
   File file = new File(ruta);
    int res = 0;
    try{
       byte[] icono = new byte[(int) file.length()];
        InputStream input = new FileInputStream(file);
        input.read(icono);

        PS = CN.getConnection().prepareStatement(SQL);
        PS.setString(1, nom);
        PS.setString(2, apell);
        PS.setString(3, com);
        PS.setString(4, pos);
        PS.setString(5, emai);
        PS.setString(6, tele);
        PS.setString(7, not);
        PS.setBytes(8, icono);
        PS.setString(9, id);

        res =PS.executeUpdate();
        if(res > 0){
            JOptionPane.showMessageDialog(null, "Registro editado exitosamente");
        }
    }catch(Exception e){
        System.out.println("Error al editar los datos en la BD" + e.getMessage());
    }finally{
        PS=null;
        CN.close();
    }
    return res;

}
    
answered by 07.08.2018 в 20:57