laker02
laker02

Reputation: 117

Java File type into SQL Insert clause

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?

Upvotes: 0

Views: 114

Answers (1)

Barett
Barett

Reputation: 5948

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.

Upvotes: 1

Related Questions