Java File upload to MySQL [duplicate]

2019-02-14 05:44发布

This question already has an answer here:

I have this problem getting a java.io.File from the selection of a JFileChooser to upload the said java.io.File object to the MySQL Table having this table structure

     COL_NAME  COL_TYPE     ATTRIBUTES        EXTRA
(PK) idSample  int(10)      UNSIGNED ZEROFILL AUTO_INCREMENT
     FileName  varchar(250)
     FileBin   blob         BINARY 

And from this Java code, the newConnection method is a static method of the class wherein it returns a new instance of a DriverManager default to the said MySQL Database. Another thing, I am using the java.sql package not the com.mysql.jdbc package.

public static boolean insert(final String filename, File file) throws SQLException, IOException {
    boolean flag = false;
    try {
        Connection connection = newConnection();
        PreparedStatement statement = connection.prepareStatement(SQL_INSERT);
        statement.setString(1, filename);
        statement.setBlob(2, new FileInputStream(file), file.length());
        flag = statement.executeUpdate() > 0;
        statement.close();
        connection.close();
    } catch (SQLException ex) {
        throw ex;
    } catch (IOException ex) {
        throw ex;
    }
    return flag;
}

When I tried to run the program, it returns an error leading to the statement.setBlob line having this stacktrace:

Exception in thread "main" java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;J)V

How can I resolve this problem?

1条回答
家丑人穷心不美
2楼-- · 2019-02-14 06:35

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

查看更多
登录 后发表回答