Retrieving binary data from a database table

This example will read images stored in a table.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import java.io.*;  
 
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      ArrayList al = getImagesFromDB();
      Iterator iter = al.iterator();
      while (iter.hasNext()) {
         getContentPane().add((JLabel) iter.next());
      }
   }
 
   public ArrayList getImagesFromDB() {
      ArrayList al = new ArrayList();
 
      try {
         Class.forName("org.gjt.mm.mysql.Driver").newInstance();
         Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.1/esus", 
                                                       "joris",
                                                       "mypass");
 
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT name, length, data FROM images");
      
         while(rs.next()) {
            String name = rs.getString("name");
            int length = rs.getInt("length");
            byte[] buf = rs.getBytes("data");
 
            ImageIcon ii = new ImageIcon(buf);
            JLabel label = new JLabel(name, ii, SwingConstants.CENTER); 
            al.add(label);
         }
      }
      catch(Exception e) {
         e.printStackTrace();
      }
 
      return al;
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   } 
}