0

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

5
  • 3
    Debug each row in the code. Check how many rows you get, check whether 'blob' is null, check whether retrieved bytes are ok, check whether 'image' is created (not null and width/height >0). Commented Mar 15, 2013 at 8:03
  • my if (pic.getRow() == 1) equals to 0 Commented Mar 18, 2013 at 1:10
  • but my SQL statement is correct @StanislavL Commented Mar 18, 2013 at 2:19
  • yes @StanislavL now its ok but still i wont display Commented Mar 20, 2013 at 3:51
  • @Martijn Courteaux need help Commented Mar 20, 2013 at 4:08

5 Answers 5

6
+25

First : Return the Input stream from your database :

String query = "SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'";
stmt = (PreparedStatement) con.prepareStatement(query);
ResultSet result = stmt.executeQuery();

Returned image from Database

BufferedImage im = ImageIO.read(result.getBinaryStream(1));

Then make rezise to this image :

im =linearResizeBi(im, /*width*/, /*height*/);

linearResizeBi Method :

static public BufferedImage linearResizeBi(BufferedImage origin, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height ,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        float xScale = (float)width / origin.getWidth();
        float yScale = (float)height / origin.getHeight();
        AffineTransform at = AffineTransform.getScaleInstance(xScale,yScale);
        g.drawRenderedImage(origin,at);
        g.dispose();
        return resizedImage;
    }

then make the image is an Icon:

ImageIcon image1 = new ImageIcon(im);

then add the Icon to The Jlabel :

picture.setIcon(image);
getContentPane().add(picture);
setVisible(true);
Sign up to request clarification or add additional context in comments.

Comments

1

Use a resultset

 Statement stmt = con.createStatement();
 ResultSet rs = stmt.executeQuery("SELECT `Picture` FROM `photo` WHERE `Employee ID` = 'EQ0103'");

You may change from

Blob blob = rs.getBlob(1);

to another altenative of

InputStream binaryStream = rs.getBinaryStream(1);

You can refer to the official guide of getting image from a blog here http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/blob.html

1 Comment

no I must not use resultset cause I tried it and I always have user connection error cause I have here 100+ computers
1

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

Comments

0

Blob has a getBinaryStream() which returns a stream of bytes containing the data stored in the blob.

ImageIcon, which implements Icon, has a constructor which takes a byte array as argument.

JLabel has a setIcon(Icon) method.

label.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));

Comments

0

Try:

picture.setIcon(new ImageIcon(ByteStreams.toByteArray(blob.getBinaryStream())));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.