始创于2000年 股票代码:831685
咨询热线:0371-60135900 注册有礼 登录
  • 挂牌上市企业
  • 60秒人工响应
  • 99.99%连通率
  • 7*24h人工
  • 故障100倍补偿
您的位置: 网站首页 > 帮助中心>文章内容

Oracle读取Blob数据-通过JDBC

发布时间:  2012/8/26 16:09:48

通过JDBC读取Blob数据,演示代码

1.  
2.import java.io.File;  3.import java.io.FileOutputStream;  4.import java.io.IOException;  5.import java.io.InputStream;  6.import java.io.OutputStream;  7.import java.sql.Connection;  8.import java.sql.DriverManager;  9.import java.sql.ResultSet;  10.import java.sql.Statement;  11. 
12.public class JDBC {  13.//  public static void main(String[] args) throws Exception {   14.//      // 写入BLOB数据   15.//     16.//      // 得到数据库连接   17.//      Class.forName("Oracle.jdbc.driver.OracleDriver");   18.//   19.//      Connection con = DriverManager.getConnection(   20.//   21.//      "jdbc:Oracle:thin:@192.168.0.68:1521:myOracle", "my", "123");   22.//   23.//      // 处理事务   24.//   25.//      con.setAutoCommit(false);   26.//   27.//      Statement st = con.createStatement();   28.//   29.//      // 用for update方式锁定数据行   30.//   31.//      ResultSet rs = st.executeQuery(   32.//   33.//      "select content from user_content where id = 2 for update");   34.//   35.//      if (rs.next()) {   36.//   37.//          // 得到java.sql.Blob对象,然后Cast为Oracle.sql.BLOB   38.//   39.//          Oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);   40.//   41.//          // 到数据库的输出流   42.//   43.//          OutputStream outStream = blob.getBinaryOutputStream();   44.//   45.//          // 这里用一个文件模拟输入流   46.//   47.//          File file = new File("d:\\proxy.txt");   48.//   49.//          InputStream fin = new FileInputStream(file);   50.//   51.//          // 将输入流写到输出流   52.//   53.//          byte[] b = new byte[blob.getBufferSize()];   54.//   55.//          int len = 0;   56.//   57.//          while ((len = fin.read(b)) != -1) {   58.//   59.//              outStream.write(b, 0, len);   60.//   61.//              // blob.putBytes(1,b);   62.//   63.//          }   64.//   65.//          // 依次关闭(注意顺序)   66.//   67.//          fin.close();   68.//   69.//          outStream.flush();   70.//   71.//          outStream.close();   72.//   73.//          con.commit();   74.//   75.//          con.close();   76.//      }   77.//  }   78. 
79.    public static void main(String[] args) throws Exception {  80.        // 读取BLOB数据   81.         
82.        Class.forName("Oracle.jdbc.driver.OracleDriver");  83. 
84.        Connection con = DriverManager.getConnection( 
85. 
86.        "jdbc:Oracle:thin:@192.168.0.68:1521:myOracle", "my", "123");  87. 
88.        con.setAutoCommit(false);  89. 
90.        Statement st = con.createStatement(); 
91. 
92.        // 这里的SQL语句不再需要”for update”   93. 
94.        ResultSet rs = st.executeQuery( 
95. 
96.        "select content from user_content where id = 2");  97. 
98.        if (rs.next()) {  99. 
100.            java.sql.Blob blob = rs.getBlob(1);  101. 
102.            InputStream ins = blob.getBinaryStream(); 
103. 
104.            // 用文件模拟输出流   105. 
106.            File file = new File("d:\\output.txt");  107. 
108.            OutputStream fout = new FileOutputStream(file);  109. 
110.            // 下面将BLOB数据写入文件   111. 
112.            byte[] b = new byte[1024];  113. 
114.            int len = 0;  115. 
116.            while ((len = ins.read(b)) != -1) {  117. 
118.                fout.write(b, 0, len);  119. 
120.            } 
121. 
122.            // 依次关闭   123. 
124.            fout.close(); 
125. 
126.            ins.close(); 
127. 
128.            con.commit(); 
129. 
130.            con.close(); 
131.        } 
132.    } 
133.} 


 


本文出自:亿恩科技【www.enkj.com】

服务器租用/服务器托管中国五强!虚拟主机域名注册顶级提供商!15年品质保障!--亿恩科技[ENKJ.COM]

  • 您可能在找
  • 亿恩北京公司:
  • 经营性ICP/ISP证:京B2-20150015
  • 亿恩郑州公司:
  • 经营性ICP/ISP/IDC证:豫B1.B2-20060070
  • 亿恩南昌公司:
  • 经营性ICP/ISP证:赣B2-20080012
  • 服务器/云主机 24小时售后服务电话:0371-60135900
  • 虚拟主机/智能建站 24小时售后服务电话:0371-60135900
  • 专注服务器托管17年
    扫扫关注-微信公众号
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 亿恩科技 版权所有  地址:郑州市高新区翠竹街1号总部企业基地亿恩大厦  法律顾问:河南亚太人律师事务所郝建锋、杜慧月律师   京公网安备41019702002023号
      0
     
     
     
     

    0371-60135900
    7*24小时客服服务热线