Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Carey Brown
  • Frits Walraven

Retrieving pictures from MySQL using Java

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have a question related to retrieving files from mysql using JDBC, I managed to save a .png file from my desktop & retrieve it successfully, but I can't read the file, it shows that the format is unsupported (eventhoug I can open the original file without any issue).

Here's the code:

Any ideas what could be the cause and how to fix it?
Thank you in advance!
Posts: 7056
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble understanding exactly what's happening.  You have a PNG file in your mySQL DB.  You can retrieve the Clob in the DB in Java.  You can write the PNG file back to the disk in Java.  But when you try to open the picture with some outside program, it says the PNG file is the wrong format?  Is that correct?
Posts: 25452
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say the problem is here:

Images are stored in a binary format, so converting the bytes to characters and then back to bytes is going to make a mess, since images aren't meant to represent text.

You should be using Blob and InputStream and getBinaryStream. And FileOutputStream.

And you didn't show the code which you used to write the image into the database; perhaps it has the same problem?
Saloon Keeper
Posts: 21975
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Expanding on Knute's answer. A "clob" is a Character large object. Images are not characters (unless you count ASCII art). An image should be a blob - Binary Large Object.
Bras cause cancer. And tiny ads:
Two software engineers solve most of the world's problems in one K&R sized book
    Bookmark Topic Watch Topic
  • New Topic