Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display multiple blob images from database on basis of email id but i can only get one image and other image are associated with same email id is not displaying not displaying i need your help to display multiple images from
database .

What I have tried:

    package controller;
import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import Databse.AppDao;
import com.mysql.jdbc.ResultSet;

/**
 *    Servlet implementation class Image
 */
@WebServlet("/Image")
public class Image extends HttpServlet {
 private static final long serialVersionUID = 1 L;

 /**
  * @see HttpServlet#HttpServlet()
  */
 public Image() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 response)
 */
 protected void doGet(HttpServletRequest request,
  HttpServletResponseresponse) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setContentType("image/jpg");
  OutputStream img;
  java.sql.Blob image = null;
  byte[] imgData = null;
  HttpSession session = request.getSession();
  // String email =(String)session.getAttribute("email");
  String email = "tanumanu19971@gmail.com";

  String sql = "select photo from photos  where email=?";
  AppDao obj = new AppDao();
  java.sql.ResultSet rs = AppDao.getimage(sql, email);

  OutputStream o;

  try {
   while (rs.next()) {
    String imgLen = "";
    imgLen = rs.getString(1);
    System.out.println(imgLen.length());
    int len = imgLen.length();
    byte[] rb = new byte[len];
    InputStream readImg = rs.getBinaryStream(1);
    int index = readImg.read(rb, 0, len);
    System.out.println("index" + index);


    response.setContentType("image/jpg");
    response.getOutputStream().write(rb, 0, len);



   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   while (rs.next()) {
    String imgLen1 = "";
    imgLen1 = rs.getString(1);
    System.out.println(imgLen1.length());
    int len1 = imgLen1.length();
    byte[] rb = new byte[len1];
    InputStream readImg = rs.getBinaryStream(1);
    int index = readImg.read(rb, 0, len1);
    System.out.println("index" + index);
    response.setContentType("image/jpg");
    response.getOutputStream().write(rb, 0, len1);
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }


 AppDao class

 package Databse;
 import java.io.InputStream;
 import java.sql.Blob;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import javax.servlet.ServletInputStream;
 import com.oreilly.servlet.multipart.Part;
 public class AppDao {
  public static String username = "root";
  public static String password = "root";
  public static String url = "jdbc:mysql://localhost:3306/wedding";
  public static String driver = "com.mysql.jdbc.Driver";
  public static Connection con = null;
  public static PreparedStatement ps = null;
  public static ResultSet rs = null;
  public static ResultSet rs1 = null;
  public static ResultSet ds = null;
  static {

   try {
    Class.forName(driver);
    con = DriverManager.getConnection(url, username, password);
    System.out.println("Connection successfull");
   } catch (Exception e) {
    e.printStackTrace();
   }


  }

  public static ResultSet getimage(String sql, String email) {
   try {
    ps = con.prepareStatement(sql);
    ps.setString(1, email);
    rs = ps.executeQuery();

   } catch (Exception e) {

   }

   return rs;
  }


  Database wedding

  Create DATABASE wedding;

  use wedding;

  create table `photos` (
   `id`
   int(10),
   `photo`
   blob,
   `security`
   varchar(105),
   `email`
   varchar(120)
  );

  insert into `photos` (`id`, `photo`, `security`, `email`)
  values('1', 'ÿØÿà', 'tanumanu19971123', 'tanumanu19971@gmail.com');
  insert into `photos` (`id`, `photo`, `security`, `email`)
  values('2', 'ÿØÿà', 'tanumanu19971123', 'tanumanu19971@gmail.com');
  insert into `photos` (`id`, `photo`, `security`, `email`)
  values('3', 'ÿØÿà', 'tanumanu19971123', 'tanumanu19971@gmail.com');




  Explanation

  I want to get multiple blob images from MySQl database from email id but
  after running this code only single image display but two other images which
  are associate with tanumanu19971 @gmail.com are not displaying i have used
  while loop and repeat the image content twice but could not get any result
   .I know that this question is asked before but trust me i have tried all
  post   for me i am dealing with this
  situation from many days so please do not ignore my post or put it in hold
  and allow your community to help me out any modification in this code will be
  really helpful so that i can able to get multiple blob images from
  database
          based on email id
Posted
Updated 28-May-20 19:46pm
v2

1 solution

i want to upload and display blob img and text data from data base in jsp using in netbens 8 but showing some error in code .the code is below



<%--
Document : newjsp
Created on : May 28, 2020, 10:18:20 PM
Author : lenovo
--%>


<%@page import="java.io.OutputStream"%>
<%@page import="java.sql.Blob"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.sql.ResultSet"%>


<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.lang.String"%>


<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>



<title>JSP Page


<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/upload_img", "root", "root");

//Connection con = FactoryConnection_SQL_SERVER.getConnection("empCHART");
Statement stSuper = con.createStatement();
Statement stSetor = con.createStatement();

Blob image = null;
byte[] imgData = null;

ResultSet rsSuper = stSuper.executeQuery("SELECT * FROM image");

if (rsSuper.next()) {
image = rsSuper.getBlob(12);
imgData = image.getBytes(1, (int) image.length());
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData); // even here we got the same as below.
//o.flush();
//o.close();
//--[...]





< img title="<%=rSuper.getString("1").trim()%> " src="<% = o.wite(imgData);// o.flush(); o.close(); %>">







correct the error

<%=rsSuper.getString("1")%>


<%=rsSuper.getString("Position")%>


Id:
<%=rsSuper.getString("id")%>


Phone:
<%=rsSuper.getString("Phone")%>


E-Mail:
<%=rsSuper.getString("Email")%>


 
Share this answer
 
Comments
phil.o 29-May-20 9:33am    
This is not an answer. If you have got a question, please post a new question instead. But, please, format your code properly with <pre> tags, and provide an acurate description of the error. "but showing some error in code" does not tell us anything useful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900