首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

oracle 存BLOB跟读取

2012-09-25 
oracle 存BLOB和读取public void savaSoftFile(InputStream inputStream,String userid) {// 传的是存入数

oracle 存BLOB和读取
   public void savaSoftFile(InputStream inputStream,String userid) {
    // 传的是存入数据库图片的id
        Connection conn=getConn();
    Statement st = null;
    BLOB blob = null; // 图片类型
    OutputStream outputStream = null; // 输出流
    File file = null; // 文件
   
    ResultSet rs = null;
    try {
    conn.setAutoCommit(false); // 事物由程序员操作
    st = conn.createStatement();
    st.executeUpdate( "update  SOFTDEPFILE  set  DEPFILE = empty_blob() where ID='"
    + userid + "'");
    rs = st.executeQuery("select DEPFILE from SOFTDEPFILE  where id='"
    + userid + "' ");
    if (rs.next()) {
    blob = (BLOB) rs.getBlob(1);
    outputStream = blob.getBinaryOutputStream();
   
    byte[] b = new byte[blob.getBufferSize()];
    int len = 0;
    while ((len = inputStream.read(b)) != -1) {
    System.out.println(len);
    outputStream.write(b, 0, len);
    }
    }

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    inputStream.close();
    outputStream.flush();
    outputStream.close();
    rs.close();
    st.close();
    conn.commit();
    conn.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    }
       
       
        public  void writeImg(OutputStream os,String userid) {  
         
            try {  
              Connection conn=getConn();
                Statement stmt = conn.createStatement();  
                ResultSet rs = stmt.executeQuery("select uploadpic from pub_usertable where  userid='"
    + userid + "'");  
                byte[] b = new byte[1024];  
                if (rs.next()) {  
                    Blob blob = rs.getBlob(1);  
                    InputStream is = blob.getBinaryStream();  
                    int i = 0;  
                    while ((i = is.read(b)) != -1) {  
                        os.write(b, 0, i);  
                    }  
                    os.close();  
                    is.close();  
                }  
                rs.close();  
                stmt.close();  
                conn.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        } 

热点排行