隨筆-71  評(píng)論-4  文章-0  trackbacks-0
          package com.hbky.bo;

          import java.io.BufferedInputStream;
          import java.io.BufferedOutputStream;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.IOException;
          import java.io.InputStream;
          import java.io.OutputStream;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.sql.Statement;

          import oracle.sql.BLOB;

          public class FilmBo {
          Connection con;
              
              
          public FilmBo(){
                  
          try {
                      Class.forName(
          "oracle.jdbc.driver.OracleDriver");
                  }
           catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  }

              }

              
              
          /**
               * 取出blob試驗(yàn)數(shù)據(jù)
               *
               
          */

              
          public void getBlob(BufferedOutputStream out){
                  BufferedInputStream in 
          = null;
                  
                  
          try {

                      con
          =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:zhanglei""scott","tiger");

                      con.setAutoCommit(
          false);

                      PreparedStatement pre 
          = con.prepareStatement("select fname,fblob from ftp_test where fid = ?");

                      pre.setString(
          1,"00001");

                      ResultSet set 
          = pre.executeQuery();
                      
                      
          if(set.next()){
                          in 
          = new BufferedInputStream(set.getBlob("fblob").getBinaryStream());

          //                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("c:\"+set.getString("fname"))));

                          
          byte[] buffer = new byte[1024];

                          
          int i = 0;
                          
                          
          while((i = in.read(buffer)) != -1){
                              out.write(buffer,
          0,i);
                          }


                          out.flush();
                          System.out.println(
          "******************************");

                          in.close();

                          out.close();
                          
                          con.commit();
                      }


                      pre.close();

                      con.close();
                      
                  }
           
          //        catch (FileNotFoundException e) {
          //            try {
          //                con.rollback();
          //            } catch (SQLException e1) {
          //                e1.printStackTrace();
          //            }
          //            e.printStackTrace();
          //        } 
                  catch (SQLException e) {
                      
          try {
                          con.rollback();
                      }
           catch (SQLException e1) {
                          e1.printStackTrace();
                      }

                      e.printStackTrace();
                  }
           catch (IOException e) {
                      
          try {
                          con.rollback();
                      }
           catch (SQLException e1) {
                          e1.printStackTrace();
                      }

                      e.printStackTrace();
                  }

                  
              }

              
          /**
               * 插入blob試驗(yàn)數(shù)據(jù)
               
          */

              
          public void insertBlob(){
                  
                  
          try {
                      Connection con 
          = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:zhanglei""scott""tiger");
                      con.setAutoCommit(
          false);
                      Statement st 
          = con.createStatement();
                      st.executeUpdate(
          "insert into ftp_test(fid,fblob,fname)  values('00002',empty_blob(),'test.jpg')");
                      ResultSet rs 
          = st.executeQuery("select fblob from  ftp_test  where  fid='00002' for update");
                      
          if (rs.next()) {
                          BLOB blob 
          = (BLOB) rs.getBlob(1);
                          OutputStream outStream 
          = blob.getBinaryOutputStream();
                          File file 
          = new File("F:\pic\CIMG4386.jpg");
                          InputStream fin 
          = new FileInputStream(file);
                          
          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();
                          con.commit();
                          con.close();

                      }

                  }
           catch (FileNotFoundException e) {
                      
          try {
                          con.rollback();
                      }
           catch (SQLException e1) {
                          e1.printStackTrace();
                      }

                      e.printStackTrace();
                  }
           catch (SQLException e) {
                      
          try {
                          con.rollback();
                      }
           catch (SQLException e1) {
                          e1.printStackTrace();
                      }

                      e.printStackTrace();
                  }
           catch (IOException e) {
                      
          try {
                          con.rollback();
                      }
           catch (SQLException e1) {
                          e1.printStackTrace();
                      }

                      e.printStackTrace();
                  }

                  
              }

          }

          posted on 2007-10-27 20:33 zjw_albert 閱讀(1003) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 霍邱县| 务川| 行唐县| 青海省| 达日县| 顺昌县| 滨海县| 商都县| 万安县| 赤水市| 宝山区| 荥阳市| 汉阴县| 宁陕县| 普兰县| 太和县| 启东市| 建始县| 聂荣县| 中卫市| 和静县| 巴彦淖尔市| 乃东县| 潞西市| 三门县| 建阳市| 东城区| 临沧市| 岱山县| 恭城| 安龙县| 蒲江县| 九台市| 宣威市| 东兰县| 正宁县| 五寨县| 莱州市| 山阴县| 正定县| 萝北县|