日期:2014-05-16  浏览次数:20409 次

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