zoukankan      html  css  js  c++  java
  • java读取Oracle的BFile文件

    /**
    *
    * @author Jasmine
    */
    public class GetBlob
    {
    public static void main(String[] args)
    {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try
    {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    Class.forName(driver);
    System.out.println("class");
    conn = DriverManager.getConnection(url, "lyy", "lyy");
    System.out.println("connect");
    stmt = conn.createStatement();
    String sql = "select files from lyy.filetable where id=4";
    rs = stmt.executeQuery(sql);
    System.out.println("sql="+sql);
    while (rs.next())
    {
          System.out.println(rs.getMetaData().getColumnTypeName(1));

    //获取locator
    BFILE bf = (BFILE)rs.getObject(1);
    System.out.println("isFileOpen="+bf.isFileOpen());
    OutputStream ops = null;
    InputStream ips = null;
    File file = new File("e:"+File.separator+bf.getName());
    try
    {
        System.out.println(""+bf.fileExists());
        bf.openFile();
        ips = bf.getBinaryStream();
        byte[] buffer =new byte[bf.getBytes().length];
        ops = new FileOutputStream(file);

        //生成os系统文件

       for (int i; (i = ips.read(buffer)) > 0;)
       {
            System.out.println("i=" + i);
            ops.write(buffer, 0, i);
            ops.flush();
        }

    catch (Exception ex)
    {
           ex.printStackTrace(System.out);
    }
    finally
    {
          ips.close();
          bf.closeFile();
          ops.close();
    }

    }

    }
    catch (Exception ex)
    {
           ex.printStackTrace(System.out);
    }
    finally
    {
         try
        {
               rs.close();
               stmt.close();
               conn.close();
        }
        catch (SQLException ex)
        {
               Logger.getLogger(GetBlob.class.getName()).log(Level.SEVERE, null, ex);
         }
    }
    }
    }

  • 相关阅读:
    Kali Linux下安装配置ProFTPD实例
    mysql 如何用root 登录
    串口总是报'Error opening serial port'
    用SPCOMM 在 Delphi中实现串口通讯 转
    delphi中使用spcomm来实现串口通讯(转载)
    SPCOMM的一些用法注意
    MySQL 字符串 转 int/double CAST与CONVERT 函数的用法
    彻底删除mysql服务
    mysql 非安装版的一个自动安装脚本及工具(更新版)
    bat操作数据库mysql
  • 原文地址:https://www.cnblogs.com/liuyuanyuanGOGO/p/java_oracle_bfile.html
Copyright © 2011-2022 走看看