Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 2nd, 2004, 07:40 AM
Registered User
 
Join Date: Mar 2004
Location: Singapore, Singapore, Singapore.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ban_gal
Default Display picture from database using JSP codes

I have no idea how to start displaying a picture in a JSP page from the database.
I started off with these codes..

My Servlet
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;

public class ImageServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            String filename = request.getParameter("filename");
            filename = "baby.gif";
            //execute you sql to get the clob here
            String url = "jdbc:mysql://localhost/test";
            String userID="root";
            String PWD="";
            Connection c = null;
            String classPath= "com.mysql.jdbc.Driver";

            try{
                Class.forName(classPath).newInstance();
                conn = DriverManager.getConnection(url,userID,PWD);
            }catch(Exception exc){
                out.println(exc.toString());
            }
            Statement s = null;
            ResultSet rst = null;

            // Connection c = ...
            Statement s = c.createStatement();
            String strSQL = "SELECT binaryfile FROM uploads WHERE filename='" +filename +"'";
            rst = stm.executeQuery(strSQL);
            byte [] barray = rst.getBytes("binaryfile");
            rst.close();
            s.close();
            response.setContentType("image/gif");
            response.getOutputStream().write(barray);

        } catch (Exception sqle) {

        }

    }

}



in my JSp page, i used this
<embed src="http://localhost:8080/servlet/ImageServlet" content="image/gif">

However, no picture was displayed...
What went wrong? Please advise me. Thanks to all.

  #2 (permalink)  
Old March 3rd, 2004, 03:11 AM
Authorized User
 
Join Date: Feb 2004
Location: Jilin, Jilin, China.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to cash
Default

There is mistakes in :
/********************* mistake **********************/
Connection c = null;

conn = DriverManager.getConnection(url,userID,PWD);//conn mistake

Statement s = c.createStatement();

rst = stm.executeQuery(strSQL);//stm mistake
/********************* mistake **********************/

would be:
Connection c = null;
c = DriverManager.getConnection(url,userID,PWD);
Statement s = c.createStatement();
rst = s.executeQuery(strSQL);
  #3 (permalink)  
Old March 3rd, 2004, 11:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cash has already pointed out that your source contains several typos so I'm won't comment on them. Have a look at this URL, which has examples of what you are trying to do: http://jguru.com/faq/view.jsp?EID=159

Cheers

Martyn
  #4 (permalink)  
Old November 15th, 2007, 03:26 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, i am facing problems so hope you guys can help me out! I did try out many different solutions. What i want is to retrieve the BLOB image in mysql and display it in jsp.

I tried out the code. But there is no image displayed out. I have no idea what went wrong.
I embed the servlet in one of the tags so i am not sure if i am right. If not, please guide me. Thanks.

---comments.jsp---

table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="428" height="63">
<tbody>
<td bgColor="#008080" width="28" align="center" height="19"><b>No.</b></td>
<td bgColor="#008080" width="126" align="center" height="19"><b>Name</b></td>
<td bgColor="#008080" width="224" align="center" height="19"><b>Picture</b></td>
<td bgColor="#008080" width="270" align="center" height="19"><b>Comments</b></td>



<%

 String DRIVER = "com.mysql.jdbc.Driver";
 Class.forName(DRIVER).newInstance();

 Connection conn = null;
 Statement stmt = null;


 try{

    String dbUrl = "jdbc:mysql://localhost/homepage?user=root&password=mysql";

  int i=1;
  conn = DriverManager.getConnection(dbUrl);
  stmt = conn.createStatement();
  String query = "Select * FROM Comments";
  ResultSet rst = stmt.executeQuery(query);
  while(rst.next()){

  if(i==(i/2)*2){
  %>

  <tr>
  <td width="28" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=i%></td>
  <td width="126" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=rst.getString(3)%></td>
 <td width="224" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"> <embed src="http://localhost:8080/servlet/ImageServlet" content="image/jpeg"></embed></td></td>
  <td width="270" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=rst.getString(4)%></td>
  </tr>

  <%
  }else{
  %>
  <tr>
  <td width="28" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=i%></td>
  <td width="126" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=rst.getString(3)%></td>
   <td width="224" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><embed src="http://localhost:8080/servlet/ImageServlet" content="image/jpeg"></embed> </td>
  <td width="270" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=rst.getString(4)%></td>

  </tr>
  <% }

  i++;
  }
  rst.close();
  stmt.close();
  conn.close();
  }catch(Exception e){
  System.out.println(e.getMessage());
  }
  %>
  </tbody>
  </table>


<---ImageServlet.java-->

import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;

public class ImageServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            String filename = request.getParameter("name");
            //filename = "baby.gif";
            //execute you sql to get the clob here
            String url = "jdbc:mysql://localhost/homepage";
            String userID="root";
            String PWD="mysql";
            Connection conn = null;
            String classPath= "com.mysql.jdbc.Driver";

            try{
                Class.forName(classPath).newInstance();
                conn = DriverManager.getConnection(url,userID,PWD);
            }catch(Exception exc){
                out.println(exc.toString());
            }
            Statement stmt = null;
            ResultSet rst = null;

            // Connection c = ...
              stmt = conn.createStatement();
            //String strSQL = "SELECT Picture FROM comments WHERE name='" +filename +"'";
            String strSQL = "SELECT Picture1 FROM comments WHERE name='" +filename+"'";
            rst = stmt.executeQuery(strSQL);

            byte [] barray = rst.getBytes("binaryfile");
            rst.close();
            stmt.close();
            response.setContentType("image/jpeg");
            response.getOutputStream().write(barray);

        } catch (Exception sqle) {

        }

    }

}
  #5 (permalink)  
Old November 16th, 2007, 09:44 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can just use simple <img> tag and src of this tag should be the servlet you've written. And set the content type in the servlet itself. That would be very simple.

- Rakesh
  #6 (permalink)  
Old November 16th, 2007, 09:46 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try also by calling the flush() method on the output stream.


- Rakesh
  #7 (permalink)  
Old May 30th, 2008, 08:21 PM
softomania
Guest
 
Posts: n/a
Default

Hi
this forum has been of much help
I was facing a similar problem
Regards and thanx to all



---------------------------

Also visit my site for
Software testing software testing tutorial software testing interview questions
________________________________________
Softwareqatestings.com TelecomFYI.com


 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Professional JSP(1st edition) source Codes, Help ! meisam Pro JSP 2 November 19th, 2007 08:06 AM
Picture display Venkatesan General .NET 2 January 31st, 2007 08:27 AM
Inserting picture into Sqlserver using JSp Thushara Jacob Pulickal Pro JSP 0 May 4th, 2006 12:42 AM
How to display a picture stored in SqlServer yiyufeiyang BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 March 27th, 2006 10:14 PM
Why .asp source codes are display on screen? Andraw Classic ASP Basics 0 January 24th, 2005 11:06 PM



All times are GMT -4. The time now is 11:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.