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

标签 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 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)

此错误的原因可能是什么?输入字段名称为名字、姓氏和照片

最佳答案

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

关于java.lang.AbstractMethodError : com. mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15855222/

相关文章:

java - SQL 查询可在 MySQL 中运行,但不能在 Java 中运行

Java JDBC : Reply. 填充()

java - 可调用接口(interface)循环停止主要方法进一步执行

java - 依赖于特定 dll 的 play(java) 项目如何在非 Windows 计算机上运行?

java - 调用 Restful Webservice 时 Uri 不是绝对异常

java - 通过 JDBC 连接到远程 Mapr Hive

mysql - WordPress 数据库错误日志但博客已加载且看起来正常

MySQL触发器更改插入的文本

mysql - Delphi + Zeos 上的临时自动增量列,多个查询失败?

sql - MyBatis RowBounds vs Oracle 分页查询使用 rownum 和嵌套子查询