5

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?

4
  • Have you tried using SetBinaryStream instead of SetBlob? docs.oracle.com/javase/6/docs/api/java/sql/…, java.io.InputStream) Commented Dec 9, 2012 at 17:50
  • Still return the same exception. Commented Dec 9, 2012 at 18:07
  • The exception is telling you that the last parameter is a long (J). Try casting file.length to int: ...., (int) file.length()); Commented Dec 9, 2012 at 18:26
  • Read the byte data from the stream and use that as your argument to setBlob instead. Commented Dec 9, 2012 at 19:01

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.