<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    I'll be back!

      Focus on BPM, celebrate PegaRULES Process Commander (PRPC)
    posts - 76, comments - 161, trackbacks - 0, articles - 2
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Java讀寫Oracle BLOB字段示例

    Posted on 2013-05-28 13:33 zolly 閱讀(1947) 評論(1)  編輯  收藏
    導入導出Oracle BLOB字段不能使用SQL,可以使用EXP或者EXPDP

    EXP導出示例:
    exp name/password@orcl file=E:/exp.dump log=E:/exp.log table=(tb_emp) query="""where UserName='FN'"""

    EXPDP導出示例:
    Sample.bat
    expdp name/password@orcl parfile=E:/expdp.par
    expdp.par
    DIRECTORY=dir_dump
    DUMPFILE=backup.dump
    LOGFILE=backup.log
    TABLES=pr_data_admin
    QUERY=pr_data_admin:"where UserName='FN'"

    以上方法因為我電腦上的Oracle版本對不上,總是報錯,后來采用java的方法,本文主要介紹如何使用java讀寫Oracle的BLOB字段,達到復制轉移BLOB數據的目的。代碼如下,加入ojdbc.jar,復制代碼可以直接運行:
    DBConnection.java
    package com.zolly.blob;

    import java.sql.Connection;
    import java.sql.DriverManager;

    public class DBConnection {

        public static Connection getDBConnectionFrom() throws Exception {
            Connection con = null;
            String driver = "oracle.jdbc.driver.OracleDriver";// database driver
            String url = "jdbc:oracle:thin:@192.168.1.2:1545:ORCL";// database URL
            String user = "user1"; // database name
            String password = "password1"; // database Password
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            return con;
        }
        
        public static Connection getDBConnectionTo() throws Exception {
            Connection con = null;
            String driver = "oracle.jdbc.driver.OracleDriver";// database driver
            String url = "jdbc:oracle:thin:@192.168.1.3:1521:ORCL";// database URL
            String user = "user2"; // database name
            String password = "password2"; // database Password
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            return con;
        }

    }

    BLOBUtil.java
    package com.zolly.blob;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import oracle.sql.BLOB;

    public class BLOBUtil {
        @SuppressWarnings("deprecation")
        public static void writeBLOB() {
            Connection con = null;
            long start = System.currentTimeMillis(); // count runtime
            InputStream fin = null;
            OutputStream outStream = null;
            String path = "E:\\requestor.txt";
            File file = new File(path);
            try {
                con = DBConnection.getDBConnectionTo();
                con.setAutoCommit(false);

                Statement stmt = con.createStatement();
                ResultSet rs = stmt
                        .executeQuery("select pzPVStream from pr_data_admin where pxInsName='PEGA!BROWSER' for update");
                // get specially columns and rows for update
                while (rs.next()) {
                    // System.out.print(rs.getInt(1)+rs.getString(2)+"\n");//print
                    
    // select sql for debug
                    BLOB blob = (BLOB) rs.getBlob("pzPVStream");
                    outStream = blob.getBinaryOutputStream();
                    fin = new FileInputStream(file); // put file into stream
                    byte[] b = new byte[blob.getBufferSize()];
                    int len = 0;
                    while ((len = fin.read(b)) != -1) {
                        outStream.write(b, 0, len);
                    }

                    fin.close();
                    outStream.flush();
                    outStream.close();
                }
                System.out.print("\nupdate ok\n");

                con.commit();

                con.close();
            }

            catch (Exception e) {
                e.printStackTrace();
            }

            long end = System.currentTimeMillis();
            System.out.println(end - start);
        }

        public static void readBLOB() {
            Connection con = null;
            long start = System.currentTimeMillis(); // count runtime
            String path = "E:\\requestor.txt";
            File file = new File(path);
            try {
                con = DBConnection.getDBConnectionFrom();
                con.setAutoCommit(false);
                Statement stmt = con.createStatement();
                ResultSet rs = stmt
                        .executeQuery("select pzPVStream from pr_data_admin where pxInsName='PEGA!BROWSER'");
                // get blob form your table
                if (rs.next()) {
                    BLOB blob = (BLOB) rs.getBlob("pzPVStream");
                    // get word column
                    FileOutputStream output = new FileOutputStream(file);
                    // define a file output stream
                    InputStream input = blob.getBinaryStream();// put blob into
                                                                
    // input
                    byte[] buffer = new byte[blob.getBufferSize()];
                    // if use 1024 it will lose some bytes
                    int len = 0;
                    while ((len = input.read(buffer)) != -1) {
                        // get all input stream into output file stream
                        output.write(buffer, 0, len);
                        input.close();
                        output.flush();
                        output.close();
                    }
                    System.out.print("\ndownload ok\n");
                }
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            long end = System.currentTimeMillis();
            System.out.println(end - start);

        }

        public static void main(String[] args) {
            BLOBUtil.readBLOB();
            BLOBUtil.writeBLOB();
        }

    }

    評論

    # re: Java讀寫Oracle BLOB字段示例  回復  更多評論   

    2014-03-14 16:59 by rivers
    while ((len = input.read(buffer)) != -1)
    循環錯了。
    while ((len = input.read(buffer)) != -1) {
    // get all input stream into output file stream
    output.write(buffer, 0, len);
    }
    input.close();
    output.flush();
    output.close();

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品动漫免费二区| 精品免费AV一区二区三区| 成人午夜免费福利| 日本视频免费观看| 亚洲天堂一区二区| 国产成人免费a在线视频app| 国产一级在线免费观看| 亚洲免费观看网站| 免费A级毛片无码久久版| 色猫咪免费人成网站在线观看| 亚洲最大av资源站无码av网址| 中文字幕亚洲不卡在线亚瑟| 精品香蕉在线观看免费| 国产精品免费看久久久香蕉| 最新国产成人亚洲精品影院| 精品国产综合成人亚洲区| 国产精品视频免费一区二区| A片在线免费观看| www亚洲精品久久久乳| 久久亚洲AV成人无码国产| 免费观看四虎精品国产永久| 亚洲一区二区三区免费观看| a一级爱做片免费| 亚洲精品无码不卡在线播放| 亚洲av福利无码无一区二区| 全亚洲最新黄色特级网站| 免费能直接在线观看黄的视频| a高清免费毛片久久| 亚洲人成网男女大片在线播放| 国产亚洲一区二区三区在线观看| 国产黄色片在线免费观看| 麻豆视频免费观看| 大地资源中文在线观看免费版| 国产午夜亚洲精品不卡电影| 亚洲乱码中文论理电影| 亚洲精品无码久久久影院相关影片| 四虎影在线永久免费四虎地址8848aa| 91成人免费在线视频| 三年片在线观看免费观看大全一| 成人免费一区二区三区| 男女猛烈无遮掩视频免费软件|