0

I've created a software using netbeans. now I want to add pictures to my database. I have created a table and changed the type as 'BLOB'. But, IDK how to code in java to do this. with this, I get a image and set it to a jLabel. now how to save this photo in mysql?

try {
            lbl_imge1.setIcon(null);
            jFileChooser1.showOpenDialog(this);
            BufferedImage upload = ImageIO.read(jFileChooser1.getSelectedFile());
            java.awt.Image photo = upload.getScaledInstance(lbl_imge1.getWidth(), lbl_imge1.getHeight(), java.awt.Image.SCALE_SMOOTH);
            lbl_imge1.setIcon(new ImageIcon(photo));
        } catch (Exception e) {
            e.printStackTrace();
        }

Now I am here,

try {
            jLabel1.setIcon(null);
            jFileChooser1.showOpenDialog(this);
            BufferedImage upload = ImageIO.read(jFileChooser1.getSelectedFile());
            java.awt.Image photo = upload.getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(), java.awt.Image.SCALE_SMOOTH);
            jLabel1.setIcon(new ImageIcon(photo));

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/crazy", "root", "123");

            BufferedImage buffered = ImageIO.read(jFileChooser1.getSelectedFile());

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(buffered, "jpg", baos);
            byte[] imageInByte = baos.toByteArray();

            Blob blob = con.createBlob();
            blob.setBytes(1, imageInByte);

            String query="INSERT INTO image VALUES ('"+jTextField1.getText()+"','"+blob+"')";
            PreparedStatement statement=con.prepareStatement(query);
user271865
  • 25
  • 1
  • 11

2 Answers2

2
  1. Take your BufferedImage

    BufferedImage buffered= ImageIO.read(jFileChooser1.getSelectedFile());
    
  2. Get a byte array from it (From this answer)

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(buffered, "jpg", baos );
    byte[] imageInByte = baos.toByteArray();
    
  3. Save byte array as blob (From this answer) (probably use a Prepared Statement)

    Blob blob = connection.createBlob();
    blob.setBytes(1, imageInByte);
    

UPDATE: connection is your database connector i.e:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Can you please describe the 3rd step? what is 'connection' ? netbeans gives me errors. – user271865 Apr 01 '15 at 09:02
  • Is your connection to database! I.E: `Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");` I updated the answer ;) – Jordi Castilla Apr 01 '15 at 09:06
  • It depends of how you manage the relation btw your entitys and database... do you have `Hibernate Mappings` with `EntityManager`? maybe a simple `HQL` query is enough for you, do it as you do in other cases... If no, `PreparedStatement` as suggested in answer... `String query = "insert into table(......)"; PreparedStatement ps=connection.prepareStatement(query);` – Jordi Castilla Apr 01 '15 at 09:31
  • Statement statement=con.createStatement(); statement.executeUpdate("INSERT INTO image VALUES ('"+jTextField1.getText()+"','"+blob+"')"); this saves a memory address. Is that ok? if yes, how to get it back as an image? – user271865 Apr 01 '15 at 09:34
  • now i tried the same query with PreparedStatement. but nothing happens. nothing saves in database. – user271865 Apr 01 '15 at 09:39
  • Are you sure your `blob` is created correctly? If possible, please update your question to check in which point are you now :) – Jordi Castilla Apr 01 '15 at 11:00
0

You can save any byte array to a blob column. I know that this is possible with Oracle and this also should work with MySql.
Here is a small example how to do a insert with java code:

String sql = "insert into yourtable(id,binaryImage) values (?,?)";
PreparedStatement pstmt =dbConnection.prepareStatement(sql);

ByteArrayInputStream bais = new ByteArrayInputStream(your_byte_array);
pstmt.setString(1, "your_id");
pstmt.setBinaryStream(2, bais);
pstmt.execute();
pstmt.close();
chris
  • 1,685
  • 3
  • 18
  • 28