Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Saving binary data from SQL Server column

Reply
Thread Tools

Saving binary data from SQL Server column

 
 
oswaldt@ameritech.net
Guest
Posts: n/a
 
      07-12-2006
Hello,

Please let me know if this is not the proper group to post to, and if
this question has been answered in the past please forgive me, I
couldn't seem to find it.

I have a process that puts a file into a image column in SQL Server.
This runs very well and fast on a 2 megabyte file. Here is the code I
use.

CallableStatement call = mConnection.getCallableStatement("{call
spInsertWebFile (?,?,?,?,?,?,?,?,?,?)}");
call.setInt(index++, this.SiteNumber);
call.setInt(index++, this.SiteRevision);
call.setString(index++, sf.getDestinationFile());
call.setDate(index++, new java.sql.Date(f.lastModified()));
call.setLong(index++, f.length());
call.setString(index++, this.BatchRevisedBy);
call.setString(index++, mil.dla.dlis.wds.DateFormat.format(now));
call.setInt(index++, 0);
call.setBinaryStream(index++, (InputStream)new FileInputStream(f),
(int)f.length());

call.registerOutParameter(index, java.sql.Types.INTEGER);

call.execute();

I will say it uploads the file in about 10 seconds. Now when I
download I can't seem to find \such an efficient manner as this. I
have to use a loop to read some bytes from the OutputStream and then
write them to the FileInputStream. This method takes about 40 seconds.

byte[] input = new byte[blockSize];
int count=0;
int ret=0;
java.io.InputStream in = rs.getBinaryStream("file_data");
int size = rs.getInt("File_size");


String fullpath = path;
int off=0;

if (! System.getProperty("os.name").equalsIgnoreCase("wi ndows"))
fullpath = fullpath.replace('\\','/');

java.io.File f = new java.io.File(fullpath);
if (f.exists()) f.delete();

java.io.FileOutputStream out = new
java.io.FileOutputStream(fullpath);

while (size > 0)
{
count++;
if (size < blockSize)
input = new byte[size];
ret = in.read(input);
out.write(input);
size -= ret;
}
out.close();

Is there a more efficient way? I have tied just reading the whole file
into a byte array but as you can imagine that uses a lot of memory. I
have also tried different block sizes like 128k, 256k and the default
of 64k.

thanks,
Tyson

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fetching & Inserting Data into a column of TEXT Data type in SQL server 2000 Using ASP.NET Bhavesh ASP .Net 5 07-18-2007 07:39 AM
Fetching & Inserting Data into a column of TEXT Data type in SQL server 2000 Using ASP.NET Bhavesh ASP .Net 1 07-17-2007 09:05 AM
Inserting & Fetching Data into a column of TEXT Data type in SQL server 2000 Using ASP.NET Bhavesh ASP .Net 0 07-16-2007 11:15 AM
EXCEL question saving a file saving the the first column as read only Luis Esteban Valencia ASP .Net 0 01-06-2005 07:02 PM
Deleting binary data (image column) from sql server with ADO.NET Ryan Taylor ASP .Net 2 10-22-2004 12:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57