Click here to Skip to main content
15,868,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I have been struggling for days now trying to figure out how I can display an image on a jLabel from a blob column in oracle database but all efforts seems not sufficient to produce desired results. The problem is that the Label displays a dark colour stuff like background. I've spent so much time googling and also searching here on the stack overflow but, the available resources aren't putting me through Please help me out ! The variable image is the jLabel I'm using to set the image. And I forgot to mention that while researching I realised that I can do a result2.getBlob(); but my it throws a UnsupportedOperationException(my driver doesn't support that). at the result2.getBlob(); line. So I had to omit it



Java
 private void studentIdItemStateChanged(java.awt.event.ItemEvent evt)          {                                           
        String id = (String)studentId.getSelectedItem();


        try{


             con = DriverManager.getConnection("jdbc:odbc:test", "SYSTEM", "VICTOR");
             stmt = con.createStatement();
             result = stmt.executeQuery("select * from STUDENTDBASE.STUDENT where   Student_Id = '" + id + "' ");
             result = stmt.getResultSet();

             while (result.next()){
                 surname.setText(result.getString("Surname"));
                 firstName.setText(result.getString("First_Name"));
                 otherNames.setText(result.getString("Other_Names"));
                 phoneNumber.setText(result.getString("Mobile_Number"));
                 dob.setText(result.getString("Date_Of_Birth"));
                 gender.setText(result.getString("Gender"));
                 address.setText(result.getString("Address"));
                 state.setText(result.getString("State"));
                 lga.setText(result.getString("Lga"));

                }
             result.close();


             PreparedStatement ps;           
             con = DriverManager.getConnection("jdbc:odbc:test", "SYSTEM", "VICTOR");
             ps = (PreparedStatement) con.prepareStatement("select Image from STUDENTDBASE.STUDENT_Images where STUDENT_ID = '" + id + "' ");
             result2 = ps.executeQuery();

             while (result2.next()){


                InputStream in = result2.getBinaryStream("Image");


               BufferedImage im = ImageIO.read(in); 
               BufferedImage outimage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
               Graphics2D g = outimage.createGraphics();

                float xScale = (float)image.getWidth() / outimage.getWidth();
                float yScale = (float)image.getHeight() / outimage.getHeight();
                AffineTransform at = AffineTransform.getScaleInstance(xScale,yScale);
                g.drawRenderedImage(im,at);
                g.dispose();
                Image scaledImage = outimage.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH);
                ImageIcon icon = new ImageIcon(scaledImage);
                image.setIcon(icon);
                image.revalidate();
                basicDetails.revalidate();
                }
             result2.close();
             con.close();
   }
   catch(SQLException d){
       JOptionPane.showMessageDialog(null, "Error is due to " + d.getMessage(), "Error message from database", JOptionPane.ERROR_MESSAGE);
   }
       catch(IOException io){
       JOptionPane.showMessageDialog(null, "Error is due to " + io.getMessage(), "Error message from database", JOptionPane.ERROR_MESSAGE);
        }







//Insert Student image into database
            url = "jdbc:odbc:test";
            String query1 = "insert into STUDENTDBASE.STUDENT_Images (Student_Id, Image, Image_Path, Image_Size)values('" + counterValue + "', ?, '" + path + "', '" + stringLength + "' ) ";
            con = DriverManager.getConnection(url, "SYSTEM", "VICTOR");
            PreparedStatement ps=con.prepareStatement(query1);
            FileInputStream fin=new FileInputStream(file);
            ps.setString(1, file.getName());
            ps.setBinaryStream(2, fin, file.length());
            ps.executeUpdate();
Posted
Comments
Glek 24-Nov-13 17:35pm    
When you insert the image into the database, are you doing it right? I only see one question mark, but you specify that the image data stream goes in the second placeholder. Also, you may want to consider using all the set features of the PreparedStatement like so:

String query1 = "insert into STUDENTDBASE.STUDENT_Images(Student_Id, Image, Image_Path, Image_Size) values (?, ?, ?, ?)";
PreparedStatement ps = con.prepareStatement(query1);
ps.setString(1, counterValue);
ps.setBinaryStream(2, fin, file.length());
ps.setString(3, path);
ps.setString(4, stringLength);

The PreparedStatement should automatically escape your strings (it always has for me in the past).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900