Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-28-2006, 10:24 AM
|
#1
|
|
Member
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167
Rep:
|
Storing Images in MySQL
Hi Guys!
I wanted to Know any MySQL command that I can use to insert an image file as it is in a certain field in a database. I've heard that the blob field can store images but i don't know how to add the images and to retrieve them! All the codes I've seen up to now are in PHP which I don't understand!! I am using Java to serve as platform to view and save the images!. Any help would be greatly appreciated  !
|
|
|
|
02-28-2006, 02:05 PM
|
#2
|
|
Member
Registered: Dec 2005
Distribution: Fedora 4
Posts: 89
Rep:
|
the best way is just to store the location of the file in a varchar or text or something like that, and then just retrieve it with whatever language you are using
|
|
|
|
02-28-2006, 02:14 PM
|
#3
|
|
Member
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167
Original Poster
Rep:
|
Yeah I know about that but thats not possible with the app am just doing. In this app, there are multiple computers each having a database for them. The data for each computer are then centralised to a server in another place and then redistributed. Its a sort of Distributed System. This the method of keeping path that process becomes very lenthly and I think that saving the images in the database itself is the best way! So if anyone could tell me how that can be done it would be just great! I know it can be done as I've seen it being done and I've also seen it in the MySQL Documentation... Its just that I Don't know how to do that!! Help Please!!!!
|
|
|
|
02-28-2006, 11:42 PM
|
#4
|
|
Member
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167
Original Poster
Rep:
|
Haven't anyone tried to store an image in the MySQL??? A MySQL Command would do!
|
|
|
|
03-01-2006, 12:46 AM
|
#5
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
For whatever it's worth, we store images - and we do the same thing airswit recommended. Here's an example:
Code:
mysql> describe images;
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| image_id | int(10) unsigned | | PRI | NULL | auto_increment |
| image_location | varchar(50) | YES | | NULL | |
| image_alt | varchar(255) | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
|
|
|
|
03-01-2006, 01:10 AM
|
#6
|
|
Member
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167
Original Poster
Rep:
|
I know about the saving of location! but my problem is that i want the image in the database itself!
|
|
|
|
03-01-2006, 01:54 AM
|
#7
|
|
LQ Newbie
Registered: Feb 2006
Location: Bucharest, Romania
Distribution: OpenSUSE 10
Posts: 10
Rep:
|
Last edited by k^7; 03-01-2006 at 02:01 AM.
|
|
|
|
03-01-2006, 04:11 AM
|
#8
|
|
Member
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167
Original Poster
Rep:
|
Thanks! I found out how to save the images! But now i am totally lost at displaying thos saved images in the JFrame. How can i do that?
|
|
|
|
03-01-2006, 04:29 AM
|
#9
|
|
LQ Newbie
Registered: Feb 2006
Location: Bucharest, Romania
Distribution: OpenSUSE 10
Posts: 10
Rep:
|
After searching a bit with google, here is what i found:
Link 1
Link 2
Link 2 is connected to google cache so you can see the article without registering on that site.
|
|
|
|
03-01-2006, 05:19 AM
|
#10
|
|
Member
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167
Original Poster
Rep:
|
Thanks! But these are a bit complicated. I've finally done it! Its a Simple Code:
Code:
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
public class IMAGETEST extends JFrame
{
public IMAGETEST()
{
try
{
//Put your connection details here
//Displaying the Picture
String query = "select Image from testdb.images where ID ='10'";
ResultSet rs = Connection.createStatement.executeQuery(query);
rs.next();
byte[] bytearray = new byte[4096];
int size=0;
InputStream sImage;
sImage = rs.getBinaryStream(1);
Image image = null;
try {
image = ImageIO.read(sImage);
} catch (IOException e) {System.out.println(e);}
JLabel label = new JLabel(new ImageIcon(image));
getContentPane().add(label, BorderLayout.CENTER);
pack();
setVisible(true);
sImage.close();
//Updating the picture with a new one
PreparedStatement stmt = Connection.prepareStatement("update testdb.images set image = ? where id = 10");
File f = new File("08.jpg");
FileInputStream fis = new FileInputStream(f);
stmt.setBinaryStream(1,fis,(int)f.length());
System.out.println("photo length = " + f.length());
System.out.println("no of rows effected "+stmt.executeUpdate());
}catch(Exception ex){System.out.println(ex);}
}
public static void main(String args[])
{
IMAGETEST im = new IMAGETEST();
}
}
|
|
|
|
03-01-2006, 05:23 AM
|
#11
|
|
LQ Newbie
Registered: Feb 2006
Location: Bucharest, Romania
Distribution: OpenSUSE 10
Posts: 10
Rep:
|
You're welcome!
PS: You should put the code for inserting a picture in mysql, just as a future reference.
|
|
|
|
03-01-2006, 05:49 AM
|
#12
|
|
Member
Registered: Nov 2005
Location: Mauritius
Distribution: PCQLinux, SUSE Linux,Fedora Core 5, Fedora Core 6, Knoppix Live, Kubuntu Edgy, PCLinuxOS
Posts: 167
Original Poster
Rep:
|
for inserting you just have to replace the paragraph for updating th picture with this:
PreparedStatement stmt = Connection.prepareStatement("insert into testdb.images values(2,?)");
File f = new File("pic.jpg");
FileInputStream fis = new FileInputStream(f);
stmt.setBinaryStream(2,fis,(int)f.length());
System.out.println("photo length = " + f.length());
System.out.println("no of rows effected "+stmt.executeUpdate());
//the 2 in values is the key field value and the ? represent the image
|
|
|
|
03-01-2006, 10:41 AM
|
#13
|
|
Member
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601
Rep:
|
paulsm4, is that image_location value a URL or a fully-qualified pathname like image_user@image_server:/data/images ?
|
|
|
|
03-01-2006, 11:06 AM
|
#14
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
johnMG -
It's whatever you want it to be. In our case, we're:
a) The script is in PHP
b) It generates a random number between 1 and [max image id]
c) It does a PHP/mySql "select image_location,alt_image where image_id=id;"
d) It writes out "<img src=\"$image_location\" alt=\"$alt_image />
So in our case, it's a path relative to the web root.
|
|
|
|
03-01-2006, 11:13 AM
|
#15
|
|
Member
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601
Rep:
|
Thanks Paul. 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:24 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|