JDBC读取OracleBlob数据

  1. public static void main(String[] args) throws Exception {  
  2.         // 读取BLOB数据  
  3.           
  4.         Class.forName("oracle.jdbc.driver.OracleDriver");  
  5.   
  6.         Connection con = DriverManager.getConnection(  
  7.   
  8.         "jdbc:oracle:thin:@192.168.0.68:1521:myOracle""my""123");  
  9.   
  10.         con.setAutoCommit(false);  
  11.   
  12.         Statement st = con.createStatement();  
  13.   
  14.         // 这里的SQL语句不再需要”for update”  
  15.   
  16.         ResultSet rs = st.executeQuery(  
  17.   
  18.         "select content from user_content where id = 2");  
  19.   
  20.         if (rs.next()) {  
  21.   
  22.             java.sql.Blob blob = rs.getBlob(1);  
  23.   
  24.             InputStream ins = blob.getBinaryStream();  
  25.   
  26.             // 用文件模拟输出流  
  27.   
  28.             File file = new File("d:\\output.txt");  
  29.   
  30.             OutputStream fout = new FileOutputStream(file);  
  31.   
  32.             // 下面将BLOB数据写入文件  
  33.   
  34.             byte[] b = new byte[1024];  
  35.   
  36.             int len = 0;  
  37.   
  38.             while ((len = ins.read(b)) != -1) {  
  39.   
  40.                 fout.write(b, 0, len);  
  41.   
  42.             }  
  43.   
  44.             // 依次关闭  
  45.   
  46.             fout.close();  
  47.   
  48.             ins.close();  
  49.   
  50.             con.commit();  
  51.   
  52.             con.close();  
  53.         }  
  54.     }  
  55. }  

猜你喜欢

转载自jayyanzhang2010.iteye.com/blog/1544229