Convert Object.BufferedImage to BLOB - DERBY - JAVA NETBEANS

0

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

I've already converted it to bytes (or at least I guess). This 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 in the blob.getBytes line. I do not know if the problem is exactly there, it is the first time having to save an image in the bank.

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

    
asked by anonymous 26.11.2017 / 21:03

0 answers