Oracle下对Blob和Clob的操作

前段时间碰到这个问题,用Java写了两个sample,以下是在Oracle下对这两种类型的插入和读取操作。
Blob的插入和读取:
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. public class BlobTest {
  14.     
  15.     String driver = "oracle.jdbc.driver.OracleDriver";
  16.     String url = "jdbc:oracle:thin:@192.168.38.243:1521:orcl";
  17.     String username = "fm";
  18.     String password = "manager";
  19.     Connection con = null;
  20.     Statement stmt = null;
  21.     
  22.     public BlobTest() {
  23.         con = getConnection();
  24.         try {
  25.             stmt = con.createStatement();
  26.         } catch (SQLException e) {
  27.             e.printStackTrace();
  28.         }
  29.     }
  30.     
  31.     public static void main(String[] args) {
  32.         BlobTest bt = new BlobTest();
  33.         bt.insertBolb();
  34.         bt.getBlob();
  35.         bt.close();
  36.     }
  37.     
  38.         //insert blob
  39.     private void insertBolb() {
  40.         try {
  41.             //insert an empty blob
  42.             String sql1 = "insert into blobtest values ('001',empty_blob())";
  43.             stmt.executeUpdate(sql1);
  44.             
  45.             //query the empty blob for update
  46.             String sql2 = "select filedata from blobtest where fileid = '001' for update";
  47.             ResultSet rs = stmt.executeQuery(sql2);
  48.             
  49.             //read the empty bolb
  50.             while(rs.next()) {
  51.                 //get connection from weblogic by jndi,use weblogic's jar
  52.                 //OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob(1);
  53.                 
  54.                 oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("filedata");
  55.                 OutputStream os = blob.getBinaryOutputStream();
  56.                 
  57.                 //read outter file
  58.                 InputStream is = new FileInputStream(new File("test.txt"));
  59.                 
  60.                 //write inputstream to outputstream
  61.                 byte[] b = new byte[blob.getBufferSize()];
  62.                 int len = 0;
  63.                 while((len = is.read(b)) != -1) {
  64.                     os.write(b, 0, len);
  65.                 }               
  66.                 is.close();
  67.                 os.flush();
  68.                 os.close();
  69.                 con.commit();
  70.             }
  71.             
  72.         } catch (SQLException e) {
  73.             e.printStackTrace();
  74.         } catch (FileNotFoundException e) {
  75.             e.printStackTrace();
  76.         } catch (IOException e) {
  77.             e.printStackTrace();
  78.         }
  79.     }
  80.     
  81.     //read bolb
  82.     private void getBlob() {
  83.         try {
  84.             //get blob from db
  85.             String sql = "select filedata from blobtest where fileid = '001'";
  86.             ResultSet rs = stmt.executeQuery(sql);
  87.             
  88.             while(rs.next()) {
  89.                 //get inputstream from resultset
  90.                 oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("filedata");
  91.                 InputStream is = blob.getBinaryStream();
  92.                 
  93.                 //create fileoutputstream
  94.                 OutputStream os = new FileOutputStream(new File("out.txt"));
  95.                 
  96.                 //write to file
  97.                 byte[] b = new byte[1024];
  98.                 int len;
  99.                 while((len = is.read(b)) != -1) {
  100.                     os.write(b, 0, len);
  101.                 }
  102.                 os.flush();
  103.                 os.close();
  104.                 is.close();
  105.             }
  106.         } catch (SQLException e) {
  107.             e.printStackTrace();
  108.         } catch (FileNotFoundException e) {
  109.             e.printStackTrace();
  110.         } catch (IOException e) {
  111.             e.printStackTrace();
  112.         }
  113.     }
  114.     
  115.         //get connection
  116.     private Connection getConnection() {
  117.         try {
  118.             Class.forName(driver);
  119.             con = DriverManager.getConnection(url,username,password);
  120.         } catch (ClassNotFoundException e) {
  121.             e.printStackTrace();
  122.         } catch (SQLException e) {
  123.             e.printStackTrace();
  124.         }
  125.         return con;
  126.     }
  127.     
  128.         //close connection
  129.     private void close() {
  130.         if(stmt != null) {
  131.             try {
  132.                 stmt.close();
  133.             } catch(SQLException e) {
  134.                 e.printStackTrace();
  135.             } finally {
  136.                 if(con != null) {
  137.                     try {
  138.                         con.close();
  139.                     } catch (SQLException e) {
  140.                         e.printStackTrace();
  141.                     }
  142.                 } 
  143.             }
  144.         }
  145.     }
  146. }

