Reputation: 35
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.
Upvotes: 0
Views: 2425
Reputation: 1695
The stacktrace suggests that table "images" was not found. Some possibilities:
1) "images" is not the right table name; table was created with quotes
2) you need to specify database schema - either in your query (I would first try this), either in your settings file
3) something else in settings file is incorrect
Also, take a look at these posts:
- Java SQL "ERROR: Relation "Table_Name" does not exist"
- PSQLException: ERROR: relation "TABLE_NAME" does not exist
Upvotes: 0
Reputation: 182
database guy here. This is acceptable for class work but if you store images in a database for a real application with more than a handful of users, you will most likely be sorry and your database will be slow. A preferred pattern is to store filepaths in the database but not image files.
Upvotes: -1
Reputation: 20736
This is your error. Write down 1000 times on a blackboard I will not do such again!.
// BAD BAD BAD BAD BAD BAD BAD
} catch (Exception e) {
return false;
}
This would be a step closer, but still bad:
// BAD BAD BAD BAD BAD BAD BAD
} catch (Exception e) {
//NEVER do this. It hides the stacktrace, the main point of logging an error...
System.err.println(e.getMessage());
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
Or even better, with a proper logger:
} catch (Exception e) {
//notice the ",e"! 99.999999% you have to log with full stacktrace!
LOG.error("Unexpected error during file upload", e);
return false;
}
You swallowed the error. This is bad. Very-very bad. Such code can be grounds for dismissal...
And now:
Upvotes: 2