I'm trying to write a JSP web app that allows to upload images to a PostgreSQL database. I was following this as a guide, but the image is not uploaded to the DB and the method (below) enters the catch.
This is my code so far:
public boolean upIm() {
try {
File file = new File("bg.jpg");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = con.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, "background");
ps.setBinaryStream(2, fis, (int) file.length());
ps.executeUpdate();
ps.close();
fis.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
There seems to be a problem with the FileInputStream, because the statement that goes to the db is INSERT INTO images VALUES ('background', ?)
, and I've tested file.length() and it works fine.
That's it; if you need more info or more code please let me know.
EDIT: I get this stacktrace:
org.postgresql.util.PSQLException: ERROR: relation "images" does not exist
Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:363)
at bd.provaImg.upIm(provaImg.java:50)
at bd.prova2.main(prova2.java:14)
I think position 13 is the line in the class (not shown here) that simply instances the class in which there is this method.