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;
}