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

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

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

    每日一得

    不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速開發
    最近關心的內容:SSH,seam,flex,敏捷,TDD
    本站的官方站點是:顛覆軟件

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      220 隨筆 :: 9 文章 :: 421 評論 :: 0 Trackbacks
    key words : blob , oracle ,插入圖片

    come from  here

    顯示blob

    try {
    Class.forName(
    "sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    Connection con
    =DriverManager.getConnection("jdbc:odbc:Multimedia","samp","samp");
    Statement stmt
    =con.createStatement();
    ResultSet rs
    =stmt.executeQuery("select frame from MULTIMEDIA_TAB where clip_id=1");
    if(rs.next()){
    try {
    InputStream dis
    =rs.getBinaryStream(1);
    byte[] x = new byte [10*1024]; //creare byte array to hold the blob data
    int lengthRead = 0// Number of bytes read
    String fileToWriteBlob = "./default-app/MyProgs/ImageRetrieval/ganpati.jpg";
    FileOutputStream outputStream 
    = new FileOutputStream(fileToWriteBlob);
    while( (lengthRead =dis.read(x)) != -1){
    outputStream.write(x);
    }

    dis.close();
    outputStream.close();
    }

    catch(Exception e) {
    System.out.println (
    "Error is : " + e);
    }

    }

    }

    catch(Exception ex) {
    System.out.println(
    "Error " + ex);
    }
     


    If you are inserting a row:

    1.Insert the row with all values, but insert an empty blob into the blob column:
    "INSERT INTO yourtable (id,blob_value) VALUES (?,empty_blob())"

    2. Select the empty blob you created:
    "SELECT blob_value FROM yourtable WHERE id=?"
    oracle.sql.BLOB blob = rs.getBLOB("blob_value");
    (some code is of course missing, but this should give you the idea)
    3. Write the bytes into the blob:

    try {
    OutputStream outstream = blob.getBinaryOutputStream();
    outstream.write(blobValue);
    outstream.flush();
    outstream.close();
    } catch (IOException e) {
    // handle exception
    }
    Where blobValue is the byte array containing the image data.

    4. Update the row:
    "UPDATE yourtable SET blob_value=? WHERE id=?"
    pstmt.setBLOB(1, blob);
    pstmt.setInt(2, id);
    (NOTE: the setBLOB is an Oracle specific method, you can probably also use the setBlob method).
    Again, some essential code concerning the prepared statements is missing, but this should give you an idea...

    That's that. If you just need to update the row, you just go through steps 2-4 above.


    Here is an example:

    import java.sql.*;
    import java.io.*;

    public class imgtest {

    private PreparedStatement ps = null;
    private Connection conn = null;

    public void initialise() {
    String URL 
    = "jdbc:oracle:thin:@localhost:1521:SID";
    String userid 
    = "user";
    String passwd 
    = "pass";
    // register the JDBC driver
    try {
    DriverManager.registerDriver(
    new
    oracle.jdbc.driver.OracleDriver());
    // get a connection
    Connection conn = DriverManager.getConnection(URL, userid, passwd);
    this.conn = conn;
    // create a prepared statement
    ps = conn.prepareStatement("INSERT INTO images (NAME,IMAGE) VALUES (?,?)");
    }

    catch (Exception e) {
    System.out.println(
    "Error: " + e);
    System.exit(
    1);
    }

    }


    public imgtest() {
    initialise();
    File fImage 
    = null;
    FileInputStream isImage 
    = null;

    try {
    fImage 
    = new File( "fileo.gif" );
    isImage 
    = new FileInputStream( fImage );
    }

    catch (FileNotFoundException fnf) {
    System.out.println(
    "File not found: " + fnf);
    System.exit(
    1);
    }

    try {
    ps.setString(
    1,"Open File");
    ps.setBinaryStream(
    2,isImage,(int)(fImage.length()));
    ps.executeUpdate();
    ps.close();
    conn.close();
    }

    catch (Exception e) {
    System.out.println(
    "Error: " + e);
    System.exit(
    1);
    }

    }


    public static void main(String[] args) {
    new imgtest();
    }

    }
     


    My table looks like this:
    // ---->%----
    CREATE TABLE images
    (NAME VARCHAR2(16) primary key not null,
    IMAGE BLOB(64k));
    commit;
    quit;
    // ---->%----


    update(2007-5-13)
    /**
     * Oracle中的Blob類型字段操作
     * User: Alex
     * Date: 2007-5-9
     * Time: 16:42:43
     
    */

    public class BlobUtil {

        
    /**
         * 獲得blob內容
         *
         * 
    @param documentId : 文檔編號
         * 
    @param response   : HttpServletResponse
         
    */

        
    public static void downloadDocument(String documentId, HttpServletResponse response) {
            
    if (null == documentId) throw new JyhdException("文檔編號為空,請檢查");

            DbDao db 
    = null;
            ResultSet rs 
    = null;
            BLOB blob 
    = null;
            OutputStream out 
    = null;
            
    try {
                String sql 
    = "select id,file_blob from tbl_blob_document where id = " + documentId;
                db 
    = new DbDaoImpl();
                rs 
    = db.query(sql);

                
    if (rs.next()) {
                    blob 
    = (BLOB) rs.getBlob("file_blob");
                    InputStream in 
    = blob.getBinaryStream();
                    out 
    = response.getOutputStream();
                    
    int size = blob.getBufferSize();
                    
    byte[] buffer = new byte[size]; // 建立緩沖區
                    int len;
                    
    while ((len = in.read(buffer)) != -1)
                        out.write(buffer, 
    0, len);
                }

            }

            
    catch (Exception e) {
                e.printStackTrace();
                
    throw new JyhdException("===>>>table非法");
            }

            
    finally {
                
    try {
                    
    if (null != out) {
                        out.close();
                    }

                    
    if (null != db) {
                        db.close();
                    }

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

            }


        }


        
    /**
         * 插入blob內容
         * updated: 支持update,動態判斷documentId是否已經存在,若已經存在則修改
         *
         * 
    @param inputStream : 讀取輸入流
         * 
    @param documentId  :  請從WellsoonUtil.getSequence("TBL_BLOB_DOCUMENT_SEQ") 獲取
         
    */

        
    public static void uploadDocument(InputStream inputStream, String documentId) {
            DbDao db 
    = null;
            ResultSet rs 
    = null;
            Connection conn 
    = null;
            String sql 
    = null;
            
    try {
                db 
    = new DbDaoImpl();
                conn 
    = db.getConnection();
                conn.setAutoCommit(
    false);
                Statement stmt 
    = conn.createStatement();

                
    //如果已經存在則更新
                String hasOldRecord = "select id from tbl_blob_document where id =" + documentId;
                
    if (!DbUtil.hasRecord(hasOldRecord)) {
                    sql 
    = "insert into tbl_blob_document( id,file_blob) values(" + documentId + ",EMPTY_BLOB())";
                    
    int count = stmt.executeUpdate(sql);
                }

                
    //select for update
                sql = "select id,file_blob from tbl_blob_document where id = " + documentId + " for update";
                rs 
    = stmt.executeQuery(sql);

                
    if (rs.next()) {
                    BLOB blob 
    = ((OracleResultSet) rs).getBLOB("file_blob");  // 得到BLOB對象
                    OutputStream out = blob.getBinaryOutputStream();  // 建立輸出流
                    int size = blob.getBufferSize();
                    
    byte[] buffer = new byte[size];  // 建立緩沖區
                    int len;
                    
    while ((len = inputStream.read(buffer)) != -1)
                        out.write(buffer, 
    0, len);
                    inputStream.close();
                    out.close();


                }

                conn.commit();

            }

            
    catch (Exception e) {
                e.printStackTrace();
                
    throw new JyhdException("===>>>table非法");
            }

            
    finally {
                
    try {
                    db.close();
                }
     catch (Exception ee) {
                    ee.printStackTrace();
                }

            }

        }


        
    /**
         * 填充Blob
         
    */

        
    private static void fillBlob(oracle.sql.BLOB blob, byte[] btaData) throws Exception {
            
    int len = btaData.length;
            
    long num = 0;
            
    byte[] btBlockData;
            
    for (int i = 0; i < len; i += num) {
                
    if (i == 0)
                    btBlockData 
    = btaData;
                
    else {
                    btBlockData 
    = new byte[len - i];
                    System.arraycopy(btaData, i, btBlockData, 
    0, len - i);
                }

                num 
    = blob.putBytes(i + 1, btBlockData);
            }

        }


        
    /**
         * dump Blob
         *
         * 
    @param blob
         * 
    @return
         * 
    @throws Exception
         
    */

        
    private static byte[] dumpBlob(oracle.sql.BLOB blob) throws Exception {
            
    long len = blob.length();
            
    byte[] byte0 = new byte[(int) len];
            
    byte[] byte1 = new byte[32512];
            
    int num = 0;
            
    for (int i = 0; i < len; i += num) {
                num 
    = blob.getBytes(i + 1, (int) len - i, byte1);
                System.arraycopy(byte1, 
    0, byte0, i, num);
            }

            byte1 
    = null;
            
    return byte0;
        }



        
    public static void main(String[] args) throws Exception {
            
    //文件上傳
            File file = new File("Y:\\我的照片\\Palm\\Photo_121306_013.jpg");
            InputStream in 
    = new FileInputStream(file);
            String sequence 
    = SequenceUtil.getSequence("TBL_BLOB_DOCUMENT_SEQ");
            uploadDocument(in, sequence);

        }


    }


    posted on 2007-03-28 16:55 Alex 閱讀(4588) 評論(0)  編輯  收藏 所屬分類: java 、dataBase
    主站蜘蛛池模板: 成人免费乱码大片A毛片| 77777_亚洲午夜久久多人| 麻豆国产精品入口免费观看| 在线观看特色大片免费视频| 亚洲 欧洲 视频 伦小说| 亚洲免费视频网址| 亚洲高清在线播放| 亚洲AV乱码久久精品蜜桃| 亚洲av无码av制服另类专区| 久久精品国产精品亚洲艾草网| 亚洲爆乳无码一区二区三区| 亚洲αv在线精品糸列| 亚洲午夜视频在线观看| 久久亚洲日韩看片无码| 亚洲人成在线中文字幕| 亚洲av无码片在线观看| 亚洲 欧洲 视频 伦小说| 亚洲精品乱码久久久久久| 亚洲综合图色40p| 欧亚精品一区三区免费| 女人被免费视频网站| 四虎永久免费影院| 亚洲国产婷婷综合在线精品| 亚洲午夜久久久影院伊人| 亚洲AV日韩AV永久无码久久| 亚洲欧洲精品一区二区三区| 亚洲第一男人天堂| 老子影院午夜伦不卡亚洲| 亚洲精品视频免费| 野花香在线视频免费观看大全| 最近免费mv在线电影| 午夜小视频免费观看| 亚洲国产精品自产在线播放| 日韩亚洲人成在线综合日本 | 午夜视频免费在线观看| 亚洲视频免费在线播放| 成年人免费视频观看| 国产成人精品日本亚洲专区| 亚洲日本va午夜中文字幕一区| 亚洲精品国产日韩| 午夜不卡AV免费|