Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java File upload to MySQL [duplicate]

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?

like image 833
David B Avatar asked Nov 03 '22 09:11

David B


1 Answers

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

like image 121
giorgiga Avatar answered Nov 09 '22 13:11

giorgiga