2

次のように、アップロードされたファイルを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)

このエラーの原因は何でしょうか?入力フィールド名は、firstName、lastName、および写真です

4

2 に答える 2

3

これを機能させるために使用する必要があります mysql-connector-java-5.1.7-bin.jar

于 2015-02-06T09:46:21.580 に答える
1

この投稿で giorgiga が述べているように、JDBC ドライバーのバージョンです。更新するか、古いバージョンの setBlob を使用してください。

編集: リンクが切れた場合に備えて、giorgigaの回答から取得。

AbstractMethodError は、JDBC ドライバーの PreparedStatements が setBlob(int, InputStream, long) を実装していないことを意味します。

古い setBlob(int, Blob) を使用するか、ドライバーを更新します (Connector/J 5.1 は Jdbc 4.0 を実装します。これは、setBlob(int, InputStream, long) に必要なものである必要があります)。

于 2013-04-06T19:59:07.930 に答える