Can not save binary using setBinaryStream to postgreSQL using java prepared statement

359 Views Asked by At

I have code to save binary to PostgreSQL, I am using JDK 1.5. but I got error..

And after I print the insert statement, then I try in my postgresql console, something error like this image:

https://imgur.com/IF4hI3A

File file = new File("E:\\myimage.gif");
FileInputStream fis;

try {
    fis = new FileInputStream(file);
    PreparedStatement ps = conn.prepareStatement("INSERT INTO golf_fnb.coba VALUES (?)");
    ps.setBinaryStream(1, fis, (int)file.length());
    System.out.println("SQl: "+ps);
    ps.executeUpdate();
    ps.close();
    fis.close();
} catch (FileNotFoundException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

and this is the error in my eclipse console:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "\"

    at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139)
    at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:152)
    at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:100)
    at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43)
    at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:517)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50)
    at org.postgresql.jdbc1.AbstractJdbc1Statement.executeUpdate(AbstractJdbc1Statement.java:273)
    at finger.ConsoleUserInterfaceFactory$ConsoleUserInterface.verify4(ConsoleUserInterfaceFactory.java:605)
    at finger.ConsoleUserInterfaceFactory$ConsoleUserInterface.run(ConsoleUserInterfaceFactory.java:117)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:651)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:676)
    at java.lang.Thread.run(Thread.java:595)
1

There are 1 best solutions below

1
On

I think the main error is using the column. Syntax: INSERT INTO TABLE(col1, ...) VALUES(val1, ...).

It just might also be you(or someone having a similar problem) intendedUPDATE golf_fnb SET coba = ? WHERE id = ?`. For the INSERT:

try (FileInputStream fis = new FileInputStream(file);
        PreparedStatement ps = conn.prepareStatement("INSERT INTO golf_fnb(coba) VALUES (?)",
                Statement.RETURN_GENERATED_KEYS)) {
    ps.setBinaryStream(1, fis, (int)file.length());
    System.out.println("SQl: "+ps);
    int updateCount = ps.executeUpdate();
    if (updateCount == 1) {
        try (ResultSet rs = ps.getGeneratedKeys()) {
            if (rs.next()) {
                long id = rs.getLong(1);
                System.out.println("ID " + id);
                return;
            }
        }
     }
} catch (SQLException | IOException e) {
    e.printStackTrace();
}
  • Used try-with-resources to close all automatically.
  • The inserted record one might like to find. Assuming a long primary key, added getGeneratedKeys.
  • \', the apostrophe might give some problem. Manualy one should have \047 instead maybe. I hope this to-octal-conversion by the driver disappears with the new syntax above.