[OT] Upload 2 images with one jdbc INSERT?

2003-11-07 Thread john-paul delaney
Hello List... 

Apologies if this is off-topic, I posted to the jdbc forum on sun with little response.

I insert an image into a postgres db using the following code:

URL urlImage = getServletContext().getResource(
   /images/ + imageFilename);

URLConnection urlImageConn = urlImage.openConnection();
int urlImageLength = urlImageConn.getContentLength();
InputStream is = urlImage.openStream();

   ... and using a preparedStatement,
pStmt.setBinaryStream(1, is, urlImageLength);

However I'd like to insert a second image into the same db record but don't know if 
it's possible to use the same inputstream.

Thanks for any suggestions,
/j-p.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: [OT] Upload 2 images with one jdbc INSERT?

2003-11-07 Thread Christopher Schultz
j-p,
Apologies if this is off-topic, I posted to the jdbc forum on sun with little response.
I always try www.jguru.com -- I used to answer a lot of questions there. 
I should go back...

I insert an image into a postgres db using the following code:

URL urlImage = getServletContext().getResource(
   /images/ + imageFilename);
URLConnection urlImageConn = urlImage.openConnection();
int urlImageLength = urlImageConn.getContentLength();
InputStream is = urlImage.openStream();
   ... and using a preparedStatement,
pStmt.setBinaryStream(1, is, urlImageLength);
However I'd like to insert a second image into the same db record
 but don't know if it's possible to use the same inputstream.

Actually, I'm not entirely clear on when the stream is read. Either the 
PreparedStatement reads the stream immediately, or it reads the stream 
when you perform the execute(). I read the 1.4 JavaDoc and I'm still not 
convinced either way.

Just in case it doesn't read the stream until later, simply use another 
stream:

URL urlImage2 = getServletContext().getResource(second image);
URLConnection urlImageConn2 = urlImage2.openConnection();
int urlImageLength2 = urlImageConn.getContentLength();
InputStream is2 = urlImage2.openStream();
	pStmt.setBinaryStream(x, is2, urlImageLength2);

Don't forget to close your streams and URLConnections when you're done 
(i.e. in a finally block)!

-chris

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: [OT] Upload 2 images with one jdbc INSERT?

2003-11-07 Thread john-paul delaney
Thanks for the advice(s) Chris... 

I ignorantly hadn't realized I could open more than one stream, using only one stream 
returned a negativearray exception (?), while the second stream appears to have worked 
nicely.  

And yes, as you guessed - I hadn't closed the streams and URLConnections.  I'll 
remember in future.


regards
/j-p.



On Fri, 7 Nov 2003, Christopher Schultz wrote:

 I always try www.jguru.com -- I used to answer a lot of questions there. 
 I should go back...
 
 Actually, I'm not entirely clear on when the stream is read. Either the 
 PreparedStatement reads the stream immediately, or it reads the stream 
 when you perform the execute(). I read the 1.4 JavaDoc and I'm still not 
 convinced either way.
 
 Just in case it doesn't read the stream until later, simply use another 
 stream:
 
   URL urlImage2 = getServletContext().getResource(second image);
   URLConnection urlImageConn2 = urlImage2.openConnection();
   int urlImageLength2 = urlImageConn.getContentLength();
   InputStream is2 = urlImage2.openStream();
 
   pStmt.setBinaryStream(x, is2, urlImageLength2);
 
 Don't forget to close your streams and URLConnections when you're done 
 (i.e. in a finally block)!
 



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]