当前位置:   article > 正文

blob与string类型的相互转换——把stringlexington的数据存进oracle的blob字段中_oracle数据库blob能否存string

oracle数据库blob能否存string
<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>

  1. <pre name="code" class="java">package com.coci.test2;
  2. import java.io.OutputStream;
  3. import java.io.UnsupportedEncodingException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9. import oracle.sql.BLOB;
  10. /**
  11. *
  12. * @author Coci
  13. *
  14. */
  15. public class TestBlob {
  16. public static void main(String[] args) {
  17. // blob内存放的是字节数组
  18. // String 的getBytes方法获得该字符串的字节数组(注意编码),然后存入blob即可
  19. String blobStr = "blob";
  20. byte[] bytes = null;
  21. try {
  22. bytes = blobStr.getBytes("utf-8");
  23. System.out.println("===" + bytes);
  24. } catch (UnsupportedEncodingException e) {
  25. e.printStackTrace();
  26. }
  27. instertData(bytes);
  28. // 从数据库中读取Blob类型数据后,要转换成String类型,即转换成InputStream,再从InputStream转成byte[],再到String即可。
  29. // blob转换成String
  30. // String result = "";
  31. // try {
  32. // ByteArrayInputStream msgContent = (ByteArrayInputStream) blob
  33. // .getBinaryStream();
  34. // byte[] byte_data = new byte[msgContent.available()];
  35. // msgContent.read(byte_data, 0, byte_data.length);
  36. // result = new String(byte_data);
  37. // } catch (SQLException e) {
  38. // e.printStackTrace();
  39. // }
  40. }
  41. @SuppressWarnings("deprecation")
  42. public static void instertData(byte[] value) {
  43. // TODO Auto-generated method stub
  44. try {
  45. Class.forName("oracle.jdbc.driver.OracleDriver");
  46. String url = "jdbc:oracle:thin:@10.211.19.71:1521:orcl";
  47. String username = "yst";
  48. String password = "yst";
  49. Connection con = DriverManager.getConnection(url, username,
  50. password);
  51. con.setAutoCommit(false);
  52. String sql1 = "insert into testcoci(id,name) values('88',empty_blob())";
  53. Statement statement = con.createStatement();
  54. boolean b2 = statement.execute(sql1);
  55. System.out.println("第一次===" + b2);
  56. String sql2 = "select name from testcoci where id=88 for update";
  57. PreparedStatement stmt = con.prepareStatement(sql2);
  58. ResultSet rs = stmt.executeQuery();
  59. OutputStream outStream = null;
  60. if (rs.next()) {
  61. System.out.println("进来了");
  62. BLOB blob = (BLOB) rs.getBlob(1);
  63. System.out.println("数据库 blob =" + blob + "=");
  64. outStream = blob.getBinaryOutputStream();
  65. outStream.write(value, 0, value.length);
  66. }
  67. outStream.flush();
  68. outStream.close();
  69. con.commit();
  70. con.close();
  71. } catch (Exception e) {
  72. System.out.println(e.getCause());
  73. }
  74. }
  75. }
<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>





                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/723057
推荐阅读
相关标签
  

闽ICP备14008679号