Simon Mardiné
Simon Mardiné

Reputation: 520

Insert file in blob field with jaybird 2.1.6

I'm trying to insert a file in a blob field in a Firebird database using lib jaybird 2.1.6.

First, I create a record in my database, then, with the id of record, I try to insert my file in a blob (subtype 0) field.

Here is my code:

public static boolean insertBlob(File p_file, String p_maxId) {
    String requette = 
        "UPDATE MAIL_RECU a SET a.CONTENU=? where a.ID_MESSAGE_RECU="+ p_maxId;

    PreparedStatement ps = null;
    FileInputStream input = null;
    try {
        ps = laConnexion.prepareStatement(requette);
        input = new FileInputStream(p_file);

        int paramIdx = 1;
        ps.setBinaryStream(paramIdx++, input, p_file.length());
        ps.executeUpdate();
    } catch (SQLException e) {
        System.out.println("cause: " + e.getCause());
        System.out.println("stacktrace: " + e.getStackTrace());
        System.out.println(e);
        messageUtilisateur.affMessageException(e,
                "Erreur à l'insertion d'un blob");
    } catch (FileNotFoundException e) {
        messageUtilisateur.affMessageException(e,
                "impossible de trouver le fichier");
    } finally {
        try {
            ps.close();
            input.close();

        } catch (SQLException e) {
            messageUtilisateur.affMessageException(e,
                    "Erreur à l'insertion d'un blob");
        } catch (IOException e) {
            messageUtilisateur.affMessageException(e, "fichier non trouvé");

        }
    }

    return true;
}

The problem is I have an exception when

ps.setBinaryStream(paramIdx++, input, p_file.length());

is executed.

I have a message "java.sql.SQLException: not yet implemented". My question is, is anyone have already have this problem? If yes, how did he (or she) fix it? Is there another way to store file in blob with jaybird?

Upvotes: 1

Views: 1324

Answers (1)

user330315
user330315

Reputation:

setBinaryStream(int, InputStream, long) is a JDBC4 (Java 6) method.

As far as I know Jaybird is still JDBC3, so you need to use PreparedStatement.setBinaryStream(int, InputStream, int) instead:

ps.setBinaryStream(paramIdx++, input, (int)p_file.length());

Upvotes: 2

Related Questions