Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help-How to retrive binary object

 
Saeyone Bala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following problem for my assignment and need help in coding can anyone help? I am new to java and jdbc
Provide a brief code sample showing how to retrieve binary object data from a database using a PreparedStatement and ResultSet object. Assume the database table has the following structure and your program should retrieve the serialized "user_data" object stored for the user_id = 1:
Column NameDatatype
user_idnumber(20)
user_namevarchar(20)
user_dataBLOB
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saeyone Bala:

Provide a brief code sample showing how to retrieve binary object data from a database using a PreparedStatement and ResultSet object. Assume the database table has the following structure and your program should retrieve the serialized "user_data" object stored for the user_id = 1:
Column NameDatatype
user_idnumber(20)
user_namevarchar(20)
user_dataBLOB

PreparedStatement ps = yourDBConnection.prepareStatement(yourSqlString);
ResultSet rs = ps.executeQuery();
while(rs.next()){
java.sql.Blob blob = rs.getBlob("user_data");
//You may put it in a Vector or List...
}
You get each blob one by one record and put them in a list or vector... I hope it might help you...
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
 
sing
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
if not mistaken, we can also use
String blob = rs.getString("user_data");
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steffy Sing:
hi,
if not mistaken, we can also use
String blob = rs.getString("user_data");

It wont work
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep
Any idea why? I have no experience with BLOB. If your table has a 'user_id' column with NUMBER datatype, still you can write
String u_id = rs.getString("user_id");
But I know the reverse will not work all the times as you cannot convert all VARCHAR to numerical type. Does BLOB follow any strict conversion rule?
-Sainudheen
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FROM ORACLE db docs
Every BLOB variable stores a locator, which points to a large binary object
 
Maydene Fisher
Author
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saeyone Bala:
I have the following problem for my assignment and need help in coding can anyone help? I am new to java and jdbc
Provide a brief code sample showing how to retrieve binary object data from a database using a PreparedStatement and ResultSet object. Assume the database table has the following structure and your program should retrieve the serialized "user_data" object stored for the user_id = 1:
Column NameDatatype
user_idnumber(20)
user_namevarchar(20)
user_dataBLOB
 
Maydene Fisher
Author
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steffy Sing:
hi,
if not mistaken, we can also use
String blob = rs.getString("user_data");

Hi Steffy,
Sorry, but you can use only the methods getBlob and getObject to retrieve an SQL BLOB value.
Cheers,
Maydene Fisher
 
Sainudheen Mydeen
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maydene Fisher
If I am not wrong, you are the Author of JDBC API Tutorial and Reference, Third Edition.
Originally posted by Maydene Fisher:

Hi Steffy,
Sorry, but you can use only the methods getBlob and getObject to retrieve an SQL BLOB value.
Cheers,
Maydene Fisher

Every BLOB variable stores a locator, which points to a large binary object

Is this the reason? Or something else?

-Sainudheen
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use getBytes() or getBinaryStream() to get a Blob value. Although judging by the previous posts, I have to wonder if this is database specific. I have used both of these in Db2.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steffy Sing:
hi,
if not mistaken, we can also use
String blob = rs.getString("user_data");

Surely it won't work... But if it is CLOB, you can convert it to the string using resultSet.getClob("columnName") and subString it....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic