add a jpg file to mysql DB in java -
i have field in mysql table set data type 'blob'. how can insert jpg file table in java code? mean's how can read jpg file , use in insert expression? use follow instructions read jpg file:
file fi = new file("c:\\users\\kamyar\\desktop\\i.jpg"); byte[] filecontent = files.readallbytes(fi.topath());
and using following code insert table:
class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:3306/server1", "root", "admin"); statement stmt=con.createstatement(); stmt.execute("insert users values( 'salar5' , '1234' , 'describe' ,'" + **here** + "' , 88 , 99 , '1/1/93' );"); con.close();
i don't know how can use file in insert expression. thanks...
what can create preparedstatement instead of statement object, , can bind values need statement need them.
for blobs, 1 thing can use binary stream.
preparedstatement ps = con.preparestatement("insert statement"); inputstream = new fileinputstream(fi); ps.setbinarystream(n, is, filecontent.length);
where n index in prepared statement, in case 3 if bind every value.
Comments
Post a Comment