如何在JFrame上显示从mysql检索的图像

问题描述:

我在显示从JFrame上的数据库检索的图像时遇到了问题。这里是我会使用, .........如何在JFrame上显示从mysql检索的图像

 try 
       { 
       Class.forName("com.mysql.jdbc.Driver"); 
      Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/studio","root",""); 
      Statement st=con.createStatement(); 
      ResultSet rs = st.executeQuery("select image from photo_instn where cust_id='2'") ; 
      while(rs.next()) 
      { 
      byte[] imagedata = rs.getBytes("image") ; 
      Image img = Toolkit.getDefaultToolkit().createImage(imagedata); 
      ImageIcon icon =new ImageIcon(img); 
      JLabel lPhoto = new JLabel(icon) ; 
      setLayout(null);        // BYTES TO IMAGE                  
      System.out.println("Inside"); 
      System.out.println(lPhoto); 
      this.add(lPhoto) ; 
      lPhoto.setBounds(200,20,300,400); 
    } 
} 

这些代码不会显示在框架没有问题。但是图像... 请帮我解决这个问题....

+0

请使用代码格式化。 – Mudassir 2011-01-28 07:27:05

尝试加入this.setVisible(true)到您的代码后

this.add(lPhoto);

1从数据库中检索byte[],你在你的代码做了那么

InputStream in = new ByteArrayInputStream(imageInByte); 
BufferedImage bImageFromConvert = ImageIO.read(in); 

2代表了JFrame的

boolean Graphics.drawImage(Image img, 
          int x, int y, 
          ImageObserver observer); 

该图像请参见

我们不知道如何使用此代码的上下文。

添加/从GUI卸下组件后,通常你需要使用:

panel.revalidate(); 
panel.repaint(); 

import java.awt.*; 
import java.sql.*; 

import javax.swing.*; 

public class DisplayImage extends JFrame { 

Connection connection = null; 
PreparedStatement statement = null; 

ResultSet result; 

public DisplayImage() { 
    super("Image Display"); 
    setSize(600, 600); 
    connection = getConnection(); 
    try { // table name:image and second image is field name 
     statement = connection 
       .prepareStatement("select image from image where id = 1"); 
     result = statement.executeQuery(); 

     byte[] image = null; 
     while (result.next()) { 
      image = result.getBytes("image"); 

     } 
     Image img = Toolkit.getDefaultToolkit().createImage(image); 
     ImageIcon icon = new ImageIcon(img); 
     JLabel lPhoto = new JLabel(); 
     lPhoto.setIcon(icon); 
     add(lPhoto); 

    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    setVisible(true); 
} 

public Connection getConnection() { 
    Connection connection = null; 

    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     connection = DriverManager.getConnection(
       // user name:root and password:blank 
       "jdbc:mysql://localhost:3306/insertRetriveImages", "root", 
       ""); 

    } catch (Exception e) { 
     System.out.println("Error Occured While Getting the Connection: - " 
       + e); 
    } 
    return connection; 
} 

public static void main(String[] args) { 
    new DisplayImage(); 
} 
} 

Class.forName("com.mysql.jdbc.Driver"); 
      Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/vodafonecare","root","root"); 
      PreparedStatement ps=con.prepareStatement("select * from photo"); 
      ResultSet rs=ps.executeQuery(); 
      if(rs.next()){ 
       rs.next(); 
       rs.next(); 
      Blob b=rs.getBlob(3); 
      byte bt[]=b.getBytes(1,(int)b.length()); 
      Image img = Toolkit.getDefaultToolkit().createImage(bt); 
      photoL.setIcon(new ImageIcon(img));