Thread: Writing Binary

Writing Binary

From
buki@gmx.net (Andreas Buer)
Date:
Hi,

I've tried the example explained in the postgresql 7.1 doc.
But somehow it won't work. Instead of a gif I try to store a pdf.

openDB();
// All LargeObject API calls must be within a transaction
db.setAutoCommit(false);

File file = new File("c:/test/d.pdf");
FileInputStream fis = new FileInputStream(file);

PreparedStatement ps =
       db.prepareStatement("INSERT INTO blob_test VALUES (?, ?)");

ps.setString(1, file.getName());

// cast because long won't work
ps.setBinaryStream(2, fis, (int)file.length());
ps.executeUpdate();

ps.close();
fis.close();


I get the following output:
16\020h[2>\3345\3209u\316\360#\367<\3.............(seems to be the pdf)
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:94)
    at org.postgresql.Connection.ExecSQL(Connection.java:398)
    at org.postgresql.jdbc2.Statement.execute(Statement.java:130)
    at org.postgresql.jdbc2.Statement.executeUpdate(Statement.java:73)
    at org.postgresql.jdbc2.PreparedStatement.executeUpdate(PreparedStatement.java:113)
    at Blob.main(Blob.java:26)

I'm using PostGreSql 7.1.3, ODBC Driver Version
7.2.4 and j2sdk1.4.1

Best regards

Andi