p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Java > Java and JDK > Servlets
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 29th, 2009, 12:01 PM
Authorized User
Points: 48, Level: 1
Points: 48, Level: 1 Points: 48, Level: 1 Points: 48, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Unable to display out images from SQL Query Browser:(

Hello, I am peggie. Currently, I am using servlet to display out images on my image.jsp page. However, nothing appears.

This is my servlet code:
Code:
 
package sg.nyp.edu.sit;
import java.sql.*;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class DisplayImage
 */
public class DisplayImage extends HttpServlet {
 private static final long serialVersionUID = 1L;
    /**
     * Default constructor. 
     */
    public DisplayImage() {
        // TODO Auto-generated constructor stub
    }
 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  String connectionURL = "jdbc:mysql://localhost/images";
  java.sql.Connection con= null;
  try{
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   con = DriverManager.getConnection(connectionURL,"root","");
   Statement st1=con.createStatement();
   ResultSet rs1 = st1.executeQuery("select image from images where imageID ='2'");
   String imglen="";
   if(rs1.next()){
    imglen = rs1.getString(1);
    System.out.println(imglen.length());
    
}
   
   rs1 = st1.executeQuery("select image from images where imageID = '2'");
   if (rs1.next()){
    int len = imglen.length();
    byte[] rb = new byte [len];
    InputStream readImg = rs1.getBinaryStream(1);
    int index=readImg.read(rb,0,len);
    System.out.println("index" + index);
    st1.close();
    response.reset();
    response.setContentType("image/jpg");
    response.getOutputStream().write(rb,0,len);
    response.getOutputStream().flush(); 
    
   }
   
  }
  
  catch (Exception e){
   e.printStackTrace();
   
   
   
  }
  
  
 }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }
}
This is my java getter and setter:
Code:
 
package sg.nyp.edu.sit.model;

public class Image {
int imageID;
Byte image;
String imglen;
public int getImageID() {
return imageID;
}
public void setImageID(int imageID) {
this.imageID = imageID;
}
public Byte getImage() {
return image;
}
public void setImage(Byte image) {
this.image = image;
}
public String getImglen() {
return imglen;
}
public void setImglen(String imgLen) {
this.imglen = imgLen;
}















}
Code:



This is my image.jsp

Code:
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="form1" method="get" action="./DisplayImage">


</form

</body>
Code:
</html>


My database is called: images
and the attributes are:
int imageID;
Blob image;

May I know what's wrong with it.
Thanks:D
Hope to receive ur reply soon!:D
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display sql server images in asp.net c# bobwith5 ASP.NET 2.0 Basics 0 October 20th, 2007 12:07 PM
Display text data in textarea from SQL query palvin Classic ASP Professional 13 November 30th, 2005 05:52 PM
Cannot display/return SQL Query Output from a Vari sjm SQL Server DTS 1 September 9th, 2005 12:40 PM
inserting images in ms sql server using query anal connect2sandep SQL Server 2000 2 July 24th, 2005 05:41 PM
Unable to create SQL Query using parameters odyzeus Crystal Reports 0 March 3rd, 2005 01:29 PM



All times are GMT -4. The time now is 12:27 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc