gpt4 book ai didi

Java FX 在搜索查询中显示图像

转载 作者:行者123 更新时间:2023-12-02 08:55:21 24 4
gpt4 key购买 nike

我最近正在使用 blob 图像进行图像上传,并且它有效但现在当我尝试在搜索中搜索特定的人时,它没有显示我试图将 blob 图像转换为字节并以 java fx 形式显示在图像字段中

这是我的代码

 @FXML
private void handleSearchResults(KeyEvent event) throws IOException {

try {

String sql = "select * from Staff_information where id=? ";
pst = conn.prepareStatement(sql);
pst.setString(1, txtSearch.getText());
rs = pst.executeQuery();

String add1 = rs.getString("id");
txtid.setText(add1);

byte[] img = rs.getBytes("Image");
BufferedImage bufferedImage = ImageIO.read(img);
WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);

imageDisplay.setImage(image);
imageDisplay.setFitWidth(200);
imageDisplay.setFitHeight(200);


} catch (Exception e) {
System.out.println("missingdata");
} finally {

try {

rs.close();
pst.close();

} catch (Exception e) {

}
}

}

这是我的文件上传代码,可以正常工作

文件上传

 @FXML
private void UploadImageActionPerformed(ActionEvent event) {

FileChooser fileChooser = new FileChooser();

//Set extension filter
FileChooser.ExtensionFilter extFilterJPG
= new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG");
FileChooser.ExtensionFilter extFilterjpg
= new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg");
FileChooser.ExtensionFilter extFilterPNG
= new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG");
FileChooser.ExtensionFilter extFilterpng
= new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters()
.addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);

try {
BufferedImage bufferedImage = ImageIO.read(file);
WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);
img.setImage(image);
img.setFitWidth(200);
img.setFitHeight(200);
img.scaleXProperty();
img.scaleYProperty();
img.setSmooth(true);
img.setCache(true);
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];

for (int readNum; (readNum = fin.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
}
person_image = bos.toByteArray();

} catch (IOException ex) {
Logger.getLogger("ss");
}
}


byte[] person_image = null;

@FXML
private void handleaddemployee(ActionEvent event) throws IOException {

try {
String empID = "nill";
if (!txtempID.getText().equals("")) {
empID = txtempID.getText();
}

Employee e = new Employee(
empID,
person_image
);
dbemployee.addEmployee(e);

} else {
System.out.println("not working");
}

} catch (SQLException ex) {
System.out.println("error");

} catch (NumberFormatException e) {
System.out.println("number error");

}

}

为了验证我尝试了从数据库读取的第一个字段,该字段给出了结果“2”测试数据但员工“2”的图像未加载并崩溃

enter image description here

任何帮助将不胜感激谢谢

错误 enter image description here

错误

enter image description here

最佳答案

ResultSet 中的光标(指向当前行的指针)最初指向第一行之前next()方法返回一个 boolean 值,指示是否还有更多行要移动到,如果有,则移动到下一行。

在您发布的代码中,您从未调用 rs.next() ,因此当您尝试检索 id 和图像时,光标没有指向任何行,并且(我认为)会引发异常。

在您的情况下,由于您正在按看似主键的内容进行搜索,因此您预计会返回零行或一行。所以你需要:

private void handleSearchResults(KeyEvent event) throws IOException {

try {

String sql = "select * from Staff_information where id=? ";
pst = conn.prepareStatement(sql);
pst.setString(1, txtSearch.getText());
rs = pst.executeQuery();

if (rs.next()) {
String add1 = rs.getString("id");
txtid.setText(add1);

byte[] img = rs.getBytes("Image");
BufferedImage bufferedImage = ImageIO.read(img);
WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);

imageDisplay.setImage(image);
imageDisplay.setFitWidth(200);
imageDisplay.setFitHeight(200);
} else {
// no match found.... inform user, etc
}

} catch (Exception e) {
System.out.println("missingdata");
} finally {

try {

rs.close();
pst.close();

} catch (Exception e) {

}
}

}

关于Java FX 在搜索查询中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60526486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com