blob与string类型的相互转换——把stringlexington的数据存进oracle的blob字段中
2016-08-24 19:24
489 查看
<div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;">【概述】 <wbr style="word-wrap: break-word;"></wbr></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;"><br style="word-wrap: break-word;" /></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;"> <wbr style="word-wrap: break-word;"> <wbr style="word-wrap: break-word;"> Oracle的Blob字段比较特殊,他比long字段的性能要好很多,可以用来保存例如图片之类的二进制数据。 <wbr style="word-wrap: break-word;"></wbr></wbr></wbr></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;"><br style="word-wrap: break-word;" /></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;">写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须使用cursor对 <wbr style="word-wrap: break-word;"></wbr></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;"><br style="word-wrap: break-word;" /></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;">blob进行操作,因而你在写入Blob之前,必须获得cursor才能进行写入,那么如何获得Blob的cursor呢? <wbr style="word-wrap: break-word;"></wbr></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;"><br style="word-wrap: break-word;" /></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;">这需要你先插入一个empty的blob,这将创建一个blob的cursor,然后你再把这个empty的blob的cursor <wbr style="word-wrap: break-word;"></wbr></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;"><br style="word-wrap: break-word;" /></span></div><div style="word-wrap: break-word; color: rgb(102, 102, 102); font-family: 宋体, Arial; line-height: 26px;"><span style="font-size:18px;">用select查询出来,这样通过两步操作,你就获得了blob的cursor,可以真正地写入blob数据了。 </span></div>
<pre name="code" class="java">package com.coci.test2;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import oracle.sql.BLOB;/**** @author Coci**/public class TestBlob {public static void main(String[] args) {// blob内存放的是字节数组// String 的getBytes方法获得该字符串的字节数组(注意编码),然后存入blob即可String blobStr = "blob";byte[] bytes = null;try {bytes = blobStr.getBytes("utf-8");System.out.println("===" + bytes);} catch (UnsupportedEncodingException e) {e.printStackTrace();}instertData(bytes);// 从数据库中读取Blob类型数据后,要转换成String类型,即转换成InputStream,再从InputStream转成byte[],再到String即可。// blob转换成String// String result = "";// try {// ByteArrayInputStream msgContent = (ByteArrayInputStream) blob// .getBinaryStream();// byte[] byte_data = new byte[msgContent.available()];// msgContent.read(byte_data, 0, byte_data.length);// result = new String(byte_data);// } catch (SQLException e) {// e.printStackTrace();// }}@SuppressWarnings("deprecation")public static void instertData(byte[] value) {// TODO Auto-generated method stubtry {Class.forName("oracle.jdbc.driver.OracleDriver");String url = "jdbc:oracle:thin:@10.211.19.71:1521:orcl";String username = "yst";String password = "yst";Connection con = DriverManager.getConnection(url, username,password);con.setAutoCommit(false);String sql1 = "insert into testcoci(id,name) values('88',empty_blob())";Statement statement = con.createStatement();boolean b2 = statement.execute(sql1);System.out.println("第一次===" + b2);String sql2 = "select name from testcoci where id=88 for update";PreparedStatement stmt = con.prepareStatement(sql2);ResultSet rs = stmt.executeQuery();OutputStream outStream = null;if (rs.next()) {System.out.println("进来了");BLOB blob = (BLOB) rs.getBlob(1);System.out.println("数据库 blob =" + blob + "=");outStream = blob.getBinaryOutputStream();outStream.write(value, 0, value.length);}outStream.flush();outStream.close();con.commit();con.close();} catch (Exception e) {System.out.println(e.getCause());}}}
<span style="font-size:18px;">这篇博客写的很详细:</span>
<a target=_blank href="http://blog.itpub.net/21632975/viewspace-1116728/"><span style="font-size:18px;">http://blog.itpub.net/21632975/viewspace-1116728/</span></a>
相关文章推荐
- Oracle中Clob类型的字段数据转换成String字符串
- Mysql数据库中图片字段Blob类型和String类型相互转换
- Oracle 中Clob 字段,转换为 String 类型
- C++中string,char,int,double等数据类型的相互转换及与ASCII码的转换
- Java 中 Blob类型和String类型相互转换
- Kettle 生成的XML格式数据保存到Oracle 数据库的BLOB类型字段中
- oracle String类型转换成blob类型插入
- oracle中blob字段转换成varchar类型
- int类型与char,string类型数据的相互转换
- Java从数据库取出BLOB数据放入map中,从map中拿出转换成String类型数据
- VC 中 Cstring 与 int、float、double、char、string数据类型的相互转换
- java中Date类型数据与oracle中Date类型数据的相互转换(转)
- 基于Swift的iOS应用程序开发:字符串String与Data数据类型相互转换
- Kettle 生成的XML格式数据保存到Oracle 数据库的BLOB类型字段中
- Hibernate操作Blob类型字段出现java.lang.AbstractMethodError: oracle.jdbc.driver.OracleResultSetImpl.getBlob(Ljava/lang/String;)Ljava/sql/
- Java数据类型中String、Integer、int的相互转换以及日期的转换
- oracle数据类型函数以及相互转换
- java中String类型数据与Date类型数据相互转换
- 把System.byte[]类型数据写入Oracle的blob类型字段中
- Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法"System.String ToString()"问题解决)