java - Inserting values into BLOB type in JDBC -
i have several columns of blob type in database. not these columns needed filled since contain fingerprint templates. there cases users amputated thus, not columns used.
in situation, value should insert in database? code:
public void insertlefthandbio(bio bio, string id) { try { string query = "insert fingerprint_left_tbl values(?,?,?,?,?,?,?,?,?,?,?,?)"; ps = super.getconnection().preparestatement(query); ps.setint(1, 0); ps.setstring(2, id); //left hand //left thumb file file = new file("left hand thumb.jpg"); inputstream stream = (inputstream) new fileinputstream(file); ps.setbytes(3, bio.getlefthandthumbtemplate().serialize()); ps.setbinarystream(4, stream); //left index file = new file("left hand index.jpg"); stream = (inputstream) new fileinputstream(file); ps.setbytes(5, bio.getlefthandindextemplate().serialize()); ps.setbinarystream(6, stream); //left middle file = new file("left hand middle.jpg"); stream = (inputstream) new fileinputstream(file); ps.setbytes(7, bio.getlefthandmiddletemplate().serialize()); ps.setbinarystream(8, stream); //left ring file = new file("left hand ring.jpg"); stream = (inputstream) new fileinputstream(file); ps.setbytes(9, bio.getlefthandringtemplate().serialize()); ps.setbinarystream(10, stream); //left little file = new file("left hand little.jpg"); stream = (inputstream) new fileinputstream(file); ps.setbytes(11, bio.getlefthandlittletemplate().serialize()); ps.setbinarystream(12, stream); int = ps.executeupdate(); if (i < 0) { system.out.println("bio inserted"); } } catch (sqlexception ex) { ex.printstacktrace(); } catch (ioexception ex1) { ex1.printstacktrace(); } super.cleanupresources(); }
thanks!
you need insert null in database, simple check enough before calling methods throw exceptions in java program. example left hand thumb:
if (bio.getlefhandthumbtemplate() != null) { file file = new file("left hand thumb.jpg"); inputstream stream = new fileinputstream(file); ps.setbytes(3, bio.getlefthandthumbtemplate().serialize()); ps.setbinarystream(4, stream); } else { ps.setnull(3, types.blob); ps.setnull(4, types.blob); }
to avoid duplicate code (5 times 2 hands) suggest put inside fingerprint
class (or whatever called).
Comments
Post a Comment