-1

Servlet

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

    String firstname = request.getParameter("Firstname");
    String lastname = request.getParameter("Lastname");
    String gender = request.getParameter("gender");
    String dob = request.getParameter("dob");
    String email = request.getParameter("email");
    String experience =  request.getParameter("experience");
    String password = request.getParameter("pwd");
    String confirmpassword = request.getParameter("cpwd");
    String address = request.getParameter("address");
    String mobilenumber = request.getParameter("mobile");



    Part file = request.getPart("fileUpload");

    inputStream = file.getInputStream();

        CandidatePojo pojo = new CandidatePojo(firstname, lastname, gender,dob,email,experience,password,confirmpassword,address,mobilenumber,file);

        CandidateRegistrationDao dao = new CandidateRegistrationDao();

        try {
            pojo = dao.insertdata(pojo);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.sendRedirect("index.jsp");

    }

Dao

public CandidatePojo insertdata(CandidatePojo pojo) throws SQLException { String sql = "INSERT INTO candidateregist (firstname, lastname, gender,dob,email,experience,pwd,cpwd,address,mobile,resume) VALUES (?, ?, ? ,? ,? ,? ,? , ? ,? ,?, ? )"; connect();

    PreparedStatement statement = jdbcConnection.prepareStatement(sql);
    statement.setString(1, pojo.getFirstname());
    statement.setString(2, pojo.getLastname() );
    statement.setString(3, pojo.getGender());
    statement.setString(4, pojo.getDob());
    statement.setString(5, pojo.getEmail());
    statement.setString(6, pojo.getExperience());
    statement.setString(7, pojo.getPassword());
    statement.setString(8, pojo.getConfirmpassword());
    statement.setString(9, pojo.getAddress() );
    statement.setString(10, pojo.getMobilenumber());  


    statement.setBlob(11, (Blob) pojo.getFile());



    int rowsInserted = statement.executeUpdate();
    if (rowsInserted > 0) {
        System.out.println("A new record was inserted successfully!");
    }

    //boolean rowInserted = statement.executeUpdate() > 0;
    statement.close();
    disconnect();
    return pojo;
}

Pojo

public class CandidatePojo {

private String firstname;
private String lastname;
private String gender;
private String dob;
private String email;
private String experience;
private String password;
private String confirmpassword;
private String address;
private String mobilenumber;
private Part file;


public CandidatePojo(String firstname, String lastname, String gender, String dob, String email, String experience,
        String password, String confirmpassword, String address, String mobilenumber,Part file) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.gender = gender;
    this.dob = dob;
    this.email = email;
    this.experience = experience;
    this.password = password;
    this.confirmpassword = confirmpassword;
    this.address = address;
    this.mobilenumber = mobilenumber;
    this.file = file;

}

HI... I cannot cast Blob to my coding , please some one find what error in this code..

Getting Error like this in console

java.lang.ClassCastException: org.apache.catalina.core.ApplicationPart cannot be cast to java.sql.Blob at registration.Dao.CandidateRegistrationDao.insertdata(CandidateRegistrationDao.java:62) at registration.servelt.RegistrationController.doGet(RegistrationController.java:75) at registration.servelt.RegistrationController.doPost(RegistrationController.java:48) at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

  • Your Problem is at the following line in CandidateRegistrationDao: statement.setBlob(11, (Blob) pojo.getFile()); Your pojo does not contain a java.sql.Blob, but a org.apache.catalina.core.ApplicationPart – keuleJ Mar 07 '19 at 15:54
  • I think you have to call getInputStream() on the Part and save the content of the stream: https://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/Part.html#getInputStream() – keuleJ Mar 07 '19 at 15:57

1 Answers1

1

The issue is that you're tying to directly convert an org.apache.catalina.core.ApplicationPart into a Blob by direct cast:

statement.setBlob(11, (Blob) pojo.getFile());

The problem is that a Part cannot be cast to a Blob in this way.

A way to sort this out would be to convert the Part into an InputStream and convert that into a byte[], which is understood by a SerialBlob, which is usable as a Blob - a bit of a long route.

Using Commons IO. Add the following to the pojo to turn the Part into a byte[]:

public byte[] getFileAsByteA() {
    try {
        return IOUtils.toByteArray(file.getInputStream());
    } catch (IOException ex) {
        return null;
    }
}

Add the following to the insert code:

javax.sql.rowset.serial.SerialBlob sb = new SerialBlob(pojo.getFileAsByteA());
statement.setBlob(11, sb);

Note: code just cobbled together.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122