So I m trying to display images stored in my database as Blob to an image column in table view. All of the other data is displayed well except for those in the image column. I ve been struggling for days but still unable to find a solution.
这里我迄今为止所尝试的:
public void loadPropertyData() throws IOException {
try {
Connection connection = OracleConnect.getConnection();
String query = "SELECT id, property, image, price, contact, location, status FROM property" + getProfileName().getText();
PreparedStatement statement = connection.prepareStatement(query);
ResultSet resultSet = statement.executeQuery();
ObservableList<Property> propertyList = FXCollections.observableArrayList();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String property = resultSet.getString("property");
Blob imageBlob = resultSet.getBlob("image");
Image image = null;
if (imageBlob != null) {
InputStream is = imageBlob.getBinaryStream();
image = new Image(is);
}
String price = resultSet.getString("price");
String contact = resultSet.getString("contact");
String location = resultSet.getString("location");
String status = resultSet.getString("status");
Property propertyObj = new Property(id, property, image, price, contact, location, status);
propertyList.add(propertyObj);
}
propertyIDColumn.setCellValueFactory(cellData -> Bindings.createObjectBinding(() -> cellData.getValue().getId()));
propertyColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getProperty()));
propertyImageColumn.setCellFactory(param -> new TableCell<Property, ImageView>() {
private final ImageView imageView = new ImageView();
{
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
@Override
protected void updateItem(ImageView item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
imageView.setImage(null);
setText(null);
setGraphic(null);
} else {
imageView.setImage(item.getImage());
setGraphic(imageView);
}
}
});
propertyPriceColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPrice()));
propertyContactColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getContact()));
propertyLocationColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getLocation()));
propertyStatusColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getStatus()));
propertyTableView.setItems(propertyList);
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
I would appreciate any help.