Advertisement
MOSDEV

Adding Thumbnail

Dec 7th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. public void setThumbnail(BufferedImage thumbnail) throws SQLException, IOException {
  2.         TinyLink tinyLink = this;
  3.         if (thumbnail != null) {
  4.             Connection connection = databaseInstance.getConnection();
  5.             if (connection == null) return;
  6.             String statement = "UPDATE TinyLink set thumbnail=? WHERE id=?";
  7.             PreparedStatement preparedStatement = connection.prepareStatement(statement);
  8.  
  9.             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  10.             ImageIO.write(thumbnail, "jpg", byteArrayOutputStream);
  11.             InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
  12.  
  13.             preparedStatement.setBinaryStream(1, inputStream);
  14.             preparedStatement.setInt(2, tinyLink.id);
  15.  
  16.             byteArrayOutputStream.flush();
  17.             byteArrayOutputStream.close();
  18.             inputStream.close();
  19.             preparedStatement.executeUpdate();
  20.             preparedStatement.close();
  21.         }
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement