Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

MySQL Blob is shown being read as [B@da99836  RSS feed

 
Ranch Hand
Posts: 130
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been using Java and Hibernate for DAO. I stored a long text in on of the field which is MySQL blob and inserted the record in DB using MYSQL Query Browser. When I run select query it showing the string ok.

But When I read it in Java application and trying to show on JSP its been shown as [B@da99836. or some similar value. I debugged the application if its JSP issue.. even in Java application also its been read as same junk value.

Can someone help me with this.. Thanks in Advance.
 
Saloon Keeper
Posts: 5288
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you trying to show it, both in the JSP and in your Java code? Post relevant code excerpts.

Something like "[B@da99836" typically happens when the toString method is invoked  (directly or indirectly) on an object which does not meaningfully override that method.
 
Sheriff
Posts: 21650
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default toString format (if it's not overridden) is <class name>@<hash code as HEX>, and that's exactly what you're seeing here. The class name in this case is [B. The [ indicates it's an array; the B indicates the element type is byte. So what you're getting is a byte[], and you need to do something with that.
 
Marshal
Posts: 24198
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have an array of bytes. You can convert that in to a String like this:



This may work or it may not; the database designers may have had some nonstandard way of putting a string of characters into a Blob. Why isn't it a Clob if it contains text?
 
Bala Tilak
Ranch Hand
Posts: 130
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

byte[] bytes = /* something */;
String correspondingString = new String(bytes);




Got it now. So BLOB should be a byte[] in Java, not the String. So either I have to go for CLOB mapped to String in Java, or Go for BLOB and mapped it to byte[] in Java.

MySQL  -- Java

BLOB --> byte[]
CLOB --> String.


Thanks you all. I can't update the DB now , so I have changed the filed to byte[] in Java mapping file and then converting it to String using new String(byte[]) after reading from DB.


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!