欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle存入图片和读取图片教程

程序员文章站 2024-01-20 10:55:58
Oracle存入图片和读取图片教程 package com.kevinb.image; import java.io.File; import java.io.Fil...

Oracle存入图片和读取图片教程

package com.kevinb.image;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.driver.OracleBlobInputStream;

import com.kevinb.sys.db.ConnectOracleBase;


public class ProImage {
    /** database user*/
    private static final String  DBUSER = "scott";
    /** database password*/
    private static final String  DBPSW = "ffffff";
    /** database server*/
    private static final String  DBSERVER = "orcl";
    
    /**
     * 向数据库中存入图片信息;
     * @param path
     */
    @SuppressWarnings({ "static-access", "deprecation" })
    public void insertImage(String path){
        System.out.println("----insertImage");
        Connection conn = null;
        PreparedStatement ps = null;
        
        conn = ConnectOracleBase.getConnection(this.DBUSER, this.DBPSW, this.DBSERVER);
        String sql = "insert into user_manager u (u.user_id,u.user_name,u.user_photo) "
                + "values(scott.hibernate_sequence.nextval,?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, "Hello");
            File file =  new File(path);
            FileInputStream fStream = new FileInputStream(file);
            ps.setBinaryStream(2, fStream, (int) file.length());
            ps.execute();
            
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally{
            try {
                ConnectOracleBase.closeAllConnection(ps, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("图片保存成功!");
    }
    
    /**
     * 通过员工的empId取得图片信息;
     * @param userId
     */
    @SuppressWarnings("static-access")
    public void getPhoto(int userId,String path){
        System.out.println("----getPhoto");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        conn = ConnectOracleBase.getConnection(this.DBUSER, this.DBPSW, this.DBSERVER);
        String sql = "select u.user_id,u.user_name,u.user_photo "
                + "from user_manager u " + "where u.user_id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, userId);
            rs = ps.executeQuery();
            OracleBlobInputStream fStream = null;
            while(rs.next()){
                fStream = (OracleBlobInputStream)rs.getBinaryStream("user_photo");
            }
            FileOutputStream fOutputStream = new FileOutputStream(path);
            byte[] buffer = new byte[1024];
            int byteRead = 0;
            while((byteRead = fStream.read(buffer)) != -1){
                fOutputStream.write(buffer, 0, byteRead);
            }
            fOutputStream.close();
            fStream.close();
            
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                ConnectOracleBase.closeAllConnection(rs, ps, conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        System.out.println("get photo is success!");
    }
    
}

说明:ConnectOracleBase,是我自己封装的连库操作类;