java - BLOB column empty after JDBC insertion -
i have code (actually, simplified version of it, obv.)
private streamandstatement getstatement(file f, string fid, long dex, string uid, int id) { fileinputstream fis = null; preparedstatement statement; statement = connection.preparestatement("insert blobtable (fid, fdex, sfile, uid, id) values (?, ?, ?, ?, ?)"); statement.setstring(1, fid); statement.setlong(2, dex); fis = new fileinputstream(file); statement.setbinarystream(3, fis, file.length()); statement.setstring(4, uid); statement.setint(5, id); homecoming new streamandstatement(statement, fis); } private insertstuff() { file f = new file("/home/user/thisfileexists"); streamandstatement sandstatement = getstatement(f, "xyz", 18l, "abc", 78); sandstatement.getstatement().execute(); sandstatement.closestream(); }
streamandstatement straightforward , obvious might imagine.
now whole code runs without oracle yelling @ me blob ends empty...
i not messing stream @ point in between , see, getstatement creates stream , execution of statement in 2 consecutive lines no tempering...
we first insert record empty blob, go in update mode setting content. far know classical approach. code can next (dbutil give utility methods):
int insertblobgettingid(connection con, int idupload, inputstream streamfile) { int idblob = dbutil.getuniqueidentifier("seq_uploaded_blob", con); string insertemptyblob = "insert uploaded_blob (id, file_content) values ("+idblob+", empty_blob())"; dbutil.executesql(insertemptyblob, con); if(streamfile != null) { string sql = "select file_content uploaded_blob id = "+idblob+" update "; oracle.jdbc.oracleresultset rs = null; oracle.sql.blob blob = null; try{ rs = (oracle.jdbc.oracleresultset)dbutil.openresultset(sql, con); if(rs.next()){ blob = rs.getblob("file_content"); int chunksize = blob.getchunksize(); byte[] binarybuffer = new byte[chunksize]; int bytesread; int position = 1; while ((bytesread = streamfile.read(binarybuffer)) != -1) { blob.putbytes(position, binarybuffer, bytesread); position += bytesread; } } else { throw new runtimeexception("id "+idblob+" not found"); } }catch(exception e){ throw new runtimeexception(e); }finally{ dbutil.closeresultset(rs); } } homecoming idblob; }
java oracle jdbc blob
No comments:
Post a Comment