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.
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 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...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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.
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...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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 and learn the language basics before trying to write non-trivial Java applications.
Oxygen is a highly toxic and corrosive gas...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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!
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.