Java怎样能把Oracle的Blob类型的内容导出到本地?假如里面存到是文件.

解决方案 »

  1.   

    Oracle中的BLOB和CLOB
      

  2.   

    oracle 官方文档 CLOB 10g
    http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/clob10g/handlingclobsinoraclejdbc10g.html
    oracle 官方文件 LOB sample
    http://www.oracle.com/technology/sample_code/tech/java/sqlj_jdbc/files/advanced/LOBSample/LOBSample.java.html
      

  3.   

    BLOB转byte[]
    private static byte[] getBlobData(Blob blob) throws SQLException {
            byte[] content = new byte[(int) blob.length()];
            InputStream in = null;
            try {
                in = blob.getBinaryStream();
                int bytes = in.read(content, 0, (int) blob.length());
                if (bytes != blob.length()) {
                    throw new SQLException("JDBCUtil.getBlobData failed, read " + bytes + " bytes, expected "
                            + blob.length());
                }
            }
            catch (IOException e) {
                throw new SQLException("JDBCUtil.getBlobData failed: " + e.getMessage());
            }
            finally {
                if (in != null) {
                    try {
                        in.close();
                    }
                    catch (IOException e) {
                        // ignore
                        e.printStackTrace();
                    }
                }
            }
            return content;
        }
      

  4.   

    /**   
         * 读出数据并存成文件   
         *    
         * @param id   
         * @param file   
         */   
        public static void readClobDataUseSQL(Integer id, File file) {    
            Connection conn = getConnection();    
            try {    
                Statement st = conn.createStatement();    
                String sql = "select CONTENT from TEST_CLOB where ID = " + id;    
                ResultSet rs = st.executeQuery(sql);    
       
                if (rs.next()) {    
       
                    CLOB clob = (CLOB) rs.getClob("CONTENT");    
       
                    String result = convertClobToString(clob);    
                    System.out.println(result);    
                    FileUtils.writeStringToFile(file,result, "utf-8");    
                }    
            } catch (Exception e) {    
                e.printStackTrace();    
            } finally {    
                try {    
                    conn.close();    
                } catch (SQLException e) {    
                    e.printStackTrace();    
                }    
            }    
       
        }