Here is my code, and i would like to read images from my mysql database and display it over a JButton using the BufferedImage casting into Image.

Everytime i want it to display, it would fail to read the data and run to the Exception, may i please have help on it

import java.sql.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.*;

public class PictureRetriver extends JFrame
{
    Container cp = getContentPane();

    public PictureRetriver()
    {
        try
        {
            Connection con = DriverManager.getConnection("Jdbc:Odbc:albusiness","root","test");

            Statement stmnt = con.createStatement();

                JOptionPane.showMessageDialog(null, "connected to Database");

            ResultSet rs = stmnt.executeQuery("Select * from sellertable where SellerUsername='Eddy'");

            while(rs.next())
            {
                try
                {
                    JOptionPane.showMessageDialog(null, "Inside the table");

                    // Get as a BLOB
                    String theName = rs.getString("SellerUsername");

                    JOptionPane.showMessageDialog(null, "the name "+theName);

                    Blob aBlob = rs.getBlob("SellerLogo");
                    byte[] imageByte = aBlob.getBytes(1, (int) aBlob.length());
                    JOptionPane.showMessageDialog(null, "connected");

                    InputStream is=new ByteArrayInputStream(imageByte);
                    BufferedImage imag=ImageIO.read(is);
                    Image image = imag;

                    cp.setLayout(new FlowLayout());
                    cp.add(new JLabel("View the image"));
                    cp.add(new JButton(new ImageIcon(image)));
                }
                catch(Exception ex)
                {
                   // The driver could not handle this as a BLOB...
                   // Fallback to default (and slower) byte[] handling
                   byte[] bytes = rs.getBytes(1);
                   JOptionPane.showMessageDialog(null, "Error "+ex.getMessage());
                }
            }


        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }


    }

    public static void main(String[] args)
    {
        PictureRetriver pr = new PictureRetriver();
        pr.setSize(450,450);
        pr.setVisible(true);
    }
}

it would fail to read the data and run to the Exception, may i please have help on it

this doesn't really give us much to go on....
some good advice on Exception handling:
if your code can throw (for instance) a NumberFormatException, and you are aware of this, don't have the Exception class catch all of them. go as specific as possible, it'll make it easier figuring out what is going wrong where.

also: print the stacktrace, it'll make it more clear of what line throws the exception

for instance:

try{
// code that might throw NumberFormatException here
}
catch(NumberFormatException nfe){
  System.out.println("NUMBERFORMATEXCEPTION CAUGHT");
  nfe.printStackTrace(); // this 'll give you all possible info on where it goes wrong
}
catch(Exception e){ // just in case something you don't see comming might go wrong
  System.out.println("DEFAULT EXCEPTION CAUGHT");
  e.printStackTrace();
}

we don't really know anything about your DB or it's contents, so it's not that easy to give remarks on that..

Its ok... I discovered that they are files and if they are files i can have them added into the JButton by calling on the path to the image to where it would be stored.... so cool .... it is ok....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.