If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Loading blobs into table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-15-04, 12:12
Barry Lay Barry Lay is offline
Registered User
 
Join Date: Apr 2004
Posts: 6
Question Loading blobs into table

I have a dump file from an Informix table with blob data represented as hex within the delimited file. I wish to load this into a similarly defined DB2 table. The DB2 LOAD process loads the table Ok but the blob data got interpreted as text - I now have the hex equivalent of what I want in the blob column.

I thought to write a small Java program to do the load myself but ran into problems with the blob. The Blob datatype does not have a public construtor. I presume this is because it represents a reference to a server-side data block and thus needs to be created in the context of the connection. After playing around with prepared statements and examining the samples I ended up creating a one-row table with a blob column that I can query. This gives me a Blob in my client program, but when I try to load it with setBytes I get an AbstractMethodError.

Can anybody help with this error? Also, is there a more direct way to construct a blob in the client to load into a table? I don't have the blob data in a separate file which most of the samples seem to expect. I have it as part of a delimited file and it needs further processing before loading.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 04-15-04, 16:23
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Re: Loading blobs into table

Quote:
Originally posted by Barry Lay
This gives me a Blob in my client program, but when I try to load it with setBytes I get an AbstractMethodError.
See if this helps:

http://www.dbforums.com/t993796.html

Have you tried this:
Code:
PreparedStatement stmt = connection.prepareStatement("UPDATE BLOBTABLE SET BLOBCOLUMN=? WHERE KEYCOLUMN=?");

stmt.setBinaryStream(1, yourInputStream);
stmt.setInt(2, keyValue);

stmt.executeUpdate();
Reply With Quote
  #3 (permalink)  
Old 04-16-04, 12:52
Barry Lay Barry Lay is offline
Registered User
 
Join Date: Apr 2004
Posts: 6
I didn't quite understand the outcome of the referenced post. I will give setBinaryStream a try, although it does seem like a long way around. I have to extract the data from a delimited file, convert the hex to binary, then wrap a StringBufferInputStream around that. I was also hoping to use SQLj rather than JDBC for this since I have a lot of code in place already that uses SQLj.

It would appear that Oracle's support for blobs is better in this case. I didn't really have a problem with blobs on that database.

I noticed that I forgot to mention the platform: UDB 8.1.2 FP3 on RedHat ES 2.1. Client is 8.1.2 GA on Windows XP Pro, JDK 1.4.1_02.
Reply With Quote
  #4 (permalink)  
Old 04-16-04, 13:06
J Petruk J Petruk is offline
Registered User
 
Join Date: Mar 2004
Location: Toronto, ON, Canada
Posts: 513
In the samples/java/sqlj directory, see the sample BlobClobDemo.sqlj - it has what I suspect you're trying to do.
__________________
--
Jonathan Petruk
DB2 Database Consultant
Reply With Quote
  #5 (permalink)  
Old 04-16-04, 13:49
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally posted by Barry Lay
I didn't quite understand the outcome of the referenced post.
AbstractMethodError might indicate that you're using a version of JDBC driver that does not support JDBC 2.0. The referenced post shows how to activate newer driver.

Quote:
I will give setBinaryStream a try, although it does seem like a long way around.
The way you chose originally looks longer to me...
Reply With Quote
  #6 (permalink)  
Old 04-19-04, 17:18
Barry Lay Barry Lay is offline
Registered User
 
Join Date: Apr 2004
Posts: 6
Unhappy

I had a look at the Blob object that is returned by the getBlob() method (through reflection) and it looks like the only useful contrete method it implements is getBinaryStream(). Its superclass implements the rest of the methods in java.sql.Blob as abstract which is why I was getting the AbstractMethodError. I guess that IBM hasn't got around to coding the rest of the interface.

The object returned from getPreparedStatement does have a concrete implementation of setBinaryStream so it looks like I will have to do it this way. I will also check the latest fixpacks to see if the support for the other methods has been added.

A consequence of this omission is that manipulating existing blobs is going to be awkward - it will require the use of input and output streams as the blob returned from the result set can not be directly modified. It also means that you can't use SQLj with blobs for anything more complicated than copying them from one table to another.
Reply With Quote
  #7 (permalink)  
Old 04-19-04, 18:26
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally posted by Barry Lay
It also means that you can't use SQLj with blobs for anything more complicated than copying them from one table to another.
I suggest you do some more research before coming to the final conclusion...

http://publib.boulder.ibm.com/infoce...bDemo-sqlj.htm
Reply With Quote
  #8 (permalink)  
Old 04-20-04, 14:13
Barry Lay Barry Lay is offline
Registered User
 
Join Date: Apr 2004
Posts: 6
Smile

Thank you. That's what I needed. I saw a reference to this example in an earlier post but it was not installed with my client.

I played around with the second part of the example dealing with input and found that the getBytes method does not work for me (it returns an empty array). The getBinaryStream method does work so I was able to extract the data into an array.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On