GeeCON Prague 2014*
The moose likes JDBC and the fly likes how to create BLOB and CLOB data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to create BLOB and CLOB data" Watch "how to create BLOB and CLOB data" New topic
Author

how to create BLOB and CLOB data

Madhusudan Joshi
Greenhorn

Joined: Oct 08, 2010
Posts: 1
hi friends
can you please help me..

how store BLOB data in Oracle database and then how to get those data to my java program....
i am unable to store these type of data in db....

please
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30595
    
154

Madhusudan,
Welcome to JavaRanch!

What code do you have so far? What error message is it giving>

I usually use setBinaryStream for BLOBs on a prepared statement to update.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

I usually use setBinaryStream for BLOBs


And can we use StringBuffer data to writing data to a CLOB .


Save India From Corruption - Anna Hazare.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

In my opinion the hard part is creating the blob/clob and getting its handle. If you are at least on Oracle 10.2 and habe Oracle 10g JDBC Driver, you could try this. (I've devised this technique from the Oracle JDBC Guide, which I strongly suggest for reading.)

Let's have a table:

Inserting a blob then could look like:

I have just taken part of my code and tried to remove my specific logic. If it doesn't compile or work, I'll try to revisit it. A few additional notes:

The technique described here works by creating an empty blob in the database "in the right place" and returning its handle. Although you can create a blob using other means, these techniques often cause a temporary blob to be created, which is then copied when you actually insert or update it. For really, really large data this overhead might be noticeable.

Once you have the Blob, you can set its data using any of its methods you like or need, eg. using the setBinaryStream method, as mentioned above.

Remember to process transactions correctly. I'd definitely advice to turn off autocommit and commit the transaction manually, I honestly don't know what would happen to the blob handle if the insert statement got commited. However, the transactional logic would be clearly broken if you allowed autocommit to happen.

If you want to update a blob, you can either update its field to an empty_blob() value as shown here ("update mytable set data=empty_blob() where id=? returning data into ?"), or just read the blob handle (select data from mytable where id=?) without the need for Oracle-specific methods.

I think you could hide the SQL statement with the returning into clause into a stored procedure which would return you the Blob and then use standard callable statement to get the handle, thus avoiding the need for Oracle-specific methods. However I personally haven't tried this. Maybe this way it could be made somehow database independent, though I'm definitely not sure of it.

You can bind clob data directly as Strings, using PreparedStatement.setString() method. Assuming the data field was clob, not blob, you could use:

The default String length limit for this is 32 KB; to increase the limit, see http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jdbc/clob10g/handlingclobsinoraclejdbc10g.html.


Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Martin Vajsar wrote:If you want to update a blob, you can either update its field to an empty_blob() value as shown here ("update mytable set data=empty_blob() where id=? returning data into ?"), or just read the blob handle (select data from mytable where id=?) without the need for Oracle-specific methods.

I need to correct myself: for the second way to work, the row would need to be locked in Oracle: select data from mytable where id=? for update. I've posted link to this post on this forum recently, so I feel I should correct this mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create BLOB and CLOB data