How to save binary file in sql database?

I have a varbinary column that we use to store excel files. I need to update this column with the contents of another xls file that is currently on my file system.

Given java.sql.Connection, how do I update a string?

We are using SQL Server 2005.

+6
java sql
source share
4 answers

I ended up doing the following:

PreparedStatement st = conn.prepareStatement("update MyTable set binaryData = ? where id= 9"); st.setBinaryStream(1, new FileInputStream(file), (int)file.length()); st.execute(); 
+5
source share

Using java.util.Connection and the correct SQL, you can create the corresponding java.sql.PreparedStatement (I do not use SQL Server, so you better write SQL yourself).

You can create java.sql.Blob using byte data read from your xls file.

Call .setBlob (Blob) on PreparedStatement and then execute it.

I have not written code for you, but this should be fundamental.

+2
source share

Go to the byte array in the BLOB field.

0
source share

In older versions of Java you can try

 obj.setObject("@somefield", some_data); 
-2
source share

All Articles