This question already has an answer here:
这个问题已经有了答案:
- Why do I get java.lang.AbstractMethodError when trying to load a blob in the db? 14 answers
- 为什么我得到了java。lang。当试图在db中加载blob时,会发生什么情况?14个答案
I am trying to save an uploaded file in MySQL database as follows:
我正在尝试在MySQL数据库中保存上传的文件如下:
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)
what could be the reason of this error ?input field names are firstName,lastName and photo
这个错误的原因是什么?输入字段名是firstName,lastName和photo
2 个解决方案
#1
1
As stated by giorgiga in this post its your JDBC driver's version. Either update it or use the older version of setBlob.
如giorgiga在这篇文章中所述,它是你的JDBC驱动程序的版本。要么更新它,要么使用旧版本的setBlob。
Edit: Taken from giorgiga's answer just in case link dies.
编辑:摘自乔尔乔加的答案,以防链接死亡。
AbstractMethodError means your JDBC driver's PreparedStatements don't implement setBlob(int, InputStream, long).
AbstractMethodError意味着您的JDBC驱动程序的preparedstatement没有实现setBlob(int, InputStream, long)。
Use the older setBlob(int, Blob) or update your driver (Connector/J 5.1 implements Jdbc 4.0, which should be what you need for setBlob(int, InputStream, long))
使用旧的setBlob(int, Blob)或更新驱动程序(Connector/J 5.1实现了Jdbc 4.0,这应该是setBlob(int, InputStream, long))需要的)
#2
1
You should use mysql-connector-java-5.1.7-bin.jar
to make this work.
您应该使用mysql-connector-java-5.1.7-bin。jar让它工作。
#1
1
As stated by giorgiga in this post its your JDBC driver's version. Either update it or use the older version of setBlob.
如giorgiga在这篇文章中所述,它是你的JDBC驱动程序的版本。要么更新它,要么使用旧版本的setBlob。
Edit: Taken from giorgiga's answer just in case link dies.
编辑:摘自乔尔乔加的答案,以防链接死亡。
AbstractMethodError means your JDBC driver's PreparedStatements don't implement setBlob(int, InputStream, long).
AbstractMethodError意味着您的JDBC驱动程序的preparedstatement没有实现setBlob(int, InputStream, long)。
Use the older setBlob(int, Blob) or update your driver (Connector/J 5.1 implements Jdbc 4.0, which should be what you need for setBlob(int, InputStream, long))
使用旧的setBlob(int, Blob)或更新驱动程序(Connector/J 5.1实现了Jdbc 4.0,这应该是setBlob(int, InputStream, long))需要的)
#2
1
You should use mysql-connector-java-5.1.7-bin.jar
to make this work.
您应该使用mysql-connector-java-5.1.7-bin。jar让它工作。