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
公共CandidatePojo insertdata(CandidatePojo pojo)引发SQLException { 字符串sql =“在候选注册表中插入(名字,姓氏,性别,dob,电子邮件,体验,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
公共类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;
}
嗨...我无法将Blob转换为我的编码,请有人发现此代码中有什么错误。
在控制台中出现这样的错误
java.lang.ClassCastException:org.apache.catalina.core.ApplicationPart无法转换为java.sql.Blob 在registration.Dao.CandidateRegistrationDao.insertdata(CandidateRegistrationDao.java:62) 在registration.servelt.RegistrationController.doGet(RegistrationController.java:75) 在registration.servelt.RegistrationController.doPost(RegistrationController.java:48) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:660) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
答案 0 :(得分:1)
问题是您要通过直接投射直接将org.apache.catalina.core.ApplicationPart
转换为Blob
:
statement.setBlob(11, (Blob) pojo.getFile());
问题在于无法以这种方式将Part
强制转换为Blob
。
解决此问题的一种方法是将Part
转换为InputStream
并将其转换为byte[]
,SerialBlob可以理解,即可用作Blob-距离很长。
使用Commons IO。将以下内容添加到pojo中,以将Part变成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);
注意:代码只是拼凑在一起。