Skip to content

Instantly share code, notes, and snippets.

@orb orb/saveImage.java
Created Jun 19, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.