Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to binary conversion

 
Rosu Thomas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i am getting a string "0x01" from textbox of my jsp page.i want to store it in my table as binary.how to do this using preparedstatement of mysql. Also like this i need to store binary of variable length in my table.please give me a solution.
thanks in advance
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rosu,

There are various ways to store binary data in DB tables:

Convert the data to binary, and then encode it to Base64 encoding. Apache provides a ready-made implementation for Base64 encoding-decoding.
Pros: Base64 is pure char format. So this data can be stored in any varchar column. Also, this is easier to manage than blob.
Cons: When a byte-buffer is converted to Base64 format, it's size is increased (typically by 33%) - and varchar column generally has limit of 4000 chars.

Store data as blob.
Pros: Data can be directly stored as binary. No need to do any encoding-decoding. Also, large objects can be stored (e.g. zipped files etc.)
Cons: If original data is not in byte format, then char-to-byte conversion has to be done manually - which is more error prone than Base64 encoding-decoding.

There is a third way of doing it via RAW datatype (which is supported on Oracle) - however, I'm not sure if that is supported on MySql.

I hope this helps.
 
Rosu Thomas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
thanks for responding to my query.actually i don't want to convert my data to hex.i just want to store it in my column of type BINARY(3).
for example,i am getting "23" from my textbox..and i want to store it in my key-id column of type BINARY(3) as 23 itself..i am using preparedstatement for inserting it in my table.
please revert ASAP.
thanks in advance
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I'm confused. If your column is binary, then 23 will be stored in binary format only(and not in decimal/string format).

If you want to store 23 in decimal/string format, you may need to change column type accordingly.

If, due to some reason, changing column type is not possible, then I guess conversion (either manual or via utility - as mentioned in my previous post) is the only way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic