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); 

2 Answers 2

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"); 
Sign up to request clarification or add additional context in comments.

6 Comments

Can you please describe the 3rd step? what is 'connection' ? netbeans gives me errors.
Is your connection to database! I.E: Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); I updated the answer ;)
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);
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?
now i tried the same query with PreparedStatement. but nothing happens. nothing saves in database.
|
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(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.