2

I have a table in SQL 2008 Enterprise named Student_Images where the S_Images column name's data type is image. Now how can i insert the image into database. I searched on google and visited more than 50 sites some says convert it to Base64 and some say convert it to byte etc but if a column type is image then what. Please guide me.

Yaroslav
  • 6,476
  • 10
  • 48
  • 89
Huzaifa Arain
  • 79
  • 1
  • 8
  • 4
    While this doesn't answer your question, I would recommend saving your images in some predictable manner and simply storing a filename or path in the database. Your database will be *vastly* smaller, but just as functional. When you need an image, retrieve its location from the database (by whatever primary key) and fetch the image in your Java code – Nick Aug 02 '12 at 13:59
  • Please use capital letters normally, it's hard to tell sentences apart otherwise. – millimoose Aug 02 '12 at 14:05

4 Answers4

2

Please don't do this. Databases are not file systems. Create a column that holds the filename and store the image in a folder.

Matt Brock
  • 5,337
  • 1
  • 27
  • 26
1

You should not store images on the database. As pointed out on the first comment, it will take lot of space. Storing path and filename is more recommended.

I guess that maybe you did not searched the correct way as right here in stackoverflow you have a great answer about the subject.

One more thing. Your question was all capitalized, avoid that, is hard to read and therefore you will have less answers.

Community
  • 1
  • 1
Yaroslav
  • 6,476
  • 10
  • 48
  • 89
1

Look at the JDBC API Javadocs. You have to create a Blob object, fill in the data from the file and just use it as column for a (Prepared)Statement. Works just like any other data type, just a few more lines of code to create the Blob.

Here's a link to the Tutorials how to get started.

Unlike the other answers suggest, there are scenarios where storing binary data in the DB is just the best fit for the requirements. Storing just a reference to a file system can create heavy headaches with backups and also reachability problems (just because you can connect to a DB does not mean you have access to the file system where the file lives).

Durandal
  • 19,919
  • 4
  • 36
  • 70
0

Yes.

  • If image size is big then it’s good to create meta data of image save its reference into database and real image save on disc. This is way faster than saving image in database.
  • Saving image in database is good when your image size is small

If you want to test it to see how this works. Take any type of image and convert it to byte array stream as below:

BufferedImage originalImage = ImageIO.read(new File(
        "resources/image/Test.png"));

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
  • Create blob column to save data in the table
  • Execute insert statement to insert data into the table.
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
user3468976
  • 629
  • 7
  • 4