How to store images in Oracle Using Stored Procedures

Source: Internet
Author: User

 

Address: http://database.51cto.com/art/201010/231598.htm

 

Oracle image storage is a feature that we often need to implement. The following describes how to use the stored procedure to store images in Oracle. If you have encountered any problems in Oracle image storage, take a look.

To store images in Oracle with the Blob type, first create the following in the database:

-- Connect to the Administrator

 
 
  1. conn sys/tbsoft as sysdba; 

-- Authorize Scott user

 
 
  1. grant create any directory to scott; 

-- Return to Scott user

 
 
  1. conn scott/tiger; 

-- Create a table for storing images

 
 
  1. CREATE TABLE IMAGE_LOB (T_ID VARCHAR2 (5) NOT NULL,T_IMAGE BLOB NOT NULL); 

-- Create a directory for storing images

 
 
  1. CREATE OR REPLACE DIRECTORY IMAGES AS 'C:\picture'; 

-- Create a folder named picture in C :.

 
 
  1. CREATE OR REPLACE PROCEDURE IMG_INSERT (TID VARCHAR2,FILENAME VARCHAR2) AS 

F_lob bfile; -- file type

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    

-- Insert an empty blob.

 
 
  1. F_LOB:= BFILENAME ('IMAGES', FILENAME); 

-- Get files in the specified directory

 
 
  1. DBMS_LOB.FILEOPEN(F_LOB, DBMS_LOB.FILE_READONLY); 

-- Open a file in read-only mode

 
 
  1. DBMS_LOB.LOADFROMFILE (B_LOB, F_LOB,DBMS_LOB.GETLENGTH (F_LOB)); 

-- Transfer object

 
 
  1. DBMS_LOB.FILECLOSE (F_LOB); 

-- Close the original file

 
 
  1. COMMIT;  
  2. END;  
  3. /  

-- Put an image 1.gif under c: \ picture.

-- Save the image to a table

 
 
  1. call IMG_INSERT('1','1.gif'); 

Create a web project to connect to the database and create a blobdao class to retrieve blob images in the table.

 
 
  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; // gets the Blob Io stream
  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.exe cutequery ();
  24. If (Rs. Next ()){
  25. BO = Rs. getblob ("t_image ");
  26. Try {
  27. INS = new bufferedinputstream (BO. getbinarystream ());
  28. Int buffersize = (INT) BO. Length (); // obtain the Blob length.
  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. // Create a byte Cache
  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.  

Call the getimage () method in the action and display the image on the page.

 
 
  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.     }  

Add images to the database

Put the image in drive c: \ 4.gif

 
 
  1. Public void savaimg (string imgid ){
  2. // Upload the ID of the image stored in the database
  3. Initconn ();
  4. Statement ST = NULL;
  5. Blob blob = NULL; // image type
  6. Outputstream = NULL; // output stream
  7. File file = NULL; // File
  8. Inputstream = NULL; // input stream
  9. Resultset rs = NULL;
  10. Try {
  11. Conn. setautocommit (false); // The transaction is operated by the programmer.
  12. St = conn. createstatement ();
  13. St.exe cutequery ("insert into image_lob values ('" + imgid + "', empty_blob ())");
  14. Rs = st.exe cutequery ("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. }
  54.  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.