Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- boolean saveImage(String fileName, String contentType, InputStream inputStream) {
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet keySet = null;
- try {
- connection = dataSource.getConnection();
- statement = connection.prepareStatement("INSERT INTO images (filename, content_type, content) values (?, ?, ?)",
- new String[] {"id"});
- statement.setString(1, fileName);
- statement.setString(2, contentType);
- statement.setBinaryStream(3, inputStream);
- statement.execute();
- keySet = statement.getGeneratedKeys();
- if (keySet.next()) {
- System.out.println("The new image ID is " + keySet.getInt(1));
- }
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- close(keySet);
- close(statement);
- close(connection);
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment