随笔-71  评论-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试验数据
     *
     
*/

    
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试验数据
     
*/

    
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 阅读(988) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: