How to retrive multiple images from database
816985Dec 5 2010 — edited Dec 7 2010Hi,
I have to retrieve multiple images from oracle database and have to display the image in the jsp.
I am passing imageCategoryId from the jsp.This id contain number of image in DB.
I googled the issue and i found some solution.want to know whether what i did is correct and how i have to display the retrieved image in jsp.
Need urgent help.Experts kindly help me.
This is my action file.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.NamingException;
import com.image.dao.ImageDAO;
public class GetImageServlet extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
try{
ImageDAO image = new ImageDAO();
String categoryId = request.getParameter("catgId");
bytes[] imagebyte = image.getImagesByCategoryId(categoryId);
InputStream is = new FileInputStream(imagebyte);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(imagebyte);
blobIs.close();
}
catch(IOException e){
e.printStackTrace();
} catch(Exception excep) {
excep.printStackTrace();
}
}
}
This is DAO file
public byte[] getImagesByCategoryId(String categoryId) throws Exception
{
PreparedStatement statement = null;
ResultSet resultSet = null;
StringBuffer queryBuffer = new StringBuffer();
Blob image = null;
String imageId = null;
ArrayList imageIdLIst = new ArrayList();
try {
this.connection = connManager.getDataSourceConnection();
queryBuffer.append("select image from Image_Table where image_category='"+categoryId+"'");
statement = connection.prepareStatement(queryBuffer.toString());
resultSet = statement.executeQuery();
while(resultSet.next()) {
image = rs.getBlob(1);
long imgLength = image.getBufferSize();
byte[] byteImage = new byte[imgLength];
byteImage = image.getBytes(1,(int)imgLength);
}
} catch(Exception excep) {
excep.printStackTrace();
}
return byteImage;
}
How to display retrieved image in jsp.
whether action and dao file is correct or what changes i have to do to correct the problem.
P.S : As a Newbie i dont know how to use code tag for this java code.Kindly forgive me.
Regards,
hardlyUsed.