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()
            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'");

                    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();
4 Years
Discussion Span
Last Post by edensigauke

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:

// code that might throw NumberFormatException here
catch(NumberFormatException nfe){
  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");

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....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.