Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

loading image from a database

Posted on 2003-03-04
4
Medium Priority
?
251 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
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Ready to kick start your career in 2018? Add app developer skills to your resume. January’s Course of the Month features Android App Development training with hands-on learning.  Read on to learn why these skills are important.
As a matter of fact, Outlook OST files are of much importance in relation to Exchange mailbox. OST files are independent as they are simply copy of data of a user’s mailbox on Exchange Server. Though, if the server’s status is changed or it is dama…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

564 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