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?

5
  • possible duplicate of How to store a file in a mysql database using blob Commented May 25, 2015 at 23:51
  • @Barett i don't see how this is a duplicate! It's a completely different question... just about the same topic Commented May 25, 2015 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? Commented May 25, 2015 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 Commented May 26, 2015 at 0:00
  • How you're executing your query does matter. See answer below. Commented May 26, 2015 at 0:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.