數據庫clob轉字符串,大字段 CLOB/BOLB與String互轉

 2023-11-18 阅读 29 评论 0

摘要:String s1="1234abc我是大字段CLOB或BOLB"; String轉大字段 //String 轉 clob Clob c = new SerialClob(s1.toCharArray()); //String 轉 blob Blob b = new SerialBlob(s1.getBytes("GBK")); //也可以這樣不傳字符集名稱,使用系統默認的
String s1="1234abc我是大字段CLOB或BOLB"; 
  1. String轉大字段
//String 轉 clob   
Clob c = new SerialClob(s1.toCharArray()); 
//String 轉 blob    
Blob b = new SerialBlob(s1.getBytes("GBK"));
//也可以這樣不傳字符集名稱,使用系統默認的
//Blob b = new SerialBlob(s1.getBytes());
  1. 大字段轉String
//clob 轉 String  
String clobStri = c.getSubString(1, (int) c.length());	
//blob 轉 String      
String blobString = new String(b.getBytes(1, (int) b.length()),"GBK");	
//前面若沒傳入字符集名稱,則這里也不需要傳入,以免出錯
//String blobString = new String(b.getBytes(1, (int) b.length()));        
System.out.println(clobString);        
System.out.println(blobString);
  • 轉換函數
public static String convertToString(Object o){if(o == null)return null;if(o instanceof String){return (String)o;}else if(o instanceof Integer){return o.toString();}else if(o instanceof SerializableClob){SerializableClob clob = (SerializableClob) o ;try {return clob.getSubString(1, (int) clob.length());} catch (SQLException e) {throw new RuntimeException(e);}}return o.toString();}

這種數據庫的字段類型轉成Java字段類型的行為,從框架上來說叫做orm(object-relative-model)對象關系模型

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/178272.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息