Save GIF in PostgreSQL database

0

Good afternoon I need some help, what I need is to save a GIF in a postgreSQL database. I have a table called words which counts of 4 fields ...

  

word of type text
description of type text
GIF of type text
  Bytea type resource

In the byte column I must save the GIF. I'm using java to do this.

When I send to save the selected GIF from the program that I am developing, it is saved correctly in the database. The problem is that when I want to recover that data and show it in a jlabel, it is shown as an image and not as a GIF.

The code to save is the following:

try {
    String sql = "INSERT INTO public.palabras(palabra, \"descripción\", gif, recurso) VALUES (?, ?, ?, ?);";
    String tipo, descrp, ruta;
    tipo = view.txt_tipo.getText();
    descrp = view.txt_desc.getText();
    ruta = ruta_archivo;
    PreparedStatement ps = c.getConexion().prepareStatement(sql);
    ps.setString(1, tipo);
    ps.setString(2, descrp);
    ps.setString(3, tipo+".GIF");
    ps.setBinaryStream(4, fileInputStream, longitudByte);
    ps.execute();
    ps.close();
    JOptionPane.showMessageDialog(null, "Se ha guardado correctamente en la base de datos");
} catch (SQLException ex) {
    Logger.getLogger(Controller_Guardar.class.getName()).log(Level.SEVERE, null, ex);
}

To recover use the following code:

String sql = "SELECT palabra, \"descripción\", gif, recurso FROM public.palabras WHERE palabra='" + view.text.getText() + "';";
System.out.println(sql);
ImageIcon foto;
InputStream inputStream;
String palabra;
String descripcion;
String gif;
try {
    ResultSet rs = c.Consulta(sql);
    while (rs.next()) {
        palabra = rs.getString(1);
        descripcion = rs.getString(2);
        gif = rs.getString(3);
        inputStream = rs.getBinaryStream(4);

        BufferedImage bi = ImageIO.read(inputStream);
        foto = new ImageIcon(bi);
        Image img = foto.getImage();
        byte[] ggg = getImageAsGIF(bi);
        ImageIcon iii = new ImageIcon(ggg);
        Icon Icono1 = new ImageIcon(iii.getImage().getScaledInstance(view.labelGifts.getWidth(), view.labelGifts.getHeight(), Image.SCALE_DEFAULT));
        view.labelGifts.setIcon(Icono1);
    }
} catch (Exception ex) {
    JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
}

I hope you can help me. Greetings

    
asked by user9378857 01.08.2018 в 22:25
source

0 answers