Java 中使用Oracle的Blob类型

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值