对Clob的插入和读取:
  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.Reader;
  6. import java.io.Writer;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. public class CLobTest {
  13.     String driver = "oracle.jdbc.driver.OracleDriver";
  14.     String url = "jdbc:oracle:thin:@192.168.38.243:1521:orcl";
  15.     String username = "fm";
  16.     String password = "manager";
  17.     Connection con = null;
  18.     Statement stmt = null;
  19.     private static String str = "bbbBBBBBBBBBBBBBBBBBB";
  20.     public CLobTest() {
  21.         con = getConnection();
  22.         try {
  23.             stmt = con.createStatement();
  24.         } catch (SQLException e) {
  25.             e.printStackTrace();
  26.         }
  27.     }
  28.     public static void main(String[] args) {
  29.         CLobTest ct = new CLobTest();
  30.         ct.insertClob();
  31.         ct.getClob();
  32.         ct.close();
  33.     }
  34.     private void insertClob() {
  35.         try {
  36.             //insert an empty clob
  37.             stmt.executeUpdate("insert into clobtest values(empty_clob())");
  38.             
  39.             //get the empty_clob for update
  40.             ResultSet rs = stmt
  41.                     .executeQuery("select myFile from clobtest for update");
  42.             while (rs.next()) {
  43.                 oracle.sql.CLOB file = (oracle.sql.CLOB) rs.getClob("myFile");
  44.                 InputStream is = new FileInputStream("path");
  45.                 Writer writer = file.getCharacterOutputStream();
  46.                 
  47.                 //write the string to output stream
  48.                 writer.write(str.toCharArray());
  49.                 writer.flush();
  50.                 writer.close();
  51.             }
  52.             con.commit();
  53.             con.close();
  54.         } catch (SQLException e) {
  55.             e.printStackTrace();
  56.         } catch (IOException e) {
  57.             e.printStackTrace();
  58.         }
  59.     }
  60.     private void getClob() {
  61.         try {
  62.             //get the clob from db
  63.             ResultSet rs = stmt.executeQuery("select myFile from clobtest");
  64.             while (rs.next()) {
  65.                 oracle.sql.CLOB file = (oracle.sql.CLOB) rs.getClob("myFile");
  66.                 Reader reader = file.getCharacterStream();
  67.                 
  68.                 //create BufferedReader with reader
  69.                 BufferedReader br = new BufferedReader(reader);
  70.                 
  71.                 //create String buffer and print clob 
  72.                 StringBuffer sb = new StringBuffer();
  73.                 String s = br.readLine();
  74.                 while (s != null) {
  75.                     sb.append(s);
  76.                     s = br.readLine();
  77.                 }
  78.                 System.out.println(sb.toString());
  79.             }
  80.         } catch (SQLException e) {
  81.             e.printStackTrace();
  82.         } catch (IOException e) {
  83.             e.printStackTrace();
  84.         }
  85.     }
  86.     private Connection getConnection() {
  87.         try {
  88.             Class.forName(driver);
  89.             con = DriverManager.getConnection(url, username, password);
  90.         } catch (ClassNotFoundException e) {
  91.             e.printStackTrace();
  92.         } catch (SQLException e) {
  93.             e.printStackTrace();
  94.         }
  95.         return con;
  96.     }
  97.     private void close() {
  98.         if (stmt != null) {
  99.             try {
  100.                 stmt.close();
  101.             } catch (SQLException e) {
  102.                 e.printStackTrace();
  103.             } finally {
  104.                 if (con != null) {
  105.                     try {
  106.                         con.close();
  107.                     } catch (SQLException e) {
  108.                         e.printStackTrace();
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值