package roseindia;

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

public class DisplayImage extends  HttpServlet{
    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String connectionURL = "jdbc:mysql://localhost:3306/test";
	java.sql.Connection con=null;
        Statement st1;
        try{			
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con=DriverManager.getConnection(connectionURL,"root","root");			
            st1=con.createStatement();
            ResultSet rs1 = st1.executeQuery("select image from pictures where id = '2'");
            String imgLen="";
            while(rs1.next()){
                imgLen = rs1.getString(1);
                System.out.println(imgLen.length());
                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);
                response.reset();
                response.setContentType("image/jpg");
                response.getOutputStream().write(rb,0,len);
                response.getOutputStream().flush();
                
            }
            st1.close();
            response.getOutputStream().close();
       } catch (Exception e){
            e.printStackTrace();
        }
    }
}