Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insert image into MySQL database

 
Aravind Prasad
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try inserting it as a BLOB[binary large object]
 
Aravind Prasad
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me ranchers...
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aravind Prasad
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers..
No reply for my doubt.. expecting and waiting for a solution!!

Regards

Aravind Prasad
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Annahita Moshiri
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic