Insert and display images in Oracle (BLOB type)

Source: Internet
Author: User

Insert and display images in Oracle (BLOB type)
Blob type for storing images in oracle


First, create a database:

-- Connect to the Administrator
Conn sys/tbsoft as sysdba;

-- Authorize scott user

Grant create any directory to scott;

-- Return to scott user

Conn scott/tiger;


-- Create a table for storing images
Create table IMAGE_LOB (T_ID VARCHAR2 (5) not null, T_IMAGE blob not null );

-- Create a directory for storing images
Create or replace directory images as 'C: \ picture ';

-- Create a folder named picture in c :.

Create or replace procedure IMG_INSERT (TID VARCHAR2, FILENAME VARCHAR2)
F_LOB BFILE; -- file type
B _LOB BLOB;
BEGIN
INSERT INTO IMAGE_LOB (T_ID, T_IMAGE)
VALUES (TID, EMPTY_BLOB () RETURN T_IMAGE INTO B _LOB;
-- Insert an empty blob.
F_LOB: = BFILENAME ('images', FILENAME );
-- Get files in the specified directory
DBMS_LOB.FILEOPEN (F_LOB, DBMS_LOB.FILE_READONLY );
-- Open a file in read-only mode
DBMS_LOB.LOADFROMFILE (B _LOB, F_LOB, DBMS_LOB.GETLENGTH (F_LOB ));
-- Transfer object
DBMS_LOB.FILECLOSE (F_LOB );
-- Close the original file
COMMIT;
END;
/


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

-- Save the image to a table
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.

Java code collection code

Public class BlobDAO {

Private static final BlobDAO instance = new BlobDAO ();

Private Connection conn = null;

Private BlobDAO (){

}

Public static BlobDAO getInstance (){
Return instance;
}

Private void initConn (){
Conn = DBAccess. getInstance (). getConn ();
}


Public byte [] getImage (String imgname ){
BufferedInputStream ins; // gets the blob io stream
Byte [] bt = null;

InitConn ();
Blob bo = null;
PreparedStatement ps = null;
ResultSet rs = null;
String SQL = "select T_IMAGE from IMAGE_LOB where t_id =? ";
Try {
Ps = conn. prepareStatement (SQL );
Ps. setString (1, imgname );
Rs = ps.exe cuteQuery ();
If (rs. next ()){
Bo = rs. getBlob ("T_IMAGE ");

Try {
Ins = new BufferedInputStream (bo. getBinaryStream ());
Int bufferSize = (int) bo. length (); // obtain the BLOB length.
Bt = new byte [bufferSize];
Try {
Ins. read (bt, 0, bufferSize );
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// Create a byte Cache
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

}
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Try {
Rs. close ();
Ps. close ();
Conn. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

Return bt;
}
}

 

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

Java code collection code

Public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response ){
// TODO Auto-generated method stub

BlobDAO blobDAO = BlobDAO. getInstance ();

Byte [] bs = blobDAO. getImage ("1 ");

Try {

Response. getOutputStream (). write (bs );

} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

Return null;
}


Add images to the database

Put the image in drive c: \ 4.gif

Java code collection code

Public void savaImg (String imgId ){
// Upload the id of the image stored in the database
InitConn ();
Statement st = null;
BLOB blob = null; // image type
OutputStream outputStream = null; // output stream
File file = null; // File
InputStream inputStream = null; // input stream
ResultSet rs = null;
Try {
Conn. setAutoCommit (false); // The transaction is operated by the programmer.
St = conn. createStatement ();
St.exe cuteQuery ("insert into IMAGE_LOB values ('" + imgId + "', empty_blob ())");
Rs = st.exe cuteQuery ("select T_IMAGE from IMAGE_LOB where t_id = '" + imgId + "' for update ");
If (rs. next ()){
Blob = (BLOB) rs. getBlob (1 );
OutputStream = blob. getBinaryOutputStream ();
File = new File ("c: \ 4.gif ");
InputStream = new FileInputStream (file );
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 ();
}

}
}

 


Operation completed!

Author "669098238"
 

Related Article

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.