来源:Oracle认证 发布时间:2012-11-12 Oracle认证视频 评论
fin.close();
System.out.println("read itotal::"+itotal);
/**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化
String mysql="insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";
opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);
opst.setString(1,"wordtemplate");
opst.setInt (2,flength);
opst.executeUpdate();
opst.clearParameters();
/**插入其它数据后,定位BLOB字段
mysql="select filebody from filelist where filename=?";
opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);
opst.setString(1,"wordtemplate");
ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();
if (ors.next())
{
oracle.sql.BLOB blob=ors.getBLOB(1);/**得到BLOB字段
int j=blob.putBytes(1,a);/**将字节数组写入BLOB字段
System.out.println("j:"+j);
db1.conn.commit();
ors.close();
}
System.out.println("insert into ok");
byte b[]=null;/**保存从BLOB读出的字节
opst.clearParameters();
mysql="select filebody from filelist where filename=?";
opst=(oracle.jdbc.driver.OraclePreparedStatement)db1.conn.prepareStatement(mysql);
opst.setString(1,"wordtemplate");
ors=(oracle.jdbc.OracleResultSet)opst.executeQuery();
if (ors.next())
{
oracle.sql.BLOB blob2=ors.getBLOB(1);
System.out.println("blob2 length:"+blob2.length());
b=blob2.getBytes(1,flength);/**从BLOB取出字节流数据
System.out.println("b length::"+b.length);
db1.conn.commit();
}
ors.close();
/**将从BLOB读出的字节写入文件
fout.write(b,0,b.length);
fout.close();
System.out.println("write itotal::"+b.length);
}
catch(Exception e)
{
System.out.println("errror :"+e.toString() );
e.printStackTrace();
}
finally
{ /**关闭所有数据联接
stmt.close();
db1.closeConn();
}
}
}
编译运行在TomCat下调试通过。
需要注意的是Blob存取的过程,一般先存入和BLOB相关的控制数据,如文件的名字,然后查询定位BLOB字段,利用OracleBlob提供的方法:
public int putBytes(long pos,byte bytes[])
public byte[] getBytes(long pos,byte bytes[])
或者利用
public OutputStream getBinaryOutputStream() throws SQLException
public InputStream getBinaryStream() throws SQLException
因为利用输入输出流总归还是利用到字节数组缓冲流,所以就不举例子了。
① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;
② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。