有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

无法转换为java。sql。斑点

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)抛出SQLException{ String sql=“插入候选专家(名字、姓氏、性别、出生日期、电子邮件、经验、pwd、cpwd、地址、手机、简历)值(?,,,,,,,,,,,,?)”; 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

公共类候选人{

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;

}

嗨。。。我无法将Blob投射到我的代码中,请有人查找此代码中的错误

在控制台中获取这样的错误

爪哇。lang.ClassCastException:org。阿帕奇。卡塔琳娜。果心ApplicationPart无法转换为java。sql。斑点 登记时。刀。候选人登记道。insertdata(CandidateRegistrationDao.java:62) 登记时。瑟维尔特。注册管理员。doGet(RegistrationController.java:75) 登记时。瑟维尔特。注册管理员。doPost(RegistrationController.java:48) 在javax。servlet。http。HttpServlet。服务(HttpServlet.java:660) 在javax。servlet。http。HttpServlet。服务(HttpServlet.java:741)


共 (1) 个答案

  1. # 1 楼答案

    问题是,您正在通过直接强制转换将org.apache.catalina.core.ApplicationPart直接转换为Blob

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

    问题是Part不能以这种方式强制转换为Blob

    对此进行分类的一种方法是将Part转换为InputStream并将其转换为byte[],这可以被SerialBlob理解为一个Blob,它可以作为一个Blob使用,这是一个长路径的一点

    使用Commons IO。将以下内容添加到pojo以将零件转换为byte[]

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

    将以下内容添加到插入代码中:

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

    Note: code just cobbled together.