Convert Bufferedimage to BLOB - DERBY - NETBEANS JAVA

Asked

Viewed 51 times

0

I created an object with several parameters and all data is entered inside the database except the last of type BufferedImage.

I have already converted it to bytes (or at least think). It is a jpg.

Following the commands used:

public static void inserirProduto(Produto pro) throws SQLException, IOException{
        estabeleceConexao();

        BufferedImage buff = new BufferedImage(pro.gg.getWidth(),pro.gg.getHeight(),java.awt.image.BufferedImage.TYPE_INT_RGB);
        buff.getGraphics().drawImage(buff, 0, 0, null);

        ByteArrayOutputStream baos  = new ByteArrayOutputStream();
        ImageIO.write(buff, "jpg", baos);

        byte[] ImageInByte = baos.toByteArray();

        Blob blob = conexao.createBlob();
        blob.setBytes(1, ImageInByte);


                    sql = "INSERT INTO PRODUTO VALUES ('" + pro.codigo + "','"
                                                          + pro.nome_produto + "','"
                                                          + pro.categoria + "',"
                                                          + pro.quantidade + ","
                                                          + pro.preco + ",'"
                                                          + pro.fabricante + "','"
                                                          + pro.responsavel + "','"
                                                          + pro.telefone + "','"
                                                          + pro.email + "','"
                                                          + pro.informacoes + "',"
                                                          + blob.getBytes(1,(int)blob) + ")";

                if (stmt.executeUpdate(sql) > 0 )//Se a execução voltar maior que zero significa que dados entraram no banco
                    JOptionPane.showMessageDialog(null, "Produto inserido com sucesso!!");   
                else
                    JOptionPane.showMessageDialog(null, "Erro ao gravar dados!","Erro",0);
    }

The problem is on the line of blob.getBytes. I don’t know if the problem is exactly there, it’s the first time having to save an image in the bank.

NOTE: The image is already instantiated inside the object in question.

  • And what is the language?

  • JAVA - ide netbans

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.