Olá pessoal, ja pesquisei praticamente em todos os fóruns a respeito de salvar imagem no banco, mas infelizmente nao consegui =( to quase desistindo ja, mas resolvi postar aqui pra ver se alguem pode me ajudar. gostaria de converter a imagem em bytes e depois salva-la no banco, porem nao estou conseguindo fazer isso, segue meus codigos:
Esse codigo é para capturar a imagem no desktop do usuario
Por favor, ao postar tópicos, não use SOMENTE LETRAS MAIÚSCULAS nos títulos.
Quanto ao seu problema… geralmente é melhor salvar a imagem no sistema de arquivos (com ImageIO.write) e salvar no banco de dados apenas uma String com um link para essa imagem.
Eu também não usaria ImageIcon, mas a classe BufferedImage direto:
Se você realmente precisar salvar a imagem no banco, você pode transforma-la num array de Bytes passando para o ImageIO.write um ByteArrayOutputStream:
Viny obrigada por responder, mas a nao to conseguindo exibir a classe BuffereImage no meu jlabel, e como faço para passar os bytes para meu banco? Desculpe sou iniciante ainda =/
Connection con;
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/videoaulajdbc", "root", "96512169");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", bos);
byte[] imgData = bos.toByteArray();
String query = "INSERT INTO cadastrousuario (imagem) VALUES (?)";
PreparedStatement stmt = con.prepareStatement(query);
stmt.setBytes(1, imgData);
stmt.executeUpdate();
stmt.close();
con.close();
JOptionPane.showMessageDialog(null,"Cadastrado com sucesso");
} catch (IOException ex) {
Logger.getLogger(imagem.class.getName()).log(Level.SEVERE, null, ex);
}catch(ClassNotFoundException ex){
JOptionPane.showMessageDialog(null,"Não foi possivel encontrar a classe");
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"Ocorreu um erro de SQL");
}
[/code]
Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
at javax.imageio.ImageIO.write(ImageIO.java:1578)
at testes.imagem.jButton1ActionPerformed(imagem.java:176)
at testes.imagem.access$100(imagem.java:29)
at testes.imagem$2.actionPerformed(imagem.java:80)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)