java.lang.AbstractMethodError:com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V
我正在尝试将上传的文件保存到MySQL数据库中,如下所示:java.lang.AbstractMethodError:com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V,java,mysql,jdbc,blob,Java,Mysql,Jdbc,Blob,我正在尝试将上传的文件保存到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 multi
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);
}
当我运行此命令时,会出现以下错误:
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,正如giorgiga在其your JDBC驱动程序版本中所述。更新它或使用旧版本的setBlob
编辑:
摘自乔治加的答案,以防万一林克死了
AbstractMethodError意味着您的JDBC驱动程序的PreparedStatements不实现setBlob(int、InputStream、long)
使用旧的setBlob(int,Blob)或更新驱动程序(Connector/j5.1实现了jdbc4.0,这应该是setBlob(int,InputStream,long)所需要的)
正如giorgiga在其JDBC驱动程序版本中所述。更新它或使用旧版本的setBlob
编辑:
摘自乔治加的答案,以防万一林克死了
AbstractMethodError意味着您的JDBC驱动程序的PreparedStatements不实现setBlob(int、InputStream、long)
使用旧的setBlob(int,Blob)或更新驱动程序(Connector/j5.1实现了jdbc4.0,这应该是setBlob(int,InputStream,long)所需要的)
你应该使用
mysql-connector-java-5.1.7-bin.jar
来完成这项工作。你应该使用mysql-connector-java-5.1.7-bin.jar
来完成这项工作。setBlob的旧版本是什么?这是旧版本我猜setBlob的旧版本是什么?这是旧版本我猜Hey。你是如何解决这个问题的,因为这里的答案对我也不起作用。@Kanchan如果你把它保存在一个文件夹中,并将该图像的路径存储在数据库中会更好。您是如何解决这个问题的,因为这里的答案对我也不起作用。@Kanchan最好将它保存在文件夹中,并将该图像的路径存储在数据库中