File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and the fly likes How to read and write binary Bytes in MySQL ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to read and write binary Bytes in MySQL ?" Watch "How to read and write binary Bytes in MySQL ?" New topic

How to read and write binary Bytes in MySQL ?

Jim Smithian

Joined: Sep 21, 2004
Posts: 18

I am working on a Java-MySQL project to compute the hash value of a document using Java MessageDigest. The output is 16 bytes of type Java 'byte'. I store the value in MySQL CHAR types(using Jdbc). When I read back the stored value from MySQL using resultSet.getBytes(); the read hash value is not the same as the stored value. Some characters are differenet. I think the problem must be from a mismatch between MySQL and Java data types or a character coding format.

Can any body please help me how to solve this problem. What data types I should use in MySQL and Java? what character coding should I use?

Thanks for your help.

Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30068

MySql 4 and above have a "binary" type. It will store your bytes without conversion.
[ March 02, 2005: Message edited by: Jeanne Boyarsky ]

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
I agree. Here's the link:
subject: How to read and write binary Bytes in MySQL ?
Similar Threads
comparing digest always return false. why?
B&S: Beginner Questions
Understanding Byte Data and Character Encoding
Reading float, short from InputStream
Reading file and putting in an array