Java文件上传到MySQL

Kai*_*ugo 5 java mysql blob jdbc r.java-file

我有这个问题java.io.File从选择a JFileChooser上传所述java.io.File对象到具有此表结构的MySQL表

     COL_NAME  COL_TYPE     ATTRIBUTES        EXTRA
(PK) idSample  int(10)      UNSIGNED ZEROFILL AUTO_INCREMENT
     FileName  varchar(250)
     FileBin   blob         BINARY 
Run Code Online (Sandbox Code Playgroud)

从这个Java代码中,该newConnection方法是该类的静态方法,其中它将DriverManager的新实例默认返回到所述MySQL数据库.另一件事,我使用的java.sql包不是com.mysql.jdbc包.

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;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行该程序时,它返回一个错误,导致该statement.setBlob行具有此堆栈跟踪:

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

我该如何解决这个问题?

gio*_*iga 2

AbstractMethodError意味着您的 JDBC 驱动程序PreparedStatement没有实现setBlob(int, InputStream, long).

使用旧版本setBlob(int, Blob)或更新您的驱动程序(Connector/J 5.1 实现 Jdbc 4.0,这应该是您所需要的setBlob(int, InputStream, long)