?
Solved

loading image from a database

Posted on 2003-03-04
4
Medium Priority
?
248 Views
Last Modified: 2010-04-01
I have pictures in bmp format stored in MS SQL Server database as an image type field.

How can I display these images in a JSP file?

I know the solution to store the path of the file instead the actual file into the database, but I don't want to do this.
0
Comment
Question by:oto27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 3

Expert Comment

by:allahabad
ID: 8069258
You can display image in JSP. Content type for JSP is "text/html". To display binary data, you can use Servlet.
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8069262
Oops,
You can not display image in JSP. Content type for JSP is "text/html". To display binary data, you can use Servlet.
0
 
LVL 3

Accepted Solution

by:
allahabad earned 400 total points
ID: 8069276
You can try this code. You will have to modify it for 'SQL Server', and 'select' statement .

import java.util.*;
 import java.io.*;
 import javax.servlet.http.*;
 import java.text.DateFormat;
 import javax.servlet.*;
 import java.sql.*;

 public class DisplayImageServlet extends HttpServlet {

   public void doGet(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException{

         res.setContentType("image/gif");
         try {


               String url = "jdbc:oracle:thin:scott/tiger";
               String jdbcDriver = "oracle.jdbc.driver.OracleDriver";


               Class.forName(jdbcDriver);
               Connection con = java.sql.DriverManager.getConnection(url);
               Statement stmt = con.createStatement();
               String query = "SELECT image FROM file_test";
               ResultSet rs1 = stmt.executeQuery(query);
               if (rs1.next()){
                  ServletOutputStream o= res.getOutputStream();
                  InputStream in=rs1.getBinaryStream(1);
                  //response.setContentLength(3187);


                  byte[] bt = new byte [1000];
                  for(int i = in.read(bt); i != -1; )
                  {
                     o.write(bt);
                     in.read(bt);
                  }
                  o.flush();
                  o.close();

              }
              con.close();


         }
         catch (Exception e){
              e.printStackTrace();
}

   }
 }

Hope this helps.
0
 

Expert Comment

by:nitinms
ID: 8551016
I have a question continued with this. I have a text box and Submit button. I get a dynamic image when i make a choice and click the button. The image is is Binary and when the image gets displayed I lose the textbox and the Submit button for the new request.
So please help me out so that, I have the Text box and the button and then the image beneth

Thanks
NT
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question