Home arrow static arrow Java Programming [Archive] - how do you use BLOB
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how do you use BLOB
This topic has 15 replies on 2 pages.    1 | 2 | Next »

Posts:343
Registered: 7/13/04
how do you use BLOB  
Jul 27, 2004 4:07 PM



 
Hi, i've read the sun page explaining the interface BLOB, and i've read the posts, but i still cant find a good explanation on how to do it. I just have a byte array, and want to store it in my database as a BLOB. how is this done?
 

Posts:27,518
Registered: 11/3/97
Re: how do you use BLOB  
Jul 27, 2004 4:51 PM (reply 1 of 15)



 
When I search using "blob" in the forum called "Java Database Connectivity (JDBC) & Transactions (JTA/JTS) " I get 600+ responses.

The very first one has a code example. I would suppose other do as well.
 

Posts:18,384
Registered: 21.03.00
Re: how do you use BLOB  
Jul 27, 2004 11:37 PM (reply 2 of 15)



 
Hi,

It depends on what database you use.

/Kaj
 

Posts:3,055
Registered: 18/06/98
Re: how do you use BLOB  
Jul 28, 2004 6:23 AM (reply 3 of 15)



 
First of all, you need to have a table with a BLOB column. The exact name of the BLOB type depends on your data base.

In SQL Server: "image"
In Oracle: "blob"
 

Posts:343
Registered: 7/13/04
Re: how do you use BLOB  
Jul 29, 2004 4:42 PM (reply 4 of 15)



 
Hi, well, i've decided to do even MORE research (I descovered that access is ****) and now using MySQL, i ALMOST created a blob, the column name is "RawData". here is my relevent code:
  public static void main(String [] args) {    int id = 1;    byte [] data = new byte[255];    int i = 0;    Access test = new Access();    for (i=0;i<255;i++)      data[i] = 'x';    try {       test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul","123");      ResultSet rs = test.query("Select * from FrameData where ID="+id);       rs.next();      int pid = rs.getInt("ParentId");        Blob blob = rs.getBlob("RawData");//the value of blob at here is: null      PreparedStatement pstmt = test.dbConn.prepareStatement(        "UPDATE FrameData SET RawData = ? WHERE ID = "+id); //and that is what i believe causes me to get a null pointer exception here      blob.setBytes(0,data);      pstmt.setBlob(1, blob);      pstmt.executeUpdate();    } catch (Exception error) {        error.printStackTrace();    }  }


I just dont see how there is anything wrong with the code (obvoiusly i'm learning how to use blob's) anyone know why my blob creation isn't working?

 

Posts:343
Registered: 7/13/04
Re: how do you use BLOB  
Jul 29, 2004 5:01 PM (reply 5 of 15)



 
I guess I can see how this topic can be looked over:

A: there are a lot of interesting topics right now
B: This question is too difficult (nobody knows the answer)
C: This question is too easy
D: This question is just too dang boring!
 

Posts:343
Registered: 7/13/04
Re: how do you use BLOB  
Jul 29, 2004 5:36 PM (reply 6 of 15)



 
anyone,
any input would be good input
 

Posts:2
Registered: 7/26/04
Re: how do you use BLOB  
Jul 29, 2004 6:05 PM (reply 7 of 15)



 
hello moerderin :
I meet this problem 3 months ago.I use the DB is MSSQL2000 and Sybase 11.9.2.In most of Databases ,the type of "text","ntext","image" and some other type that defined yourself are stored as BLOB type.In the online help doc of MSSQL2000,it said that anybody who want to use the BLOB data should use the DB`API directly(the SQL itself can not directly operate that) :( So I guess the fact that other DB are all the same. If you want to use BLOB you should read the doc of the DB that you use now,not use the SQL only:)

good luck
------krqi
 

Posts:343
Registered: 7/13/04
Re: how do you use BLOB  
Jul 29, 2004 6:42 PM (reply 8 of 15)



 
good luck
------krqi

thnx for your input
 

Posts:27,518
Registered: 11/3/97
Re: how do you use BLOB  
Jul 30, 2004 9:25 AM (reply 9 of 15)



 

Blob blob = rs.getBlob("RawData");//the value of blob at here is: null

And how did you get data into the database in the first place? If there is no data then this will be null.
 

Posts:343
Registered: 7/13/04
Re: how do you use BLOB  
Jul 30, 2004 9:39 AM (reply 10 of 15)



 

Blob blob = rs.getBlob("RawData");//the value of
blob at here is: null

And how did you get data into the database in the
first place? If there is no data then this will be
null.

I at least have something that works now. the reason the field is null, is because i want to put, the value of the byte array INTO that field. when i set the field to a value of 1, my program seems to be working (here is the slightly modified, but working version): (i wish there was a way to update the "rawdata" field without there having to be an initial value)

    int id = 1;    byte [] data = new byte[255];    int i = 0;    byte y = 0;    Access test = new Access();    for (i=0, y = 1;i<255;i++, y ++)      data[i] = y;    try {       test.connect("com.mysql.jdbc.Driver","jdbc:mysql://localhost/mdars","bfaul","123");      ResultSet rs = test.query("Select * from FrameData where ID="+id);            rs.next();            PreparedStatement pstmt = test.dbConn.prepareStatement(        "UPDATE FrameData SET RawData = ? WHERE ID = "+id );                    Blob blob = rs.getBlob("RawData");            blob.setBinaryStream(1);      blob.setBytes(1,data);      pstmt.setBlob(1, blob);            pstmt.executeUpdate();    } catch (Exception error) {        error.printStackTrace();    }  }
 

Posts:6,147
Registered: 11/9/00
Re: how do you use BLOB  
Jul 30, 2004 11:54 AM (reply 11 of 15)



 
Personally I think the Blob is a bit of a disaster area in JDBC specifications, which seem to have been written by someone who didn't really understand the concept.. In most database systems Blob/Clob data is stored separately, tied to records via some kind of handle in the database record. There's no reason why many database records can't point to the same blob (and excellent reasons why they should).

Deleting a database record won't normally delete an associated blob. JDBC supplies no method of generating new Blobs, only copying them from an existing record.

I've found, with postgress, I can write my own skeletal class implementing Blob. All it has to do is supply a getOutputStream and a getLength method (all the rest throw UnsupportedOperationException). This is the one way I've found to create a new record with Blob data in it. I only worked out how to do this by examining the driver code (the documentation, as you''ve noticed, is useless). This approach might or might not work with other databases.

As far as I can see Blob and Clob classes work backwards. You ought to load data into a Blob by writing to it, and retrieve data by reading from it. You ought to be able to read and write a blob independantly of the actual SQL calls. Certainly there should be a createBlob method in the Connection or Statement interfaces.

And when you obtain a blob from one database record and insert it in another it ought to just copy the handle, not the data.

 

Posts:6,147
Registered: 11/9/00
Re: how do you use BLOB  
Jul 30, 2004 12:42 PM (reply 12 of 15)



 
Oops! It's actually Blob.getBinaryStream you have to implement, not getOutputStream.
 

Posts:21,718
Registered: 98-02-20
Re: how do you use BLOB  
Aug 2, 2004 11:51 AM (reply 13 of 15)



 
Duke me.

%
 

Posts:21,718
Registered: 98-02-20
Re: how do you use BLOB  
Aug 2, 2004 11:52 AM (reply 14 of 15)



 
Question: you offered 10 Dukes for this question. Why didn't you award them to the folks that gave you help on this one?

You should be awarding Dukes for both after all the help you've gotten.

%
 
This topic has 15 replies on 2 pages.    1 | 2 | Next »