0

I want to store images into MySQL databases using a Java applet or at run time user will load the images.

How can I do this?

Please help me out. Is it required to have any special Java-based MySQL or will MySQL simplyl do this thing of loading BLOB image content? If possible, give me code example and mention how I should load MySQL onto my computer for this purpose.

After doing this I want to display that images from mysql onto webpage.

Is that really possible?

How to do it?

4
  • Here is some sample code:coderanch.com/t/355553/JDBC/java/… Commented Dec 29, 2011 at 4:06
  • 3
    This question shows pretty much no research or self effort. Commented Dec 29, 2011 at 4:10
  • You want us to tell you how to install MySQL on top of everything else? That's pretty absurd. Commented Dec 29, 2011 at 4:11
  • @Josh - I know how to load simple mysql but don't know whether it will work for blob object. Is there any java specific mysql or simple mysql will work? Commented Dec 29, 2011 at 5:48

4 Answers 4

6

You should not store images in MySQL. MySQL is not designed to store BLOBs and will take severe performance hits if you do so.

You can and should store pointers to images, either on your local fileserver or a remote server (like Amazon S3).

If you really want to do this despite my warnings, you can find some sample code here, though the self-same warnings are discussed thoroughly there.

Sign up to request clarification or add additional context in comments.

4 Comments

That comments about restrictions and BLOBs in coderanch are up to 2002. Does that BLOB problem stand in MySQL? I ask because I already worked with an app bringing binaries, setting them in a temp folder and displaying those images with no particular problem in the expected performance. I think it depends on concurrence, expected database growth and so
@Alfabravo The performance issues still remain. Regardless, it would constitute poor architecture. As the system scales, you'd probably like to do some sort of dynamic resizing and loading, going to your business logic for looking up images and not the database itself. A consistent image-naming schema would be enough to have your image presentation be a mixture of business logic and an HTTP request. That's vastly preferable to storing BLOBs in a DB.
I tend to agree after some odd experiences with images submitted thru oracle forms to oracle DB being transformed to bitmaps, increasing their size by 10x. A secure-and-properly-configured filesystem is always better than some emulation in a DBMS. Just wanted to know about this certain case :)
You could also do both, i e store the image in the database as a blob, but routinely write them out once stored to image file directories and store the path. When you request the image, you serve the image on disc from the path. In your database you have a backup of each image and these are saved trough normal db backup. They can also be restored quickly at a new server to a different path, if needed. Of course, you'd have to make sure the image is not mangled when stored. In my experience, it's not.
0

you can use setBinaryStream() in java to store images to blob data type column of mysql

File image = new File("C:/image.jpg");
PrepareStatement psmnt = connection.prepareStatement
("insert into save_image(image) "+ "values(?)");

FileInputStream fis = new FileInputStream(image);
psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length()));
/* executeUpdate() method execute specified sql query. Here this query 
insert data and image  */ 
int s = psmnt.executeUpdate();

See below tutorial How to insert Image in MySQL using Java Program

Comments

0

As Josh Smith suggested you should not store images using a BLOB or a LONGBLOB in MySQL. Because it effects drastically on you performance you can do it by following this method. Instead of storing images directly into database, store the image location in the database. As we compare both options, storing images in the database is safe for security purpose. Disadvantage are

  1. If database is corrupted, no way to retrieve.
  2. Retrieving image files from db is slow when compare to other option.

Storing image file location in db will have following advantages.

  1. It is easy to retrieve.
  2. If more than one images are stored, we can easily retrieve image information.

Comments

0
try {
            File image = new File("C:/honda.jpg");
            inputStream = new FileInputStream(image);
            connection = getConnection();
            statement = connection
                    .prepareStatement("insert into trn_imgs(img_title, img_data) "
                            + "values(?,?)");
            statement.setString(1, "Honda Car");
            statement.setBinaryStream(2, (InputStream) inputStream,
                    (int) (image.length()));

            statement.executeUpdate();
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException: - " + e);
        } catch (SQLException e) {
            System.out.println("SQLException: - " + e);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.