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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4825809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:13:02  来源:igfitidea点击:

how to display image retrieved from mysql on JFrame

javaswing

提问by Nidhin

I am facing a problem in displaying image that retrieved from database on JFrame. Here is the that i will use, .........

我在显示从 JFrame 上的数据库中检索的图像时遇到问题。这是我将使用的,........

     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); 
  }
}


This code has no problem.but image not displayed on frame... Please help me for solving this problem....

此代码没有问题。但图像未显示在框架上...请帮助我解决此问题....

回答by Noor Mohammad Atapoor

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();
}
}

回答by Parkhya Solutions

 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));

回答by Dhruv Gairola

try adding this.setVisible(true)to your code after

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

this.add(lPhoto) ;

this.add(lPhoto) ;

回答by Jigar Joshi

1 Retrieve byte[]from DB as you did in your code then

1byte[]像在代码中那样从数据库中 检索

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

2 Represent that image on JFrame

2 在 JFrame 上表示该图像

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

Also See

另见

回答by camickr

We don't know the context of how this code is used.

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

Generally after adding/removing a component from a GUI you need to use:

通常在从 GUI 添加/删除组件后,您需要使用:

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