Created
June 19, 2018 23:56
-
-
Save orb/d5418cf4c7e709305a2670ee8e694e73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment