Welcome 微信登录

首页 / 数据库 / MySQL / 使用存储过程实现Oracle存图片的方法

Oracle存图片是我们经常需要实现的功能,下面就教您一个使用存储过程实现Oracle存图片的方法,如果您在oracle存图片方面遇到过问题,不妨一看。要在Oracle存图片 用blob类型,首先在数据库里建立:--连接到管理员
  1. conn sys/tbsoft as sysdba; 
--为scott用户授权
  1. grant create any directory to scott; 
--回到scott用户
  1. conn scott/tiger; 
--创建存储图片的表
  1. CREATE TABLE IMAGE_LOB (T_ID VARCHAR2 (5) NOT NULL,T_IMAGE BLOB NOT NULL); 
--创建存储图片的目录
  1. CREATE OR REPLACE DIRECTORY IMAGES AS "C:picture"; 
--在c:下自己建一个叫picture的文件夹
  1. CREATE OR REPLACE PROCEDURE IMG_INSERT (TID VARCHAR2,FILENAME VARCHAR2) AS 
F_LOB BFILE;--文件类型B_LOB BLOB;
  1. BEGIN     
  2. iNSERT INTO IMAGE_LOB (T_ID, T_IMAGE)     
  3. VALUES (TID,EMPTY_BLOB ()) RETURN T_IMAGE INTO B_LOB    
--插入空的blob
  1. F_LOB:BFILENAME ("IMAGES", FILENAME); 
--获取指定目录下的文件
  1. DBMS_LOB.FILEOPEN(F_LOB, DBMS_LOB.FILE_READONLY); 
--以只读的方式打开文件
  1. DBMS_LOB.LOADFROMFILE (B_LOB, F_LOB,DBMS_LOB.GETLENGTH (F_LOB)); 
--传递对象
  1. DBMS_LOB.FILECLOSE (F_LOB); 
--关闭原始文件
  1. COMMIT;  
  2. END;  
  3. /  
--在C:picture下放一张图片1.gif--将该图片存入表
  1. call IMG_INSERT("1","1.gif"); 
然后创建一个web项目 连接数据库后 创建一个BlobDAO类 用来取出表中的blob类型图片
  1. public class BlobDAO {  
  2.     private static final BlobDAO instance = new BlobDAO();  
  3.     private Connection conn = null;  
  4.     private BlobDAO() {  
  5.     }  
  6.     public static BlobDAO getInstance() {  
  7.         return instance;  
  8.     }  
  9.     private void initConn() {  
  10.         conn = DBAccess.getInstance().getConn();  
  11.     }  
  12.     public byte[] getImage(String imgname) {  
  13.         BufferedInputStream ins;//取得BLOB的IO流  
  14.         byte[] bt = null;  
  15.         initConn();  
  16.         Blob bo = null;  
  17.         PreparedStatement ps = null;  
  18.         ResultSet rs = null;  
  19.         String sql = "select T_IMAGE from IMAGE_LOB where t_id=?";  
  20.         try {  
  21.               ps = conn.prepareStatement(sql);  
  22.               ps.setString(1, imgname);  
  23.               rs = ps.executeQuery();  
  24.               if (rs.next()) {  
  25.                   bo = rs.getBlob("T_IMAGE");  
  26.                   try {  
  27.                       ins = new BufferedInputStream(bo.getBinaryStream());  
  28.                       int bufferSize = (int) bo.length();//取得BLOB的长度  
  29.                       bt = new byte[bufferSize];  
  30.                       try {  
  31.                             ins.read(bt, 0, bufferSize);  
  32.                       } catch (IOException e) {  
  33.                             // TODO Auto-generated catch block  
  34.                             e.printStackTrace();  
  35.                       }  
  36.                       //建立字节缓存  
  37.                   } catch (SQLException e) {  
  38.                       // TODO Auto-generated catch block  
  39.                       e.printStackTrace();  
  40.                   }  
  41.               }  
  42.         } catch (SQLException e) {  
  43.               // TODO Auto-generated catch block  
  44.               e.printStackTrace();  
  45.         } finally {  
  46.               try {  
  47.                   rs.close();  
  48.                   ps.close();  
  49.                   conn.close();  
  50.               } catch (SQLException e) {  
  51.                   // TODO Auto-generated catch block  
  52.                   e.printStackTrace();  
  53.               }  
  54.         }  
  55.         return bt;  
  56.     }  
  57. }  
  58.  
在action里面调用getImage()方法并显示图片在页面上
  1. public ActionForward execute(ActionMapping mapping, ActionForm form,  
  2.               HttpServletRequest request, HttpServletResponse response) {  
  3.         // TODO Auto-generated method stub  
  4.              BlobDAO blobDAO = BlobDAO.getInstance();  
  5.         byte[] bs = blobDAO.getImage("1");  
  6.  
  7.         try {  
  8.  
  9.               response.getOutputStream().write(bs);  
  10.         } catch (IOException e) {  
  11.               // TODO Auto-generated catch block  
  12.               e.printStackTrace();  
  13.         }  
  14.         return null;  
  15.     }  
添加图片到数据库请在c盘下放入图片--c:\4.gif
  1. public void savaImg(String imgId) {  
  2.            //传的是存入数据库图片的id  
  3.            initConn();  
  4.            Statement st = null;  
  5.            BLOB blob = null; //图片类型  
  6.            OutputStream outputStream = null; //输出流  
  7.            File file = null; //文件  
  8.            InputStream inputStream = null; //输入流  
  9.            ResultSet rs = null;  
  10.            try {  
  11.                  conn.setAutoCommit(false); //事物由程序员操作  
  12.                  st = conn.createStatement();  
  13.                  st.executeQuery("insert into IMAGE_LOB values(""+ imgId +"",empty_blob())");  
  14.                  rs = st.executeQuery("select T_IMAGE from IMAGE_LOB where t_id=""+ imgId +"" for update");  
  15.                  if (rs.next()) {  
  16.                        blob = (BLOB) rs.getBlob(1);  
  17.                        outputStream = blob.getBinaryOutputStream();  
  18.                        file = new File("c:\4.gif");  
  19.                        inputStream = new FileInputStream(file);  
  20.                        byte[] b = new byte[blob.getBufferSize()];  
  21.                        int len = 0;  
  22.                        while ((len = inputStream.read(b)) != -1) {  
  23.                              System.out.println(len);  
  24.                              outputStream.write(b, 0, len);  
  25.                        }  
  26.                  }  
  27.            } catch (SQLException e) {  
  28.                  // TODO Auto-generated catch block  
  29.                  e.printStackTrace();  
  30.            } catch (FileNotFoundException e) {  
  31.                  // TODO Auto-generated catch block  
  32.                  e.printStackTrace();  
  33.            } catch (IOException e) {  
  34.                  // TODO Auto-generated catch block  
  35.                  e.printStackTrace();  
  36.            } finally {  
  37.                  try {  
  38.                        inputStream.close();  
  39.                        outputStream.flush();  
  40.                        outputStream.close();  
  41.                        rs.close();  
  42.                        st.close();  
  43.                        conn.commit();  
  44.                        conn.close();  
  45.                  } catch (IOException e) {  
  46.                        // TODO Auto-generated catch block  
  47.                        e.printStackTrace();  
  48.                  } catch (SQLException e) {  
  49.                        // TODO Auto-generated catch block  
  50.                        e.printStackTrace();  
  51.                  }  
  52.            }  
  53.      }  
Oracle创建修改系统参数后 不能启动的问题CentOS 5.4 安装 Tuxedo 8.0 操作步骤相关资讯      Oracle基础教程 
  • Oracle块编程返回结果集详解  (11/10/2013 10:45:58)
  • Oracle基础教程之设置系统全局区  (08/22/2013 14:24:00)
  • Oracle基础教程知识点总结  (06/18/2013 07:43:32)
  • Oracle基础教程之tkprof程序详解  (10/22/2013 11:49:50)
  • Oracle基础教程之sqlplus汉字乱码  (07/18/2013 16:30:00)
  • Oracle 管理之 Linux 网络基础  (02/16/2013 18:37:35)
本文评论 查看全部评论 (0)
表情: 姓名: 字数