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
posted
0
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
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
posted
0
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
Every BLOB variable stores a locator, which points to a large binary object
Maydene Fisher
Author
Greenhorn
Joined: Jul 22, 2003
Posts: 14
posted
0
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
posted
0
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
posted
0
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
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.