-2

I have a java application with apache-poi 3.5.

My database is Microsoft Sql Server.

In my application i do an upload and i want to know if it's possible to store my file (*.xls, *.csv, *.xlsx) directly in my database ?

Mercer
  • 9,736
  • 30
  • 105
  • 170
  • Are you trying to store the file itself (i.e. as a BLOB), or the data in the file (i.e. importing the spreadsheet into a SQL table?). Either way, yes, it's possible. – RB. Apr 25 '16 at 13:42

1 Answers1

1

You could consider converting the files to their binary equivalent (a byte[]) and using a VARBINARY(MAX) field to store the contents as well as the file name so that you could properly reconstruct the file) :

// Retrieve the byte[] data for your file
byte[] data = Files.readAllBytes(new File("/path/to/file").toPath());
// Write this data to a VARBINARY(MAX) field in SQL here

There are a variety of approaches to handle this, but this would be the most naive approach.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • ok, so how can i do to convert my file to their binaey .? – Mercer Apr 25 '16 at 13:44
  • There are quite a few ways to handle this as well. [This related discussion](http://stackoverflow.com/q/858980/557445) addresses a variety of them, so it's just a matter of preference. Basically, you just want to pass in your file name or an `InputStream` that is pointed to your file and retrieve the corresponding data of the file a `byte[]`, so you can store that in your database. – Rion Williams Apr 25 '16 at 13:46
  • i use java 1.5 there is no `Files` method – Mercer Apr 25 '16 at 14:13
  • Ah. Any of the other solutions [in the thread I mentioned earlier](http://stackoverflow.com/q/858980/557445) should be sufficient. They generally require some includes (as the `Files` option was introduced in JDK 7 apparently). – Rion Williams Apr 25 '16 at 14:16
  • yes all right, so how can do the reverse, get `VARBINARY` and recreate my file ? – Mercer Apr 25 '16 at 14:20