java mysql setblob_java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io...

问题

This question already has answers here:

Why do I get java.lang.AbstractMethodError when trying to load a blob in the db?

(14 answers)

Closed 3 years ago.

I am trying to save an uploaded file in MySQL database as follows:

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

回答1:

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

回答2:

As stated by giorgiga in this post its your JDBC driver's version. Either update it or use the older version of 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).

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

来源:https://stackoverflow.com/questions/15855222/java-lang-abstractmethoderror-com-mysql-jdbc-preparedstatement-setblobiljava-i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值