Java FX 图像显示在搜索查询中
Java FX image display on search query
我最近在使用 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”的图像未加载并崩溃
任何帮助将不胜感激
谢谢
错误
错误
ResultSet
中的游标(指向当前行的指针)最初指向 在 第一行之前。 next()
方法 returns 一个布尔值,指示是否有更多行要移动到,如果有则移动到下一行。
在您发布的代码中,您从不调用 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) {
}
}
}
我最近在使用 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”的图像未加载并崩溃
任何帮助将不胜感激 谢谢
错误
错误
ResultSet
中的游标(指向当前行的指针)最初指向 在 第一行之前。 next()
方法 returns 一个布尔值,指示是否有更多行要移动到,如果有则移动到下一行。
在您发布的代码中,您从不调用 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) {
}
}
}