0

I uploaded a couple of pictures in my database and i want to get their path, but i always get something like this: com.mysql.cj.jdbc.Blob@28763983 for every picture. This is the code:

  public static List<Image> selectImages() {
    ResultSet rs = null;
    try {
        imageList.clear();
        String selectImages = "select * from image";
        PreparedStatement ps = DataBaseConnection.get().prepareStatement(selectImages);
        rs = ps.executeQuery(selectImages);
        while (rs.next()) {
            int idImage = rs.getInt("id");
            Blob viewImage = rs.getBlob("image");
            Image image = new Image(idImage, viewImage);
            imageList.add(image);
        }
        return imageList;

How can i get a real path instead of that gibberish?

nemke nemke
  • 191
  • 1
  • 1
  • 9
  • 1
    A blob doesn't have a path, it is a handle to data stored in the database in some way (or possibly a wrapper around a byte array). – Mark Rotteveel Aug 07 '20 at 13:38

1 Answers1

1

That "gibberish" is the default output of Object.toString() from java.lang. Your code snippet doesn't show it, but I assume you are iterating over that list of Images and printing the Blob; something like this

for (image : selectImages()) {
    System.out.println(image.getViewImage());
}

Taking a quick look at the API docs for the mysql jdbc Blob, there doesn't appear to be any immediately available methods for achieving what you want. http://www.docjar.com/docs/api/com/mysql/jdbc/Blob.html

It sounds like you want the output of File.getAbsolutePath()

This article demonstrates how to fetch a Blob from the database as a File in the "readBlob" method; https://www.mysqltutorial.org/mysql-jdbc-blob/

Or alternatively, convert the Blob to a File; https://www.baeldung.com/convert-input-stream-to-a-file

File targetFile = new File("src/main/resources/targetFile.tmp");
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);

What you may notice about both of these approaches, is that the fileName String is an external variable and is not stored within the Blob itself.

So the moral of the story here is that the Blob object is only stored in memory, it isn't written to a file yet.

John Mitchell
  • 455
  • 2
  • 5
  • Hey John, thanks for answering, Yes i want `File.getAbsolutePath()`. I'm having problem understanding what should i put in brackets `File targetFile = new File("");` since i only have `id (int)` and `image(blob)` in database – nemke nemke Aug 07 '20 at 14:49
  • Take a look at this Answer; https://stackoverflow.com/a/50427686/8194026 You effectively choose the file location/name yourself. Like if you wanted to Save an image file on your disc, you navigate to a directory and give the file a name of your choosing. – John Mitchell Aug 07 '20 at 14:50
  • Don't if i'm stupid but i really don't get you. I have already upload images in database. Now i just want to display them in JSP something like this` `. In `File`class brackets should be put some path. Please if you want just type solution for my case, and all stress will disappear :) – nemke nemke Aug 07 '20 at 18:50
  • 1
    The situation is that: you have the image in the database, then you are reading the image into memory. But img src requires a file on disc. Try adding new File("name_" +idImage+ ".png"); inside your while loop, and write the blobs to the files using an OutputStream. Once you run the application, you should hopefully see the image files created on disc in the same directory as your main application. Then you can try or something like that. Sorry I can't write it very well as I'm now on my phone. – John Mitchell Aug 07 '20 at 22:04
  • Hey John Thank you, i did it! Couldn't solve it without your help! – nemke nemke Aug 10 '20 at 19:01