Click to See Complete Forum and Search --> : How to dispaly blob data type in jsp page


razia5
December 31st, 2002, 03:28 AM
Hi I am using the mysql database and i have a field of type blob but when I try to retrieve the values from database in my JSP page, I get some weird characters being dispalyed instead of the text stored in the blob fields. plz could you help me.

dlorde
December 31st, 2002, 04:48 AM
A blob is just a chunk of binary data. If it has some hidden type formatting of displayable data, you need to convert it to the required types. java.sql.Blob (http://java.sun.com/j2se/1.4.1/docs/api/java/sql/Blob.html) allows you to extract the blob data as a an array of bytes or via an InputStream. If you wrote text into the blob (from a String, with the Blob OutputStream as a DataOutputStream?), you need to reverse the procedure to get text out. Without knowing what data types you stored in the blob or how you stored them, it's hard to be more specific.



as the car gathered speed, she pumped the brakes repeatedly, but they were useless...

razia5
December 31st, 2002, 05:30 AM
I am storing text in the blob. So, could you plz be more specific about how to retrieve the text from the blob in my jsp page?

dlorde
December 31st, 2002, 05:47 AM
Text is not a Java type. Java characters are 2 byte UNICODE. What type did you store the text as? Just how was the data stored?

he crawled to the top of the nearest dune, only to find featureless desert stretching to the horizon on all sides...

razia5
January 1st, 2003, 08:38 PM
I do not really understand your question. In mysql, I can store it as a .txt file or just type the text I want to store in the BLOB field.

dlorde
January 2nd, 2003, 11:13 AM
A BLOB (Binary Large OBject) is just an anonymous chunk of binary data. To extract anything useful from it, you need to know exactly what you put into it. A .txt file can contain pretty well anything, and keyboard input can be stored in any number of ways (depending on the operating system, the application used for input and storage, etc).

So when you say you have text stored in the BLOB, it means very little. What counts is how the text is represented - is it ASCII (single byte), UNICODE (2 byte), was it generated in Windows or Unix (different line terminators), etc?

Only when you know the format of the data in the BLOB can you expect to extract it and display it correctly.

By the way, why not use a character-based storage type (VARCHAR or similar) rather than a BLOB? It might save a bit of messing about...

A wise man butters no parsnips...

razia5
January 3rd, 2003, 08:59 PM
Hi again. I need to use blob type since I need to store a few sentences in that field and varchar would not be large enough. Also, i can store the info in a .txt file or can type the info I was to store in a section specified for that in mysql. I do not know if it is bein represented as UNICODE or ASCII. What i know is that, as I type my info to be stored, it is being represented in hexadecimal as well. Does not tell u anything? Plz help me.

dlorde
January 6th, 2003, 06:20 PM
What can I say? You need to exactly what you are storing if you want to extract it in a known way... If you write to a java.sql.Blob class, or output text to a database BLOB using a Java OutputStream class, you should be able to reverse the process and read the contents of the java.sql.Blob or database BLOB using a Java InputStream class. Java can read what Java writes. If the BLOB contents were not written by Java, you need to know exactly how they are formatted if you want to use Java to extract and display them the way you want.

Why not use a HEX editor to have a look at the BLOB contents and compare them with the equivalent text in Java?

Perhaps it might help if you described how the displayed text is different from what you expected... are all the characters different? Just some? Just the line terminators?

the noise became so piercing, he felt his eardrums might burst...

razia5
January 7th, 2003, 04:15 AM
When I use BLOB data type and try to retrieve the values, I get things like
[B@128654

instead of

testtest

Also, in the documentation for mysql, I found that BLOB is equivalent to BINARY VARCHAR.

So, now does it give you a better idea of how I could retrieve the information in a JSP page.

Thanks in advance.

dlorde
January 7th, 2003, 04:21 AM
> I get things like [B@128654

That looks like the result of the default Object.toString() method - an object reference identifier followed by it's address in memory.

How are you trying to retrieve the values and display them (the code would be best)?

She heard the warning hiss too late. Before she could react, the snake had struck...

razia5
January 8th, 2003, 12:29 AM
Hi plz find attached some codes i wrote in my jsp page to display the values in the field QName which is type BLOB

dlorde
January 8th, 2003, 05:23 AM
OK, it's pretty obvious why you get something that looks like the result of the default Object.toString() method - it's because you're trying to display an Object. You're getting the data from the ResultSet using the ResultSet.getObject(...) method (why?), so naturally what you get back is just an Object... and, as you know, if you try and display an Object instance, the runtime has no idea what's in it, so all it can do in toString() is display the internal ID tag for the Object, and its address in memory.

The whole point of ResultSet is that you can extract the data you have requested in whatever format is appropriate (i.e. if you stored a String, you use ResultSet.getString(...) to retrieve it). Did you look at the ResultSet JavaDoc? Didn't the getBlob(...) methods catch your eye?

Another tip - as I said before, a java.sql.Blob is just a chunk of raw binary data, so implementations won't override the toString() method. Use the getBytes(...) method, or better still, the getBinaryStream() method to get the data in a form you can convert to a String for display.

Final tip - read the Java Tutorial (http://java.sun.com/docs/books/tutorial/) and learn the language basics before trying to write non-trivial Java applications.

Oxygen is a highly toxic and corrosive gas...

razia5
January 9th, 2003, 12:54 PM
Hi
I tried what you told me but it is still not working correctly.

This is what i included in the JSP page:

<%Blob test =RsQuestions.getBlob("QName"); %><%=test.getBinaryStream()%>

And this is what I get:
java.io.ByteArrayInputStream@309f9f

Do you know what the problem is? I also tried the getBytes method. Still the same problem.
Thanks in advance.

dlorde
January 10th, 2003, 04:36 PM
You didn't quite do what I said: "Use ... the getBinaryStream() method to get the data in a form you can convert to a String for display".

Why am I doing this?

To display the contents of an object, it needs to be converted to a String. This is what the toString() method every class inherits from Object is meant to do, and this is what is called when you use the JSP <%= ...> tag to display an object.

However, as I feel sure I've said before, the default implementation supplied with Object is to return a String containing the instance ID and address. This is because an Object could be anything, so what else can it output? Any class that wants to return something different in that String (i.e. a string representation of its contents) has to override the toString() method to generate something appropriate.

Now think about it - a BinaryStream is just that, a stream of binary data. Just like Object (and Blob) it could contain anything, so the class writers couldn't override the toString() method to return something more interesting. In fact none of the Java InStreams or Readers (not even the FileReader class) overrides the toString() method - why? - well apart from the reason already given, the clue is in the definition of 'stream'.

So what you have to do is to convert the contents of the BinaryStream into a String (or Strings, depending on its length).

Hint: the read(...) method will fill a byte array with bytes from the stream, and String has a constructor that takes a byte array.

Of course, you could just use the Blob.getBytes(...) method in a loop to get arrays of bytes. I only suggested BinaryStream because it's generally more flexible...

I don't want to be overly critical, but you don't seem to be thinking about what you're trying to do. Unless you understand the basics of Java, think about what the classes you're trying to use do (see the tutorials and the JavaDoc), and think about exactly what you're trying to do, you're going to be taking three steps forward and two steps back the whole way.

Fly my beauties, fly... silence those meddlesome fools once and for all!

razia5
January 15th, 2003, 10:01 AM
Hi again . I tried and tried and tried what you have told me but still cannot get the grasp of it and I am still not able to retrieve a blob type from my database. Could you plz plz plz tell which lines of codes to write to do such retrieval. I am tired of trying. Plz help.

dlorde
January 15th, 2003, 11:14 AM
> I am tired of trying

You're tired of trying?

how do you think I feel? :rolleyes:

Assuming you can fit the whole blob into a single string, it's probably something like this:<%Blob textBlob = RsQuestions.getBlob("QName");
String text = new String(textBlob.getBytes(1, (int)textBlob.length()));%>
<%=text %>If you can't get it all into a single string (or if it comes out as junk):try {
Blob blob = getBlob();
BufferedReader reader = new BufferedReader(
new InputStreamReader(RsQuestions.getBlob"QName").
getBinaryStream()));
String line = reader.readLine();
while (line != null) {
// Output line here: <%=line%>
line = reader.readLine();
}
}
catch(SQLException sqe) {
handleException(sqe);
}
catch (IOException ioe) {
handleException(ioe);
}I'll leave you to turn it into JSP.

If you get junk text coming out, you'll need to pass a Charset to the InputStreamReader along with the BinaryStream, to do the conversion from the blob bytes to Java's UNICODE characters. For example, to convert from Seven-bit ASCII (ISO646-US) coded bytes, pass in Charset.forName("US-ASCII"). Likewise for the ISO Latin Alphabet No. 1, (ISO-LATIN-1), pass in Charset.forName("ISO-8859-1"). See Charset (http://java.sun.com/j2se/1.4/docs/api/java/nio/charset/Charset.html) for details. Of course, it helps if you know what the character encoding of the bytes in the blob are in the first place!

Not tonight dear, I had a really tiring day at work today...

razia5
January 17th, 2003, 12:10 PM
Thank you very much for your help. It's finally working, all thanks to you.

dlorde
January 17th, 2003, 01:28 PM
Excellent! success at last :D

A flea met a fly in a flue,
Said the flea 'let us fly',
Said the fly 'let us flee',
So they flew through a flaw in the flue...

basilication
June 12th, 2004, 06:25 PM
I tried to use that code but I got this error:

java.lang.AbstractMethodError: org/gjt/mm/mysql/ResultSet.getBlob

here is the code, could you help me please?:


<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*, java.io.*" errorPage="" %>
<%@ include file="../Connections/Mysql.jsp" %>
<%
Driver DriverBLOB1 = (Driver)Class.forName(MM_Mysql_DRIVER).newInstance();
Connection ConnBLOB1 = DriverManager.getConnection(MM_Mysql_STRING,MM_Mysql_USERNAME,MM_Mysql_PASSWORD);
PreparedStatement StatementBLOB1 = ConnBLOB1.prepareStatement("SELECT * FROM blob1");
ResultSet BLOB1 = StatementBLOB1.executeQuery();
boolean BLOB1_isEmpty = !BLOB1.next();
boolean BLOB1_hasData = !BLOB1_isEmpty;
Object BLOB1_data;
int BLOB1_numRows = 0;
%>
<HTML>
<BODY>
<%

try {

BufferedReader reader = new BufferedReader(
new InputStreamReader(BLOB1.getBlob("blob1").
getBinaryStream()));
String line = reader.readLine();
while (line != null) {
// Output line here: <%=line
line = reader.readLine();
}
}
catch(SQLException sqe) {

}
catch (IOException ioe) {

}%>

</BODY>
</HTML>
<%
BLOB1.close();
StatementBLOB1.close();
ConnBLOB1.close();
%>

dlorde
June 13th, 2004, 05:20 PM
I dunno... maybe your JDBC driver is obsolete and doesn't support the getBlob() method... If it's any earlier than Java 1.2, it won't.

4000 feet up, exposed on the south col, he was caught in the whirling nightmare of the blizzard...

cjard
June 15th, 2004, 01:01 PM
you need to refer to your driver documentation for details on blob access; typically drivers dont provide BLOBs as objects like a whole String() anyway; they provide read/write access to them via input/output streams.. read your driver documentation

finally, may i ask why you are using a BLOB to store text, when a CLOB exists?