10

I am rewriting my question because I am struggle to find this solution. I've made a simple form to register some basic information about a student you know name, last name and so on. It also has an option to choose a photo by filechooser class to find a picture from the file system.

Let's take a look at this piece of code.

@FXML
public void onBrowserPhoto() throws IOException {
    File file = fileChooser.showOpenDialog(stage);
    if (file != null) {
        is = new FileInputStream(file);
        Image image = new Image(is);
        imageView.setImage(image);
    }

}

At this point when the user clicks the button they will choose a picture then I created a new fileInputStream and reference that is variable which is an inputream object.

private void businessOnSave() {
        if (currentStudent.getId() == 0) {
            try {
                currentStudent.setIs(is);
                currentStudent
                        .setGenger(buttonMale.isSelected() == true ? buttonMale.getText() : buttonFemale.getText());
                if (!checkStudent(currentStudent)) {
                    service.saveStudent(currentStudent);
                    studentUnBind(currentStudent);
                    sweep();
                    listView.getItems().add(currentStudent.getNickname());
                    buttonNew.disableProperty().setValue(false);
                    is.close();
                } else {
                    return;
                }

            } catch (ServiceException | IOException e) {
                e.printStackTrace();
            }
        }

When the use clicks on the save buttom this method will be called and the currentStudent variable receives that inputream (is) next it will be doing some stuff then I save it with service object. Everithing is perfect the student obj is saved.

Here is the method which save a new student

@Override
public void save(Student t) throws SQLException {
    try (PreparedStatement ps = con.prepareStatement(INSERT)) {
        ps.setString(1, t.getNickname());
        ps.setString(2, t.getLastName());
        ps.setString(3, t.getEmail());
        ps.setString(4, t.getPhone());
        ps.setString(5, t.getGenger());
        ps.setDate(6, Date.valueOf(t.getBirthDate()));
        ps.setBinaryStream(7, t.getIs());
        ps.executeUpdate();
    }
}

As you can see I am passing that inpustream variable to a preparedStatement object and it is ok, I confirm in the database the data is there. but when I bring this record from database it doesn't show the image on the UI.

here is the method that return a student record from that base.

public Student getStudentByName(String name) throws SQLException {
    Student student = new Student();
    try (PreparedStatement ps = con.prepareStatement(GET_STUDENT_BY_NAME)) {
        ps.setString(1, name);
        try (ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                student.setId(rs.getInt("student_id"));
                student.setNickname(rs.getString("nickname"));
                student.setLastName(rs.getString("lastname"));
                student.setEmail(rs.getString("email"));
                student.setPhone(rs.getString("phone"));
                student.setGenger(rs.getString("genger"));
                student.setBirthDate(rs.getDate("birthdate").toLocalDate());                    

                try(InputStream is = rs.getBinaryStream("photo")){

                    BufferedImage bg = ImageIO.read(is);

                    if(bg != null){
                        Image image = SwingFXUtils.toFXImage(bg, null);
                        student.setPhoto(image);
                    }                       

                } catch (IOException e) {
                    e.printStackTrace();                    }

            }

        }

    }

    return student;
}

The bg objec is null and i have no clue why because on database the image bytes is there. I have made another way to get the bytes then take care of where the student objet is created but it didn't work as well. If someone know a solution to this problem i will be happy. Thank you for taking the time to reading this question.

yfabio
  • 144
  • 1
  • 2
  • 8
  • 1
    Depends on how the image is encoded... – fabian May 23 '17 at 16:33
  • Sorry! But I didn't get it. I am a beginer at Java. Could you show me a piece of code please. – yfabio May 23 '17 at 16:51
  • 2
    As indicated in the previous comment, no-one can tell you how to do this without knowing how the image is encoded in the binary data you get from the result set. And "it doesn't work" is simply not a problem statement. – James_D May 23 '17 at 17:04
  • It looks like you are getting an `InputStream` from a file, reading from it to create and image, and then storing a reference to that (now consumed) input stream in the `Student` object? That's just not how streams work: once you have read from it, it is essentially useless. You should store the image in the `Student` (looks like you are already doing that), and then when you write to the database read the data from the image. – James_D May 27 '17 at 15:46

1 Answers1

24

To draw image, first convert a byte array into Image using the following code:

Image img = new Image(new ByteArrayInputStream(buffer));

Then use the drawImage method of graphicsContext:

graphicsContext.drawImage(img, x, y, width, heighth);

So my suggestion is not to store Image in the student object.

You can use byte array property and when you are displaying student record convert into Image as above.

shim
  • 9,289
  • 12
  • 69
  • 108
Prakash
  • 359
  • 1
  • 4
  • 1
    Please learn how to use correct code markup. (https://stackoverflow.com/editing-help#code) You're doing this wrong in ALL of your answers. – fabian May 23 '17 at 18:38