简体   繁体   English

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

[英]Java FX image display on search query

i was recently working on image upload using blob images and it works but now when i try to search for the specific person in the search it is not showing up i was trying to convert blob image to byte and display in image field in java fx form我最近正在使用 blob 图像进行图像上传,它可以工作,但是现在当我尝试在搜索中搜索特定人员时,它没有显示我正在尝试将 blob 图像转换为字节并以 java fx 形式显示在图像字段中

this is my code这是我的代码

 @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) {

            }
        }

    }

This is my code for file upload which works这是我的文件上传代码

File Upload上传文件

 @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");

        }

    }

to verify i tried my first field that is reading from database which gives a result "2" test data but image for the employee "2" is not loading and crashing为了验证我尝试了从数据库读取的第一个字段,该字段给出了结果“2”测试数据,但员工“2”的图像没有加载和崩溃

在此处输入图片说明

any help will be appreciated thank you任何帮助将不胜感激谢谢

Error错误在此处输入图片说明

Error错误

在此处输入图片说明

The cursor (pointer to the current row) in a ResultSet initially points before the first row. ResultSet的游标(指向当前行的指针)最初指向第一行之前 The next() method returns a boolean indicating if there are more rows to move to, and moves to the next row if there is one. next()方法返回一个布尔值,指示是否有更多行要移动到,如果有则移动到下一行。

In the code you posted, you never call rs.next() , so when you try to retrieve the id and the image, the cursor is not pointing to any row, and (I think) an exception is thrown.在您发布的代码中,您从不调用rs.next() ,因此当您尝试检索 id 和图像时,光标未指向任何行,并且(我认为)会引发异常。

In your case, since you're searching by what appears to be a primary key, you are expecting zero or one rows to be returned.在您的情况下,由于您正在搜索似乎是主键的内容,因此您希望返回零或一行。 So you need:所以你需要:

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) {

        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM