Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum Rss Feed

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
Free Newsletters:

News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted May 2, 2000

Storing and Retrieving Binary Data in SQL Server

By Les Smith

Sometimes it is necessary to save binary data  such as zip files, pictures or office documents into the database. Even if these files are extracted from the database it is sometimes better to store the document rather than recreate it through another query to the database. Also there may be a need to archive an exact replica of a document that a customer has received.

In this example we first compress all the .html and .csv files that have been delievered to a customer. A zip file of all the reporting documents is thus created. Then we insert the zip file into the SQL Server database along with the customer number and week number. It is then saved and we are free to overwrite any of the existing reports.

The field that holds the zip file has an image datatype. One might think that the image datatype is just for graphic files such .jpeg, .gif  or .bmp and the datatype to use would be varbinary or binary. The naming of the datatypes is misleading. In SQL Server 6.5 the binary and varbinary can only hold 255 bytes. In SQL Server 7.0 the binary and varbinary have a maximum storage of 8000 bytes. So image is the datatype to use for file storage.

I chose to use Java for this task because it has excellent file IO and database interfaces. It has a subclass specifically designed for file compression(to create zip files). The Java database API, JDBC has specific methods for writing and reading any kind of data to database.

Please note that this example uses a WebLogic JDBC driver. If you are using a different driver you will need to change the way the connection to the database is initiated.

The main tasks are to:
1) create a stream to hold the file for writing to the database (FileInputStream) and use the setBinaryStream() to write to the database.
2) to fill a byte array with bytes from the database using the resultset's getBytes() method and to write to file system using a  FileOutputStream.

Here are the most important methods of the FileDBTransfer class:

void insertZipFile(FileDBTransfer fdb, Connection cn, int CompID, int WeekNum, String FileName)throws IOException, FileNotFoundException, SQLException
int FileLength;
FileInputStream fis;
String Query;
PreparedStatement pstmt;
 //prepare FileInputStream
 fis = fdb.getFileInputStream(FileName);
 FileLength= fdb.getFileLength(FileName);
 //Build the insert statement
 //Create a parameterized query
 Query = ("insert ReportArchive VALUES(?,?,?)");
 pstmt = cn.prepareStatement(Query);
 pstmt.setInt(1, CompID);
 pstmt.setInt(2, WeekNum);
 pstmt.setBinaryStream(3, fis, FileLength); //method to insert a stream of bytes

 //execute the insert statement

}// end insertZipFile

Reading from the database is even simpler:

void getBinaryFile(String FileName,ResultSet rs) throws IOException, FileNotFoundException, SQLException
 //Array to hold array of file bytes
 byte[] fileBytes; //an array of bytes

 if (rs.next()) //if there is a record
  fileBytes = rs.getBytes(1); //create a byte array from contents of field
  OutputStream targetFile=  new FileOutputStream(FileName); // define the output stream
  targetFile.write(fileBytes); //write the array of bytes to file
  targetFile.close(); //close the File stream


The files used are:
1) FileDBTransfer.java   This is the class that contains methods for reading and writing files to the database. It cannot be executed directly, but rather must be instantiated and used in other classes.

2) saveToDB.java    Instantiates the FileDBTransfer class and executes methods to upload a file to database
Structure: java saveToDB [CompanyID] [Week] [FileToUpload]
Call:java saveToDB 10 32 "out.zip"

3) saveToFile.java    Instantiates the FileDBTransfer class and executes methods to download a file from database
Structure: java saveToDB [CompanyID] [Week] [TargetFile]
Call:java saveToFile 10 32 "out2.zip"

4)archiveReports.java  Class to zip up files. This class has a main method inside it and thus instantiates itself, but it may also be instantiated by other classes,
Call: java archiveReports d:\mydir

This is a class used by archiveReports.java program to filter a listing of files from a directory.

6) ReportArchiveTable.sql
An example table that contains an image datatype and is used in this article.

Documentation files generated by javadoc

MS SQL Archives

Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM