This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes storing images in database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "storing images in database" Watch "storing images in database" New topic
Author

storing images in database

sagar saravanan
Greenhorn

Joined: Dec 18, 2000
Posts: 5
hai friends,
kindly tell me how to store images on database for eg in MS SQL server
Santak Das
Greenhorn

Joined: Jun 29, 2001
Posts: 1
Linking to Images
The best way for SQL Server to deal with images is for SQL Server to store a pointer to an image. This can be a file name, directory/file combination or URL. The actual image can be stored on the file system or on the web server. It's very easy to write ASP code to build an IMG tag. The tag is customized with the file name pulled from the database. I'll walk through an example of this using ASP. For example, I have code to select a banner image and display it. My table looks something like this:
CREATE TABLE [BANNER_Ads] (
[AdId] [int] IDENTITY (1, 1) NOT NULL ,
[AdName] [char] (100) NOT NULL ,
[AdType] [char] (10) NOT NULL ,
[ImageURL] [varchar] (100) NULL ,
[Height] [smallint] NULL ,
[Width] [smallint] NULL ,
[ALTText] [varchar] (100) NULL ,
[HTML] [varchar] (2000) NULL ,
[LinkToURL] [varchar] (100) NULL ,
) ON [PRIMARY]
GO
The SELECT statement is also pretty basic:
SELECT *
FROM BANNER_Ads
WHERE AdId = 17
And here is the ASP script to generate the IMG tag. Response.Write ""
Response.Write "<IMG SRC=" & trim(bobjRS("ImageURL")) & " BORDER=0 "<br /> Response.Write " WIDTH=" & trim(bobjRS("Width"))<br /> Response.Write " HEIGHT=" & trim(bobjRS("Height")) <br /> Response.Write " ALT=" & Chr(34) & trim(bobjRS("ALTText")) & Chr(34)<br /> Response.Write ">
"

I actually wasn't very smart when I wrote this. My image URL is a fully qualified URL with the domain name, path and file. The most flexable approach would be to split those fields out. That way if code ever moves to a new domain it's easy to update. That's a minor problem though. Visual Basic would handle this just a little differently. After creating an image/picture object on the form you code set it's source to the location and filename of your image. I think Visual Basic requires path names as opposed to URL's for it's image locations. My VB days are far in the past. If someone has a few lines of sample code I'd be happy to post it here.
SELECTing Images
Now we'll retrieve images from SQL Server using ASP. In case you've forgotten (or I didn't mention it enough above) this is really a pain. You are going to need two separate ASP Pages. This code will pulled from a Microsoft Knowledge Base article. The first I called Picture.asp and it looks like this:
<HTML>
<HEAD><TITLE>Display Image</TITLE></HEAD>
<BODY>
This page will display the image New Moon Books from a SQL Server 6.5
image field.


</BODY>
</HTML>
This calls the ASP page PicShowImage.asp which looks like this:
<%@ LANGUAGE="VBSCRIPT" %>
<%<br /> ' Clear out the existing HTTP header information<br /> Response.Expires = 0<br /> Response.Buffer = TRUE<br /> Response.Clear<br /> ' Change the HTTP header to reflect that an image is being passed.<br /> Response.ContentType = "image/gif"<br /> Set cn = Server.CreateObject("ADODB.Connection")<br /> ' The following open line assumes you have set up a System DataSource<br /> ' by the name of myDSN.<br /> cn.Open "Provider=SQLOLEDB; Data Source=server9; Initial Catalog=pubs; User ID=sa; Password="<br /> Set rs = cn.Execute("SELECT logo FROM pub_info WHERE pub_id='0736'")<br /> Response.BinaryWrite rs("logo")<br /> Response.End<br /> %>

------------------
Santak


Santak
 
wood burning stoves
 
subject: storing images in database
 
Similar Threads
Help to insert/retreive image from database
Keeping an image in database
Direct way to store images
How can i display image .bmp in a JSP !!! Thanks !!!
get image from database