This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Help-How to retrive binary object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Help-How to retrive binary object" Watch "Help-How to retrive binary object" New topic
Author

Help-How to retrive binary object

Saeyone Bala
Greenhorn

Joined: Oct 08, 2003
Posts: 2
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

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Sainudheen Mydeen
Ranch Hand

Joined: Aug 18, 2003
Posts: 218
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/jdbc.html
-Sainudheen
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

http://otn.oracle.com/sample_code/tech/java/sqlj_jdbc/files/advanced/LOBSample/Readme.html


Groovy
sing
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
hi,
if not mistaken, we can also use
String blob = rs.getString("user_data");
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

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

Joined: Aug 18, 2003
Posts: 218
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

Joined: Feb 27, 2002
Posts: 8903

FROM ORACLE db docs
Every BLOB variable stores a locator, which points to a large binary object
Maydene Fisher
Author
Greenhorn

Joined: Jul 22, 2003
Posts: 14
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<br />author, "JDBC(tm) API Tutorial and Reference, Third Edition"
Maydene Fisher
Author
Greenhorn

Joined: Jul 22, 2003
Posts: 14
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

Joined: Aug 18, 2003
Posts: 218
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

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.


[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
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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....
 
wood burning stoves
 
subject: Help-How to retrive binary object
 
Similar Threads
inserting image
problem in storing an object to the database!
convert blob to .xls
File upload in Struts
Binary to string conversion