(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();
}
}
}
}
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();
}
}
}
}