---------------------------------------
-------------- BLOB ---------------
---------------------------------------
/**ORACLE 中 将 byte[] -> BLOB
---------------------------------------
---------------------------------------
final String SQL_INSERT = "insert into test ( pic,id ) values( empty_blob(), 1) ";
//其中empty_blob(),不能少
final String SQL_UPDATE = "select pic from test where id = 1 for update ";
con.setAutoCommit( false );
pstmt = con.prepareStatement( SQL_INSERT);
pstmt.executeUpdate();
pstmt = con.prepareStatement( SQL_UPDATE );
rs = pstmt.executeQuery();
oracle.sql.BLOB b = null;
while (rs.next())
{
b = (oracle.sql.BLOB) rs.getBlob(1);
}
rs.close();
// Updates blob.
// byte[] content ;
OutputStream os = b.getBinaryOutputStream();
ByteArrayInputStream is = new ByteArrayInputStream( content );
byte[] buffer = new byte[ b.getBufferSize() ];
int bytesRead = 0;
while( ( bytesRead = is.read( buffer ) ) != -1 ) os.write( buffer, 0, bytesRead );
os.close();
is.close();
con.commit();
/**ORACLE 中BLOB 类型的读取 BLOB -> OutputStream
---------------------------------------
---------------------------------------
InputStream in = null;
pstmt = con.prepareStatement(“ select pic from test where id =1 " );
rs = pstmt.executeQuery();
if ( rs.next() ) {
in = rs.getBinaryStream( "pic" );
byte[] b = new byte[1024 * 1024];
int len;
OutputStream outs = _Response.getOutputStream();
// 在servlet中用的response,适用于其它OutputStream
while ( ( len = in.read( b ) ) > 0 ) {
outs.write( b, 0, len );
}
in.close();
outs.flush();
outs.close();
}
// BLOB -> File
---------------------------------------
Blob b = rs.getBlob( Column_NO);
InputStream inputStream = b.getBinaryStream();
long length = b.length();
byte[] buffer = new byte[length ];
int lengthRead = 0;
String fileName = "wwww.gif";
FileOutputStream outputStream = new FileOutputStream( fileName );
while ((lengthRead = inputStream.read( buffer )) != -1) {
outputStream.write( buffer ,0, lengthRead );
}
inputStream.close();
outputStream.close();
// File -> BLOB
---------------------------------------
File file = new File( fileName );
int length = (int) file.length();
FileInputStream fileInputStream = new FileInputStream( file );
preparedStatement.setBinaryStream( 1, fileInputStream, length );
preparedStatement.execute();
---------------------------------------
-------------- CLOB -----------------
-----------------对于long row 也可以----
//String 类型的入库; String -> CLOB
---------------------------------------
String value = new String("…………");
Reader bodyReader = null;
String nullString = "";
if(value == null) value = nullString;
bodyReader = new StringReader(value);
pstmt.setCharacterStream(parameterIndex, bodyReader, value.length());
//String 类型的出库 CLOB -> String
---------------------------------------
Reader bodyReader = null;
String value = null;
//String 类型安标准来说没有长度限制,但是一般jdk中String的最大长度是4G
bodyReader = rs.getCharacterStream(columnIndex);
char [] buf = new char[256];
int len;
StringWriter out = new StringWriter(256);
while ((len = bodyReader.read(buf)) >= 0) {
out.write(buf, 0, len);
}
value = out.toString();
out.close();