jdbc 2.0中的BLOB和CLOB接口提供了一种从数据库中获取数据或写数据到数据库的手段,这个手段是通过从数据库中获得一个流(输入或者输出)对象.并从该流中读取数据或写入.
例:
OutputStream out=null;
BufferedInputStream in=null;
File file=new File("****");
ReslutSet rset=statement.executeQuery(sql);//从查询语句中取得一个结果集
if(rset.next())
{
Blob blob=rset.getBlob(1);
out=((oracle.sql.Blob)blob).getBinaryOutputStream();//jdbc 2.0不支持写数据到blob,因此我们用Oracle的扩展
int bufferSize==((oracle.sql.Blob)blob).getBufferSize();
in=new BufferedInputStream(new fileInputStream(file),bufferSize);
byte[] b=new byte[bufferSize];
int count=in.read(b,0,bufferSize);
//开始存储数据到数据库中
while(cout!=-1)
{
out.write(b,o,count);
cout=in.read(b,o,bufferSize);
}
//数据写完
out.close();
in.close();
connection.commit();//提交改变
........
}
类似的,我们可以从blob中得到一个输入流,把blob数据写入到文件中去
InputStream in=blob.getBinaryStream();
int bufferSize =((oracle.sql.Blob)blob).getBufferSize();
例:
OutputStream out=null;
BufferedInputStream in=null;
File file=new File("****");
ReslutSet rset=statement.executeQuery(sql);//从查询语句中取得一个结果集
if(rset.next())
{
Blob blob=rset.getBlob(1);
out=((oracle.sql.Blob)blob).getBinaryOutputStream();//jdbc 2.0不支持写数据到blob,因此我们用Oracle的扩展
int bufferSize==((oracle.sql.Blob)blob).getBufferSize();
in=new BufferedInputStream(new fileInputStream(file),bufferSize);
byte[] b=new byte[bufferSize];
int count=in.read(b,0,bufferSize);
//开始存储数据到数据库中
while(cout!=-1)
{
out.write(b,o,count);
cout=in.read(b,o,bufferSize);
}
//数据写完
out.close();
in.close();
connection.commit();//提交改变
........
}
类似的,我们可以从blob中得到一个输入流,把blob数据写入到文件中去
InputStream in=blob.getBinaryStream();
int bufferSize =((oracle.sql.Blob)blob).getBufferSize();
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货