Search code examples
javaswingnetbeansblobjlabel

MySQL blob to Netbeans JLabel


I have a blob type field in my MySQL, I want to put the data in this field in JLabel as Icon. For example this JLabel will be user's Profile Picture in my form.

I used this codes but nothing happens and also I want to fix to width or fix any image size in my jlabel

DefaultTableModel pic = MyDB.DataTable("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");
     if (pic.getRowCount() > 0){
         Blob blob = pic.getBlob(1);
         byte[] image1 = blob.getBytes(1, ALLBITS);
         ImageIcon image = new ImageIcon(image1);
         picture.setIcon(image);
         getContentPane().add(picture);
         setVisible(true);
     }

picture is the name of my jlabel


Solution

  • I got my filename should be like this

      txtPicPath.setText(file.getAbsoluteFile().toString());
    

    and i used these codes, and also it fits with the jlabel size

     ResultSet rst = MyDB.rsFetch("SELECT `Picture` FROM `photo` WHERE `Employee ID` = '"+ Data.User.getText()+"'");
             while (rst.next()) {
             Blob filenameBlob = rst.getBlob("Picture");
             byte[] content = filenameBlob.getBytes(1L,(int)filenameBlob.length());
             ImageIcon ik = new ImageIcon(content);
             Image img = ik.getImage();
             Image newimg = img.getScaledInstance(Data.picture.getWidth(), Data.picture.getHeight(), java.awt.Image.SCALE_SMOOTH);
             ik = new ImageIcon(newimg);
             Data.picture.setIcon(ik);
             }