0

I have one pojo class with setter and getter method and one servlet class in which i am getting values from jsp page and then calling a function of dataaccess class which is EmployeeBean and passing parameter so problem is coming its not getting inserted into databse because blob and datatype probelm please anyone help me here...

public class Employee 
{

private int empId;
private String iname;
private String photo;
private String contentType;
private int contentLength;
private InputStream inputstream;

public InputStream getInputstream() {
    return inputstream;
}
public void setInputstream(InputStream inputstream) {
    this.inputstream = inputstream;
}
 public String getContentType() {
    return contentType;
}
public void setContentType(String contentType) {
    this.contentType = contentType;
}


public int getContentLength() {
    return contentLength;
}
public void setContentLength(int contentLength) {
    this.contentLength = contentLength;
}
public String getIname() {
    return iname;
}
public void setIname(String iname) {
    this.iname = iname;
}
 }


 @MultipartConfig(maxFileSize = 18177215)   
 @WebServlet(name = "FileUploadServlet", urlPatterns = {"/FileUploadServlet"})
public class FileUploadServlet extends HttpServlet
  {

 protected void processRequest(HttpServletRequest request, 
    HttpServletResponse response)
        throws ServletException, IOException {

HttpSession session = request.getSession(true); 
InputStream inputStream = null;

Employee emp1 = (Employee)session.getAttribute("emp1");
int n = emp1.getEmpId();



emp1.setEmpId(emp1.getEmpId());
emp1.setIname(request.getParameter("iname"));
 Part filePart = request.getPart("photo");
if (filePart != null) {
    emp1.setContentType(filePart.getContentType());
    emp1.setContentLength((int) filePart.getSize());
    inputStream = filePart.getInputStream();
    emp1.setInputstream(inputStream);
    EmployeeBean eb = new EmployeeBean();
        emp1 = eb.addImage(emp1);
       session.setAttribute("emp1",emp1);
        response.sendRedirect("EmployeeDetail.jsp");
   }
 }

}

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      processRequest(request, response);
  }

     @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      processRequest(request, response);
  }

   @Override
  public String getServletInfo() {
      return "Short description";
  }
}



  public Employee addImage(Employee emp1) {
  Connection con = null;
  Statement stmt = null;
  PreparedStatement pstmt = null;
  ResultSet rs = null;
 try {
      con = ConnectionManager.getConnection();
      stmt = con.createStatement();
      pstmt = con.prepareStatement("INSERT INTO upload_documents(Name, contentType,     contentLength, empId, photo) values(?,?,?,?,?)");

      pstmt.setString(1, emp1.getIname());
      pstmt.setString(2, emp1.getContentType());
      pstmt.setInt(3, emp1.getContentLength());
      pstmt.setInt(4, emp1.getEmpId());
    pstmt.setBlob(5,emp1.getInputstream());
      pstmt.execute();
        } catch (SQLException  ex) {

  } finally {
      try {
          if (stmt != null) {
              stmt.close();
          }
          if (con != null) {
              con.close();
          }
      } catch (SQLException ex) {
          Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  return emp1;
 }
7
  • please anyone help me... i am new to this things.. Commented Aug 18, 2014 at 10:01
  • please anyone tell me .. why no body is replying here anything..no comments... no suggestion..? Commented Aug 18, 2014 at 11:23
  • hey why are you storing inputstream reference in database? Instead you can store the bytes which represents the contents of the file. Please change this and let us know if you are still facing issues Commented Aug 18, 2014 at 11:29
  • i didnt get proper will u tell me which part i have to change in servlet or in method.. Commented Aug 18, 2014 at 12:14
  • sir will u explain it please because i am facing this blob binary byte only this problem so please explain me.. Commented Aug 18, 2014 at 12:33

1 Answer 1

1
public class Employee 
{
....
private byte[] fileBytes;
.....
}

@MultipartConfig(maxFileSize = 18177215)   
 @WebServlet(name = "FileUploadServlet", urlPatterns = {"/FileUploadServlet"})
public class FileUploadServlet extends HttpServlet
  {

 protected void processRequest(HttpServletRequest request, 
    HttpServletResponse response)
        throws ServletException, IOException {
    ....
    InputStream is = filePart.getInputStream()
    emp1.setFileBytes(IOUtils.toByteArray(is))
    }
......
}

public Employee addImage(Employee emp1) {
  Connection con = null;
  Statement stmt = null;
  ....
   pstmt.setBytes(5,emp1.getFileBytes());
  ....
}
Sign up to request clarification or add additional context in comments.

3 Comments

here it is showing error when i am using in servlet this line emp1.setFileBytes(filePart.getBytes()) , it is saying method getBytes is undefined for the type part... even trying to cast but it is not working.... suggest me here something
its not working in the sense where is it failing? is your addImage method getting executed successfully? In this method in the catch SQLException block please put logger and check
one more question here is link on same site related to same topic please bro review that and try to help me ... stackoverflow.com/questions/25135454/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.