在java 中处理oracle中的大对象

(CLOB和BLOB数据类型):
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.Writer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.sql.BLOB;
import oracle.sql.CLOB;

public class Test ... {

/***//**
*
@paramargs
*/

publicstaticvoidmain(String[]args)...{
//TODOAuto-generatedmethodstub
System.out.println("ProcessStart...");

testClob();
}


publicstaticvoidtestClob()...{
Connectioncon
=null;
PreparedStatementpst
=null;
ResultSetrs
=null;

Stringstrsql
="truncatetabletest";
Stringstrsql1
="insertintotestvalues('test',empty_clob(),empty_blob())";
Stringstrsql2
="updatetestsetbigobject1=?,bigobject2=?wheretestid=?";
Stringstrsql3
="select*fromtestwheretestid='test'forupdate";
StringstrLong
=StringLong.strLong;

System.out.println(
"strLong.length():"+strLong.length());
//Filefile=newFile("E:/pic/2006_10_02/IMG_0087.JPG");
Filefile=newFile("D:/2007.wmv");

con
=ConnDB.getConnection66();

try...{
con.setAutoCommit(
false);

pst
=con.prepareStatement(strsql);
pst.executeUpdate();

pst
=con.prepareStatement(strsql1);
pst.executeUpdate();

pst
=con.prepareStatement(strsql3);
rs
=pst.executeQuery();

while(rs.next())...{

longstime=System.currentTimeMillis();

BLOBbb
=(BLOB)rs.getBlob(3);
CLOBcb
=(CLOB)rs.getClob(2);

/***//************************CLOBReaderString*************************/
Writerwt
=cb.getCharacterOutputStream();
wt.write(strLong);
wt.flush();
wt.close();
/***//************************CLOBReaderString*************************/

/***//************************CLOBReaderFile*************************/
//BufferedWriterbw=newBufferedWriter(cb.getCharacterOutputStream());
//BufferedReaderbfr=newBufferedReader(newFileReader("D:/test.txt"));
//intread=0;
//while((read=bfr.read())!=-1){
//bw.write(read);
//}
//bw.flush();
//bw.close();
/***//************************CLOBReaderFile*************************/


/***//************************BLOBReaderFile*************************/
OutputStreamoutstrm
=bb.getBinaryOutputStream();
FileInputStreaminpstrm
=newFileInputStream(file);
byte[]buffer=newbyte[inpstrm.available()];
inpstrm.read(buffer);
outstrm.write(buffer);
outstrm.flush();
inpstrm.close();
outstrm.close();
/***//************************BLOBReaderFile*************************/

pst
=con.prepareStatement(strsql2);
pst.setClob(
1,cb);//Clob
pst.setBlob(2,bb);//Blob
pst.setString(3,"test");
pst.executeUpdate();

longetime=System.currentTimeMillis();

System.out.println(
"ExecuteTime:"+(etime-stime)/1000.0);

}

con.commit();

/***//************************OutputCLOBStringFromDB*************************/
pst
=con.prepareStatement("select*fromtest");
rs
=pst.executeQuery();
if(rs.next())...{
CLOBclb
=(CLOB)rs.getClob(2);
BufferedReaderbr
=newBufferedReader(clb.getCharacterStream());
Strings
=br.readLine();
while(s!=null)...{
System.out.println(s);
s
=br.readLine();
}

br.close();
}

/***//************************OutputCLOBStringFromDB*************************/

System.out.println(
"ProcessEnd!");

}
catch(SQLExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
catch(FileNotFoundExceptione1)...{
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
catch(Exceptionex)...{
ex.printStackTrace();
}
finally...{
try...{
pst.close();
con.close();
}
catch(SQLExceptione)...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值