首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

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

2012-08-11 
Blob、Clob、String的对象相关操作大型对象BLOB就是使用二进制保存数据。如:保存位图。CLOB使用CHAR来保存数据

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 转换成 Stringpublic 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 转换成 Stringpublic 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());

热点排行