oralc 中图片的存储与读取

 

ai0709编写,joker_yao整理

// 数据类型 long row
// 存储
SaveImg
public   void  insertr_save_img(String img_no,String imgpath)  throws  Exception{
    PreparedStatement pstmt 
=   null ;
    
try {
        
        File file 
=   new  File(imgpath); 
        
int  fileLength  = ( int ) file.length(); 
        InputStream fin 
=   new  FileInputStream(file); 
        
        
                
        String query 
=   " Insert into save_img(  "   +
                              
" img_no, emp_pict " +
                       
"  ) values (  " +
                              
" ?, ?) " ;
        pstmt 
=  connection.prepareStatement(query);
        pstmt.setString(
1 ,img_no);
        pstmt.setBinaryStream (
2 , fin, fileLength); 
             
int  affected  =  pstmt.executeUpdate();
      
    }
catch (Exception e){
        
    }
    
finally  {
        
try {pstmt.close();} catch (Exception e){}
    } 
//  try-finally

//  end insert

// 读取
Readimg
public   byte []  select_img(String emp_no)  throws  Exception{
    PreparedStatement pstmt 
=   null ;
    
byte [] bo;
    ResultSet rs 
=   null ;
    
try {
        String query 
=   " Select  emp_pict  "   +
                       
"   from save_img "   +
                       
"   where serial_no =?   " ;
        pstmt 
=  connection.prepareStatement(query);
        pstmt.setString(
1 ,emp_no); 
        rs 
=  pstmt.executeQuery();
       
if (rs.next()) { 
           bo
= rs.getBytes( 1 );
        } 
//  end While
         else  { 
            bo
= new   byte [ 0 ];}
    } 
finally  {
        
try {rs.close();} catch (Exception e){}
        
try {pstmt.close();} catch (Exception e){}
    } 
//  try-finally
         return  bo;
//  end select
// jsp显示

<%      
                    
        
try {   
              Class.forName(
" oracle.jdbc.driver.OracleDriver " ).newInstance();         
             String   url
= " jdbc:oracle:thin:@127.0.0.1:1521:oracle?      user=scott&password=tiger&useUnicode=true&characterEncoding=8859_1 " ;         
            Connection   conn
=    DriverManager.getConnection(url);         
            String   sql   
=     " select emp_pict from ee.ba020m where emp_no= " + emp_no_imge;         
            Readimg img
=   new  Readimg(conn);
            String img_no
= " 1 " ;
            
byte [] bo  =  img.select_img(img_no);     
            
              response.reset();     
            response.setContentType(
" image/jpeg " );    
            
            ServletOutputStream   sout   
=    response.getOutputStream();     
            
               
for ( int  i =   0  ; i <  bo.length; i ++ ){   
                           sout.write(bo[i]);   
                }     
              sout.flush();     
              sout.close();     
                  
          }     
          
catch (Exception   e){System.out.println(e);}  
          
finally {
          
try {rs.close();} catch (Exception e){}
              
if  ( conn !=   null  ) conn.close();
          }   
      
%>      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值