Results 1 to 6 of 6
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default Inserting byte[] into MySQL

    Hi all

    I've been working at this for hours and read all sorts of stuff of the web and I just can't seem to figure out how to do this.

    I have 2 blob fields in my MySQL table that I'm trying to insert the byte arrays into. When I try to insert it I get an SQL syntax Exception:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[B@35d9dc39, [B@72093dcd)' at line 1
    This is how I try to insert it:

    Java Code:
    String sel = "INSERT INTO config.user (name, password) VALUES (" + usrDigest + ", " + pswdDigest + ")";
    
    Statement stmt = conn.createStatement();
    stmt.execute(sel);
    Obviously there are some try statements etc. hanging around that I have removed for easy reading and this is just a simple version of what I plan to do as later I'm going to query the DB for the blobs and compare them to inputs but I don't get what I'm doing wrong here with this insert. It looks as though MySQL is having a problem with the contents of the blob. Would a prepared statement make a difference or am I just not handling this byte array properly?

    Just as a bit of background: the byte arrays are the result of SHA-1 digests. I'm trying to make a simple user authorisation system with one-way hashing.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    PreparedStatement see the tutorials.

  3. #3
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    PreparedStatement see the tutorials.
    Yes that is what I was thinking. If I use a PreparedStatement it won't try to pass the byte array as a String. Before you posted this I had already started working on it. Hopefully that does the trick.

    I will report back with my results. Thanks :D

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Aside from the fact that you never cobble together statements like that (at least values, table names, column names, etc, okay, but not values) it leads to too much trouble to make it worthwhile.

  5. #5
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Aside from the fact that you never cobble together statements like that (at least values, table names, column names, etc, okay, but not values) it leads to too much trouble to make it worthwhile.
    Yes I know I shouldn't be patching statements together. It is a habit I'm breaking myself of. I'm already having to go through code and undo it. :o

  6. #6
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    OK sorted it out. Using the PreparedStatement.setBytes(int, byte[]) method solved my problem.

    Thanks so much for the input masijade :)

    Now I'm off to change the rest of my statements to mirror this.

Similar Threads

  1. Replies: 3
    Last Post: 12-02-2010, 02:14 PM
  2. Read a bitmap byte by byte
    By cbr400aero in forum Java 2D
    Replies: 2
    Last Post: 10-18-2010, 11:42 PM
  3. Replies: 1
    Last Post: 04-21-2010, 02:28 AM
  4. Replies: 5
    Last Post: 04-05-2010, 01:24 PM
  5. Replies: 0
    Last Post: 12-24-2007, 09:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •