Guest User

Untitled

a guest
Jun 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. boolean saveImage(String fileName, String contentType, InputStream inputStream) {
  2. Connection connection = null;
  3. PreparedStatement statement = null;
  4. ResultSet keySet = null;
  5.  
  6. try {
  7. connection = dataSource.getConnection();
  8. statement = connection.prepareStatement("INSERT INTO images (filename, content_type, content) values (?, ?, ?)",
  9. new String[] {"id"});
  10. statement.setString(1, fileName);
  11. statement.setString(2, contentType);
  12. statement.setBinaryStream(3, inputStream);
  13.  
  14. statement.execute();
  15.  
  16. keySet = statement.getGeneratedKeys();
  17. if (keySet.next()) {
  18. System.out.println("The new image ID is " + keySet.getInt(1));
  19. }
  20.  
  21. return true;
  22. } catch (SQLException e) {
  23. e.printStackTrace();
  24. } finally {
  25. close(keySet);
  26. close(statement);
  27. close(connection);
  28. }
  29.  
  30. return false;
  31. }
  32. }
Add Comment
Please, Sign In to add comment