Hi Friend,
Try the following code:
import java.sql.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class RetrieveImage extends JFrame{
JLabel l1,l2,l3;
JPanel p;
RetrieveImage(){
p=new JPanel(new GridLayout(3,2));
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:
mysql://localhost:3306/register";, "root", "root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select image1,image2,image3 from person where id='15'");
byte[] bytes1=new byte[1024];
byte[] bytes2=new byte[1024];
byte[] bytes3=new byte[1024];
if (rs.next()) {
bytes1 = rs.getBytes("image1");
bytes2 = rs.getBytes("image2");
bytes3 = rs.getBytes("image3");
}
rs.close();
stmt.close();
con.close();
if (bytes1 != null) {
File f=new File("img1.jpg");
InputStream in = new ByteArrayInputStream(bytes1);
BufferedImage image = ImageIO.read(in);
ImageIO.write(image, "jpg", f);
String path=f.getName();
ImageIcon icon = createImageIcon(path,"");
l1 = new JLabel(icon);
}
if (bytes2 != null) {
File f=new File("img2.jpg");
InputStream in = new ByteArrayInputStream(bytes2);
BufferedImage image = ImageIO.read(in);
ImageIO.write(image, "jpg", f);
String path=f.getName();
ImageIcon icon = createImageIcon(path,"");
l2 = new JLabel(icon);
}
if (bytes3 != null) {
File f=new File("img3.jpg");
InputStream in = new ByteArrayInputStream(bytes3);
BufferedImage image = ImageIO.read(in);
ImageIO.write(image, "jpg", f);
String path=f.getName();
ImageIcon icon = createImageIcon(path,"");
l3 = new JLabel(icon);
}
} catch (Exception e) {
}
p.add(l1);
p.add(l2);
p.add(l3);
add(p);
setVisible(true);
pack();
}
public static void main(String[]args){
RetrieveImage data=new RetrieveImage();
}
protected static ImageIcon createImageIcon(String path,String description) {
java.net.URL imgURL = RetrieveImage.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
For the above code,we have used following database table:
CREATE TABLE `person` (
`id` bigint(255) NOT NULL auto_increment,
`image1` longblob,
`image2` longblob,
`image3` longblob,
`contactNo` int(255) default NULL,
PRIMARY KEY (`id`)
)
Thanks