How to add a photo to mysql from java? -
i've created software using netbeans. want add pictures database. have created table , changed type 'blob'. but, idk how code in java this. this, image , set jlabel. how save 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 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 image values ('"+jtextfield1.gettext()+"','"+blob+"')"; preparedstatement statement=con.preparestatement(query);
take bufferedimage
bufferedimage buffered= imageio.read(jfilechooser1.getselectedfile());
get byte array (from answer)
bytearrayoutputstream baos = new bytearrayoutputstream(); imageio.write(buffered, "jpg", baos ); byte[] imageinbyte = baos.tobytearray();
save byte array blob (from answer) (probably use prepared statement)
blob blob = connection.createblob(); blob.setbytes(1, imageinbyte);
update: connection database connector i.e:
connection connection = drivermanager.getconnection("jdbc:mysql://localhost:3306/test", "root", "root");
Comments
Post a Comment