Working with large objects in databases often requires storing and retrieving files like images, videos, audio, and documents. There are two types of large objects: Binary Large Objects (BLOBs) for binary files up to 4GB, and Character Large Objects (CLOBs) for text files up to 4GB. To insert BLOBs, represent the file as an input stream, prepare a statement with a BLOB parameter, set the stream on the parameter, and execute. To retrieve, get the stream from the result set and write it to a file output stream. CLOB insertion is similar but uses a character stream instead of a binary stream.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
65 views
Unit-4 PPT-8
Working with large objects in databases often requires storing and retrieving files like images, videos, audio, and documents. There are two types of large objects: Binary Large Objects (BLOBs) for binary files up to 4GB, and Character Large Objects (CLOBs) for text files up to 4GB. To insert BLOBs, represent the file as an input stream, prepare a statement with a BLOB parameter, set the stream on the parameter, and execute. To retrieve, get the stream from the result set and write it to a file output stream. CLOB insertion is similar but uses a character stream instead of a binary stream.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17
Working with large objects
(BLOB and CLOB)
Sometimes as the part of programming requirement, we have to insert and retrieve large files like images, video files, audio files, resume etc with respect to database. Eg: upload image in matrinomial web sites upload resume in job related web sites To store and retrieve large information we should go for Large Objects(LOBs). There are 2 types of Large Objects. 1. Binary Large Object (BLOB) 2. Character Large Object (CLOB) 1) Binary Large Object (BLOB) A BLOB is a collection of binary data stored as a single entity in the database. BLOB type objects can be images, video files, audio files etc.. BLOB datatype can store maximum of "4GB" binary data. 2) CLOB (Character Large Objects): A CLOB is a collection of Character data stored as a single entity in the database. CLOB can be used to store large text documents(may plain text or xml documents) CLOB Type can store maximum of 4GB data. Eg: hydhistory.txt Steps to insert BLOB type into database: 1. create a table in the database which can accept BLOB type data. create table persons(name varchar2(10),image BLOB);
2. Represent image file in the form of Java File object.
File f = new File("katrina.jpg");
3. Create FileInputStream to read binary data represented by image file
FileInputStream fis = new FileInputStream(f);
4. Create PreparedStatement with insert query.
PreparedStatement pst = con.prepareStatement("insert into persons values(?,?)"); 5. Set values to positional parameters. pst.setString(1,"katrina"); To set values to BLOB datatype, we can use the following method: setBinaryStream() public void setBinaryStream(int index,InputStream is) public void setBinaryStream(int index,InputStream is,int length) public void setBinaryStream(int index,InputStream is,long length) Eg: pst.setBinaryStream(2,fis); ➔ Oracle 11g pst.setBinaryStream(2,fis,(int)f.length()); ➔ Oracle 10g 6. execute sql query pst.executeUpdate(); 1. Prepare ResultSet object with BLOB type RS rs = st.executeQuery("select * from persons");
2. Read Normal data from ResultSet
String name=rs.getString(1);
3. Get InputStream to read binary data from ResultSet
InputStream is = rs.getBinaryStream(2); 4. Prepare target resource to hold BLOB data by using FileOutputStream FOS fos = new FOS("katrina_new.jpg"); 5. Read Binary Data from InputStream and write that Binary data to output Stream. CLOB (Character Large Objects): A CLOB is a collection of Character data stored as a single entity in the database. CLOB can be used to store large text documents(may plain text or xml documents) CLOB Type can store maximum of 4GB data. Eg: hydhistory.txt Steps to insert CLOB type file in the database: All steps are exactly same as BLOB, except the following differences 1. Instead of FileInputStream, we have to take FileReader. 2. Instead of setBinaryStream() method we have to use setCharacterStream() method. public void setCharacterStream(int index,Reader r) throws SQLException public void setCharacterStream(int index,Reader r,int length) throws SQLException public void setCharacterStream(int index,Reader r,long length) throws SQLException What is the difference between BLOB and CLOB? • We can use BLOB Type to represent binary information like images, video files, audio files etc • Where as we can use CLOB Type to represent Character data like text file, xml file etc...