当前位置:   article > 正文

Oracle数据库Clob类型Blob类型与String互转_oracle clob转string

oracle clob转string

Clob类型为大字段类型默认4000长度,Blob为二进制类型常用存文件数据;这两种大字段类型开发中不会经常遇到,博主整理了这两种大字段与String的转换方法可参考(使用Junit直接测试)其中javax.sql.rowset.serial.SerialClob 和javax.sql.rowset.serial.SerialBlob两个类都是从JDK1.5引入;用到了JDK1.7语法:try...with ,要流实现了Closeable接口就可以使用

1.Blob与String互转

  1. @Test
  2. public void testGetBlob() {
  3. String str ="aaabbbccc";
  4. Blob blob = null;
  5. try {
  6. blob= new javax.sql.rowset.serial.SerialBlob(str.getBytes());
  7. } catch (SQLException e) {
  8. e.printStackTrace();
  9. }
  10. // blob 转 String
  11. try(InputStream inputStream = blob.getBinaryStream()){
  12. byte[] bs = new byte[2048];
  13. StringBuffer sb = new StringBuffer();
  14. int len =0;
  15. while((len=inputStream.read(bs))!=-1) {
  16. sb.append(new String(bs,0,len));
  17. }
  18. System.out.println("blob to String >>"+sb.toString());
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } catch (SQLException e1) {
  22. e1.printStackTrace();
  23. }
  24. }

2.Clob与String互转:

  1. @Test
  2. public void testGetClob() {
  3. //String转clob
  4. String str ="aaabbbccc";
  5. Clob clob = null;
  6. try {
  7. clob = new javax.sql.rowset.serial.SerialClob(str.toCharArray());
  8. } catch (SQLException e1) {
  9. e1.printStackTrace();
  10. }
  11. // clob 转String
  12. try(Reader rsReader = clob.getCharacterStream()){
  13. char [] chs = new char[2048];
  14. StringBuffer sb = new StringBuffer();
  15. int i =0;
  16. while((i=rsReader.read(chs))!=-1) {
  17. sb.append(chs,0,i);
  18. }
  19. System.out.println("clob to string >>"+sb.toString());
  20. }catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号