Hello Everybody,

I have stored images in ms access. The field name is "image" and the data type is OLE Object. I have stored an image with ".bmp" extension. In the database in the record it is showing "Bitmap Image". Now I am using JSP to retrieve this image and display it in the web page.

The coding is::

<%
InputStream sImage;
try {
String dataSourceName = "pictures";
String dbURL = "jdbc:odbc:" + dataSourceName;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    String sTable = "imagetable";
    Connection cn = null;
    Statement st = null;
    ResultSet rs = null;
String sSql = "SELECT machine_image,machine_name,machine_details FROM " + sTable;
cn = DriverManager.getConnection(dbURL,"","");
st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = st.executeQuery( sSql );
ResultSetMetaData rsmd = rs.getMetaData();
int n = rsmd.getColumnCount();
out.println( "<div align=\"center\">" + "<table border=2 cellspacing=2 bgcolor=LightGreen width=400><tr>" );
while(rs.next()) {
out.println( "</tr><tr>" );
byte[] bytearray = new byte[1048576];
int size=0;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/bmp");
while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
}
} //while
out.println( "</tr></table></div>" )

%>
<%

}//try

catch (Exception err) {
      out.println("ERROR" + err);
    }
%>

the output which i am getting is:
in the text box where the image is supposed to be displayed , string value is being displayed.
i am not able to get the image.

i have gone through net but not able to get any proper solution.

I will be grateful if anyone can help me. please help me as soon as possible.
thanks in advance.

are you storing the image itself or the URL to the image?/

hello parry,
I am using netbeans and my problem is that,

first from a combo i have to choose a value. the value is the purpose, say i choose for cutting.
then I have to click on a submit button then my request will go the next page and there all the image that are used for cutting purpose will be displayed.

the attributes of my data base is like this:

machine_code,machine_image,machine_purpose.

these three are the column header or attributes of my database. now if i have to choose any other purpose like cleaning then all the machines that are used for clening will be displayed. in the "machine_purpose" attribute all these purpose are stored.

i will be grateful if u can mail me the code for retrieving the image.
please help me. i need it urgently.
thanks in advance.

What you described consists of many steps:
1) submit form, send data to the second jsp page and retrieve the "purpose"
2) Call the method that executes the query with argument the "purpose" and returns the appropriate rows from the DB
3) Display the results.

Where are you having problems and what code you have so far?

What you described consists of many steps:
1) submit form, send data to the second jsp page and retrieve the "purpose"
2) Call the method that executes the query with argument the "purpose" and returns the appropriate rows from the DB
3) Display the results.

Where are you having problems and what code you have so far?

What are you trying to achieve by repeating other people's posts. Are you trying to get credit for their posts?
Be careful before someone reports you to one of the administrators for spamming.

Be careful before someone reports you to one of the administrators for spamming.

Too late :icon_twisted:

Hi singju, its not possible to provide code, but I will try to explain you the approach that I could think( use the tutorial at http://fdegrelle.over-blog.com/article-992927.html for reference)
1]
First create the java class that will read the image from the database using the primary key as given in the tutorial.

If there is no primary key for the table then create one e.g. id
The table structure will be like
id,machine_code,machine_image,machine_purpose
where 'id' is the primary key

2]
Than create the image.jsp file which will read the primary key as request parameter and pass it to the above java class

3]
Now after submitting the puspose, and querying the database you will get a result set (do not use machine_image in select query for performance improvement)

Now while iterating the resulset print out the image tag as

out.println("<img src=\"image.jsp?imgID="+rs.getString("id")+"\" width=\"115\" border=\"0\"");

Displaying images dynamically in jsp from database will always have a performance hit. Try to minimize the performance impact.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.