`
Dxx23
  • 浏览: 140982 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Blob、Clob、String的对象相关操作

阅读更多
大型对象
BLOB就是使用二进制保存数据。
如:保存位图。
CLOB使用CHAR来保存数据。
如:保存XML文档。

1.读取Blob、Clob对象
Connection con = null;
	try {
	Class.forName("oracle.jdbc.OracleDriver");
     con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "username","password");
	con.setAutoCommit(false);
	Statement st = con.createStatement();
      PreparedStatement ps = con.prepareStatement("insert into documents(text,photo) values(?,?)");
	//向数据库写入Clob字段
	File file1 = new File("d:\1.txt");
	int filel_length = (int)file1.length();
	InputStream f1 = new FileInputStream(file1);
	ps.setAsciiStream(1, f1, filel_length);
	//向数据库写入Blob字段
	File file2 = new File("d:\1.jpg");
	int file2_length = (int)file2.length();
	InputStream f2 = new FileInputStream(file2);
	ps.setBinaryStream(2, f2, file2_length);
	ps.execute();
	con.commit();
			
	ResultSet rs1 = ps.executeQuery("select text,photo from documents where id='365'");
	while(rs1.next()){
//	java.sql.Clob clob =rs1.getClob(1); //和提取一般对象一样
	InputStream is = rs1.getAsciiStream(1); //得到Clob的流
	BufferedReader br = new BufferedReader(new InputStreamReader(is));
	String line = null;
	while(null != (line=br.readLine())){
		System.out.println(line);
		}
		is.close();
				
	java.sql.Blob blob = rs1.getBlob(2);
	System.out.println(blob.length());
	InputStream bis = blob.getBinaryStream(); //得到Blob实例的字节流
	OutputStream os = new FileOutputStream("d:\11.jpg");
	int i = bis.read();
	while(i != -1){
		os.write(i);
		i = bis.read();
		}
	os.flush();
	os.close();
	bis.close();	
	}
} catch (Exception e) {
	e.printStackTrace();
	}


2.将blob 转换成 String
// 将blob 转换成 String
public static String BlobToString(){
	Connection conn = null;
	Statement st = null;
	ResultSet rs = null;
	String str = "";
	try {
	    //...获取Connection            
	    st = conn.createStatement();
	    rs = st.executeQuery("select big_bit from blob_test");
	     while (rs.next()) 
	        Blob blob = rs.getBlob(1);
	        InputStream in = blob.getBinaryStream();
	        //一般接下来是把in的字节流写入一个文件中,但这里直接放进字符串
	        byte[] buff=new byte[(int) blob.length()];
	//      byte[] buff=new byte[1024];
         //    byte[] b = new byte[blob.getBufferSize()];
	    for(int i=0;(i=in.read(buff))>0;){
	        str=str+new String(buff);
	    }    
	    in.close();
	//    blob.close();  //是否需要关闭?
	    }catch (Exception e) {
		e.printStackTrace();
	     }
	return str;
}


3.将clob 转换成 String
// 将clob 转换成 String
public static String ClobToString(CLOB clob) throws SQLException, IOException { 
	String reString = ""; 
	Reader is = clob.getCharacterStream();// 得到流 
	BufferedReader br = new BufferedReader(is); 
	String s = br.readLine(); 
	StringBuffer sb = new StringBuffer(); 
	while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING 
	sb.append(s); 
	s = br.readLine(); 
	} 
reString = sb.toString(); 
return reString; 
} 


4.将String类型转换成Blob类型
String str = "EHOMEvsLGD";
oracle.sql.BLOB b = (oracle.sql.BLOB)(str.getBytes());


5.将String类型转换成Clob类型
String str = "DKvsWEvsCCM";
java.sql.Clob c = new javax.sql.rowset.serial.SerialClob(str.toCharArray());

0
2
分享到:
评论

相关推荐

    ORACLE中CLOB字段转String类型

    ORACLE中CLOB字段转String类型

    CLOB与BLOB

    CLOB与BLOB的存储与读取,String与CLOB的想换转换,字节码文件的存储与读取等等

    mybatis 对clob类型转换

    mybatis 对clob类型转换,解决clob类型数据插入数据库报异常问题

    kettle通过java代码将数据库blob 字段抽取到本地文件

    kettle通过java代码将数据库blob 字段抽取到本地文件

    关于Clob类型在Hibernate中 的应用小结.rar

    通常,要解决超过4000字节的数据,一种做法是将数据写入文件,xml或plain file都可以,数据...另一个做法是使用clob, blob等字段类型,主要有:采用传统的jbdc方式、把clob以string方式处理、直接使用clob类型三种方案

    深入浅析mybatis oracle BLOB类型字段保存与读取

     BLOB是指二进制大对象也就是英文Binary Large Object的所写,而CLOB是指大字符对象也就是英文Character Large Object的所写。其中BLOB是用来存储大量二进制数据的;CLOB用来存储大量文本数据。BLOB通常用来保存...

    jdbc批量插入大字段

    针对oracle中blob字段的操作,能批量快速的插入大字段,效率非常高

    ssh(structs,spring,hibernate)框架中的上传下载

     我们前面已经指出Oracle的Lob字段和一般类型的字段在操作上有一个明显的区别--那就是你必须首先通过Oracle的empty_blob()/empty_clob()初始化Lob字段,然后获取该字段的引用,通过这个引用更改其值。所以要完成对...

    Hibernate注释大全收藏

    具体的java.sql.Clob, Character[], char[] 和 java.lang.String 将被持久化为 Clob 类型. java.sql.Blob, Byte[], byte[] 和 serializable type 将被持久化为 Blob 类型。 @Lob public String getFullText() { ...

    ARCH4系统开发指南

    2.8.1.2 操作CLOB数据 20 2.8.1.3 操作BLOB数据 21 2.8.2 实现Sequence主键生成策略 21 2.9 如何清除Hibernate Session中的对象 22 2.10 使用日期控件 22 2.11 如何引入外部文件 23 2.11.1 主页面采用include方式,...

    Java开发详解.zip

    031708_【第17章:Java数据库编程】_处理大数据对象(1)—处理CLOB数据笔记.pdf 031709_【第17章:Java数据库编程】_处理大数据对象(2)—处理BLOB数据笔记.pdf 031710_【第17章:Java数据库编程】_...

    DevArt UniDAC v.3.70.0.18 (15-Jun-2011) (with Source Code)

    Support of BLOB, CLOB, and NCLOB data types in TUniLoader is improved PostgreSQL data provider Application Name connection option is supported Payload parameter for PostgreSQL notification is ...

    DBKING使用指南

    在dbking中,所有的数据库数据只有五种数据类型,String、Number(BigDecimal)、Timestamp、Clob(String)、Blob(byte[]),经过反复测试后,我们会例出各种数据库数据类型到这五种类型的映射表,当然我们也有...

    zoom:Java框架支持AOPIOCORMWEB

    当您从数据库中获取BLOB,CLOB,TIMESTAMP,DATETIME之类的值并将其转换为简单的Java类型(如byte [],String,Date,long)时,此功能非常有用。 Aop(面向方面​​的编程) 容易提高一堂课。 AopFactory fa

    SqliteDev 384

     g CLOB  h BLOB  i TIMESTAMP  j NUMERIC 10 5  k VARYING CHARACTER 24  l NATIONAL VARYING CHARACTER 16  ;  前面提到在某种情况下 SQLite的字段并不是无类型的 即在字段类型为”Integer Primary ...

    SQLite(SqliteDev)

     h BLOB,  i TIMESTAMP,  j NUMERIC(10,5)  k VARYING CHARACTER (24),  l NATIONAL VARYING CHARACTER(16)  );  前面提到在某种情况下, SQLite的字段并不是无类型的. 即在字段类型为”Integer Primary Key”...

    unidac_7_1_4_pro DELPHI 10 Tokyo

    Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...

    Unidac Pro 7.1.4 XE8

    Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...

    unidac_7_1_4_pro DELPHI 10 Berlin

    Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...

    MYSQL,SQLSERVER,ORACLE常用的函数

    SQL> select instr('oracle traning','ra',1,2) instring from dual; INSTRING --------- 9 6.LENGTH 返回字符串的长度; SQL> select name,length(name),addr,length(addr),sal,length(to_char(sal)) from ....

Global site tag (gtag.js) - Google Analytics