Oracle blob demo

    public void saveBlob(String path) throws SQLException, FileNotFoundException, IOException
    {
        //ITRDR_environment.xlsx
        PreparedStatement pstmt= connection.prepareStatement("update blob_FILE set file =?where id =123456");
        InputStream is = new FileInputStream(path+"22.xlsx");
        pstmt.setBinaryStream(1, is,is.available());
        pstmt.executeUpdate();
        connection.commit();
        is.close();
    }

    public void getBlob(String path) throws SQLException, FileNotFoundException, IOException
    {
        String query = "SELECT FILE FROM blob_FILE where id =123456";
        java.sql.Blob blob= null;
        preparedStatement = connection.prepareStatement(query);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next())
        {
            blob = resultSet.getBlob("FILE");
        }
        InputStream ins=  blob.getBinaryStream();
        
        OutputStream ops = new FileOutputStream(new File(path+"11.xls"));
        
        byte[]b = new byte[1024];
        int len = 0;
        while((len=ins.read(b))!=-1)
        {
            ops.write(b, 0, len);
        }
        ops.close();
        ins.close();
    }

 

posted @ 2015-11-11 17:07  雪来了  阅读(202)  评论(0编辑  收藏  举报