java.lang.AbstractMethodError:com.mysql.jdbc.PreparedStatement.setBlob(ILjava / io / InputStream;)V [英] java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V
本文介绍了java.lang.AbstractMethodError:com.mysql.jdbc.PreparedStatement.setBlob(ILjava / io / InputStream;)V的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将上传的文件保存在MySQL数据库中,如下所示:
I am trying to save an uploaded file in MySQL database as follows:
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("photo");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// constructs SQL statement
String sql = "INSERT INTO image(image,firstName, lastName) values (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(1, inputStream);
}
statement.setString(2, firstName);
statement.setString(3, lastName);
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// sets the message in request scope
request.setAttribute("Message", message);
// forwards to the message page
getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
}
当我运行这个时,我得到一个错误:
When i run this i get an error which is:
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V
UploadServlet.doPost(UploadServlet.java:64)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
这个错误可能是什么原因?输入字段名称是firstName,lastName和photo
what could be the reason of this error ?input field names are firstName,lastName and photo
推荐答案
你应该使用 mysql-connector-java-5.1.7-bin.jar
来完成这项工作。
You should use mysql-connector-java-5.1.7-bin.jar
to make this work.
这篇关于java.lang.AbstractMethodError:com.mysql.jdbc.PreparedStatement.setBlob(ILjava / io / InputStream;)V的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文