Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库检索图像粘贴到JLabel上_Java_Image_Swing_Jlabel - Fatal编程技术网

Java 从数据库检索图像粘贴到JLabel上

Java 从数据库检索图像粘贴到JLabel上,java,image,swing,jlabel,Java,Image,Swing,Jlabel,大家好,我在JLabel上显示图像时遇到问题。我从数据库中检索图像,第一个图像成功显示,但后续图像无法显示。我正在设计一个基于用户登录检索配置文件信息的系统。如果ID为1的第一个用户登录,图像和其他信息将相应显示,ID为2、3等的后续用户不会显示图像。我已经尝试搜索其他主题,以查看是否可以找到可能错误的答案,但我无法找到,这就是我现在发布此消息的原因。请看下面我的部分代码,告诉我我做错了什么,谢谢你的建议!欢迎任何批评 class UserProfile { private Image

大家好,我在JLabel上显示图像时遇到问题。我从数据库中检索图像,第一个图像成功显示,但后续图像无法显示。我正在设计一个基于用户登录检索配置文件信息的系统。如果ID为1的第一个用户登录,图像和其他信息将相应显示,ID为2、3等的后续用户不会显示图像。我已经尝试搜索其他主题,以查看是否可以找到可能错误的答案,但我无法找到,这就是我现在发布此消息的原因。请看下面我的部分代码,告诉我我做错了什么,谢谢你的建议!欢迎任何批评

class UserProfile {
    private Image img;
    private ImageIcon icon;

    public void profile(int id, String user, String pwd) {
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Exception: " + e.getMessage());
            }
            con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/phoneshopsystem",
                    dbLogin.dbUser(), dbLogin.dbPwd());
            String query = "SELECT * FROM employee, employeeImages"
                    + " WHERE empLoginID = ?"
                    + " AND empLoginUsername = ?"
                    + " AND empLoginPwd = ?"
                    + " AND empLoginID = imgID";

            pst = con.prepareStatement(query);
            pst.setInt(1, id);
            pst.setString(2, user);
            pst.setString(3, pwd);
            rset = pst.executeQuery();

            while (rset.next()) {
                idField.setText(rset.getString(1).toString());
                usnField.setText(rset.getString(2));
                fnField.setText(rset.getString(4));
                mnField.setText(rset.getString(5));
                lnField.setText(rset.getString(6));
                sxField.setText(rset.getString(7));
                /**
                 *
                 */
                image = rset.getBytes("img");
                img = Toolkit.getDefaultToolkit().createImage(image);
                icon = new ImageIcon(img);
                photoLabel.setText(null);
                photoLabel.setIcon(icon);
            }
        } catch (SQLException sql) {
            JOptionPane.showMessageDialog(null, "Sql Exception@: " + sql.getMessage());
        } finally {
            try {
                if (pst != null | rset != null | con != null) {
                    pst.close();
                    rset.close();
                    con.close();
                }
            } catch (Exception e) {
                e.getMessage();
            }
        }
    }

“谢谢你的建议!”建议:问一个问题。我建议减少你的方法大小,使它更具可读性。我认为这段代码没有问题。也许是你使用它的方式。。。你是否仔细检查了外部因素。。。像存储的图像格式不受支持。。。“AND empLoginID=imgID”子句实际上是有效的。。。我建议您在“photoLabel.setIcon(icon);”上添加一个断点并检查图标是否已实际设置。