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