java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15855222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:03:54  来源:igfitidea点击:

java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V

javamysqljdbcblob

提问by user2137186

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

这个错误的原因可能是什么?输入字段名称是名字,姓氏和照片

回答by monu dwivedi

You should use mysql-connector-java-5.1.7-bin.jarto make this work.

你应该使用 mysql-connector-java-5.1.7-bin.jar来完成这项工作。

回答by ug_

As stated by giorgiga in this postits 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.

编辑:摘自 giorgiga 的回答,以防链接失效。

AbstractMethodError means your JDBC driver's PreparedStatements don't implement 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))

AbstractMethodError 意味着您的 JDBC 驱动程序的 PreparedStatements 没有实现 setBlob(int, InputStream, long)。

使用旧的 setBlob(int, Blob) 或更新您的驱动程序(Connector/J 5.1 实现了 Jdbc 4.0,这应该是 setBlob(int, InputStream, long) 所需要的)