Oracle读取Blob数据-通过JDBC

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

[java]  view plain copy print ?
  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. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值