File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes how can we store image and get it back from SQL Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how can we store image and get it back from SQL Server" Watch "how can we store image and get it back from SQL Server" New topic
Author

how can we store image and get it back from SQL Server

vishal, patel
Greenhorn

Joined: Apr 24, 2001
Posts: 2
i am facing problem in storing and retrieve image to and from sql server so if you know the code than please paste it
thanx for taking time for reading this question
David Freels
Ranch Hand

Joined: Feb 01, 2001
Posts: 102
I do not have code available but I can walk you through to process.
To Insert an image into the db, read the file into a byte[].
Create a PreparedStatement with the appropriate SQL.
Call the preparedstatement setBinaryStream(int pos, InputStream is, int length)
Use the ByteArrayInputStream in place of the InputStream as this will take the byte[] in the constructor.
Execute the prepared statement.
To read the image back from the db, run an SQL query and obtain a resultset.
Call the resultset getBinaryStream(int pos) method to get an InputStream.
Use this stream to read the bytes back from the db and re assemble them in an image file.
This method expects to read to and from a file. The ByteArrayInputStream is trick to make the db think it came from a file.
David
Sun Certified Programmer for the Java2 Platform
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how can we store image and get it back from SQL Server
 
Similar Threads
store image using sql server
How to display image in JTable which is stored in database.
Retrieving an image from SQL Server database
Displaying an image from byte[] into JSP
this is my first question,how to display the image from SQL-SERVER with JSP?