我正在尝试使用javafx从mysql数据库检索图像。图像使用长块存储在数据库中。这样做时会出现垃圾值。这是用于从数据库检索图像的代码。
JFXTreeTableColumn<Property,String> propertyImage = new JFXTreeTableColumn<>("Image");
propertyImage.setPrefWidth(100);
propertyImage.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<Property, String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TreeTableColumn.CellDataFeatures<Property, String> param) {
return param.getValue().getValue().propertyImage;
}
});
数据库连接
public class Property extends RecursiveTreeObject<Property>{
StringProperty propertyId;
StringProperty address;
StringProperty state;
StringProperty zipCode;
StringProperty price;
StringProperty bedNum;
StringProperty bathNum;
StringProperty propertyImage;
StringProperty propertyType;
StringProperty propertyStatus;
StringProperty squareFeet;
StringProperty parkingSpot;
StringProperty agent_id;
public Property(){
super();
}
public Property(String propertyId, String address, String state, String zipCode, String price, String bedNum, String bathNum, String propertyImage, String propertyType, String propertyStatus, String squareFeet, String parkingSpot, String agent_id) {
this.propertyId = new SimpleStringProperty(propertyId);
this.address = new SimpleStringProperty(address);
this.state = new SimpleStringProperty(state);
this.zipCode = new SimpleStringProperty(zipCode);
this.price = new SimpleStringProperty(price);
this.bedNum = new SimpleStringProperty(bedNum);
this.bathNum = new SimpleStringProperty(bathNum);
this.propertyImage = new SimpleStringProperty(propertyImage);
this.propertyType = new SimpleStringProperty(propertyType);
this.propertyStatus = new SimpleStringProperty(propertyStatus);
this.squareFeet = new SimpleStringProperty(squareFeet);
this.parkingSpot = new SimpleStringProperty(parkingSpot);
this.agent_id = new SimpleStringProperty(agent_id);
}
}
数据库连接
ObservableList<Property> properties = FXCollections.observableArrayList();
Connection connection = DBConnection.getConnection();
try {
PreparedStatement ps = (PreparedStatement)connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
properties.add(new Property(rs.getInt(1)+"",rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),rs.getString(13)));
}
} catch (SQLException ex) {
Logger.getLogger(SearchSalePropertyController.class.getName()).log(Level.SEVERE, null, ex);
}
答案 0 :(得分:0)
最后,您发布了用于数据库访问的代码。从ResultSet
获取数据时,您已经做错了。
while(rs.next()) {
InputStream in = new rs.getBinaryStream(8);
Image image = getImageFromStream(in); // You need to convert this back into a JavaFX Image instance,
// which depends on the original Image, e.g. file format etc.
// It could be as simply as new Image(in) using the JavaFX Image constructor
properties.add(new Property(
rs.getInt(1) + "",
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6),
rs.getString(7),
image,
rs.getString(9),
rs.getString(10),
rs.getString(11),
rs.getString(12),
rs.getString(13)
));
}
当然,Property.propertyImage
也应更改为ObjectProperty<Image>
。同样,该列也应更改为JFXTreeTableColumn<Property, Image>
。