0

I ran into an awkward problem.

I need to insert some values into an SQL table.

For that i use an Insert clause, like this:

INSERT INTO `USERS`(`ID`, `FILE`, `PROPERTIES`) VALUES ('1', FILE, '{"attributes":{"LANGUAGE":"ENG","AUTHOR":"John"}}')

My problem is the File value. I don't know how i can insert a File object in these circumstances. And since i cannot pass the value NULL, due to program restrictions, how can i do it?

laker02
  • 107
  • 1
  • 1
  • 6
  • possible duplicate of [How to store a file in a mysql database using blob](http://stackoverflow.com/questions/9476434/how-to-store-a-file-in-a-mysql-database-using-blob) – Barett May 25 '15 at 23:51
  • @Barett i don't see how this is a duplicate! It's a completely different question... just about the same topic – laker02 May 25 '15 at 23:52
  • I disagree (the answer given there is exactly the answer to your question), but I will help a little more if that isn't enough information for you. What persistence library are you using: JPA, JDBC, other? – Barett May 25 '15 at 23:57
  • I'm using a `dbinit.sql` file to do the inserts before running the application. i'm using a JDBC connection pool, but that doesn't matter here. i just want to know how i can insert a File object into a SQL table @Barett – laker02 May 26 '15 at 00:00
  • How you're executing your query does matter. See answer below. – Barett May 26 '15 at 00:02

1 Answers1

1

Most persistence libraries won't let you do this directly. (none that I'm aware of)

  1. convert the file into a byte array using IOUtils.toByteArray(InputStream input) or an equivalent

  2. (if using JDBC) call PreparedStatement.setBytes(byte[] b) to pass this value to the database

Alternative solution: put the actual file in some known disk location and store only the filename/path in the database.

Barett
  • 5,826
  • 6
  • 51
  • 55