0

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.

5
  • Sorry, I should have explained better. The method returns false when called from another class, and of course the image is not inserted into the db. Commented Oct 7, 2013 at 11:48
  • 2
    Stop catching Exception. A exception tells you what's wrong, and where. By catching it and ignoring it, you make it impossible to diagnose the problem. Commented Oct 7, 2013 at 11:50
  • Right, seems kinda obvious now. I'll do that and try to fix whatever error I'm making. Thanks. Commented Oct 7, 2013 at 11:56
  • 1
    writing image into database?! not cool, save the file in file system and save the file path in db. Commented Oct 7, 2013 at 12:18
  • 1
    "Stop catching Exception" should be "stop swallowing Exception" Commented Oct 7, 2013 at 13:43

3 Answers 3

2

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;
}

Correctly:

} 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:

  • please post the full stacktrace you get
  • mark the line of the error
Sign up to request clarification or add additional context in comments.

1 Comment

Well, now I get an error report but I guess I can work with that. Thank you! (I will not do such thing again!)
0

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

1 Comment

Well, that's embarrassing. The name of the table was "image" in singular and I had "images" in the Java code. It's working now. Thank you!
-1

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.

1 Comment

It's indeed a class work, I'll work with filepaths in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.