aspose file tools*
The moose likes JDBC and the fly likes Insert image into MySQL database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Insert image into MySQL database" Watch "Insert image into MySQL database" New topic
Author

Insert image into MySQL database

Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 265
Dear Ranchers,
I'm doing a HR System in JSP and MySQL. In some point i want to insert the employee photograph in to the database and also want to retrieve it back and display it in the jsp page.

I tried looking into the old POSTs and also in google search. I failed to get a solution for this. I will be thankful, if you could send me a sample code for inserting image into the database and also to retrieve it. I will be very thankful..

Regards

Aravind Prasad
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
try inserting it as a BLOB[binary large object]


- Christian
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 265
Dear Ranchers,
I got the code for inserting and retrieving image to and from the database. I would like to share the code and also have a small doubt..

Inserting image to database


Retrieving from the database to JSP Page



Also I have a small doubt, When I retrieve the image, I would like to give it a standard height and width, so that it can fit in a table data to make the HR System good looking..

How can I put the height and width in this code..

please help me with your enriched ideas.

Thanks/Regards

Aravind Prasad
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42935
    
  68
When I retrieve the image, I would like to give it a standard height and width, so that it can fit in a table data to make the HR System good looking..

How can I put the height and width in this code..[/QB]


The ImageInfo class can determine width and height of a number of image formats.
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 265
Dear Ranchers,
I've tried converting the code for inserting image into the database in JSP and tried running it as localhost. It is working fine there. But when i went to another computer and used tried to access the same page, it is giving me error. The code is as follows

put_pic.jsp


putpic.jsp



Ranchers, this code is working with http://localhost:8080/put_pic.jsp
But when i try http://192.168.0.10:8080/put_pic.jsp It is showing me an error


Error I'm getting


Please help me ranchers to solve this

Regards

Aravind Prasad
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 265
Please help me ranchers...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

When an image has not been loaded yet it returns -1 for both width and height. This normally does not happen with ImageIcons, so probably the file is not valid. Possibly the file is incorrect, you'll probably have to convert it to something the remote file system understands.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 265
Thanks for the reply.
Can you please find for me a solution.
I too know the same actully.

Regards

Aravind Prasad
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 265
Ranchers..
No reply for my doubt.. expecting and waiting for a solution!!

Regards

Aravind Prasad
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
Originally posted by Aravind Prasad:
Ranchers..
No reply for my doubt.. expecting and waiting for a solution!!

Regards

Aravind Prasad


Maybe if you worked on it on your own rather than expecting the code to be given to you you'd move faster towards your goal, all your posts seem to be asking for ready made solutions.

Thanks for the reply.
Can you please find for me a solution.
I too know the same actully.

Regards

Aravind Prasad


Rob Prime already hinted at what the problem is and judging from your reply you already know what the problem is right? When you're running it on localhost, the server and the client from where you're uploading your image are the same machine so when the server looks for "c:\imageName.jpg" it finds it.

But when you access the app from a remote machine, "c:\remoteImageName.jpg" doesn't exist on the server, it's on the client. And the server doesn't have access to the client's filesystem. So it fails to locate and insert the image into the DB.

So, now that you know the problem, what could be a solution? You need to get the image onto the server's file system. You could look into uploading the image onto the server rather than simply using the filename to access the file; that would take care of this problem.
[ September 24, 2007: Message edited by: Tarun Yadav ]
Annahita Moshiri
Greenhorn

Joined: May 22, 2008
Posts: 9
Hi everybody

I tried to use this method to upload an image and store it into my db:



but I get the following error form db:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? )' at line 1

Why am I getting this?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31076
    
233

Annahita,
Is herbariumNo a number or a string? The SQL statement implies it is a string, but it sounds like a number. If it is a number the quotes around it would cause an error in your SQL statement.

Also, is there any reason you aren't using a "?" for the number as well as the BLOB?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Annahita Moshiri
Greenhorn

Joined: May 22, 2008
Posts: 9
Originally posted by Jeanne Boyarsky:
Annahita,
Is herbariumNo a number or a string? The SQL statement implies it is a string, but it sounds like a number. If it is a number the quotes around it would cause an error in your SQL statement.

Also, is there any reason you aren't using a "?" for the number as well as the BLOB?


Hi

herbariumNo is a string. Anyway my problem is solved with a friend's help:

I should have written:
result = st.executeUpdate();

not:
result = st.executeUpdate(query);

Thank you Jeane.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insert image into MySQL database