This is a simple example of serializing an image (Icon) in a mysql blob.
create table t1 (id integer primary key auto_increment, img longblob not null);
public class Database {
public Database() {
}
private Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "toor");
}
public boolean storeIcon(Icon icon) throws SQLException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try(ObjectOutputStream os = new ObjectOutputStream(baos)) {
os.writeObject(icon);
}
try(Connection connection = getConnection()) {
String query = "insert into t1 (img) values (?)";
try(PreparedStatement statement = connection.prepareStatement(query)) {
statement.setBlob(1, new ByteArrayInputStream(baos.toByteArray()));
return statement.executeUpdate() > 0;
}
}
}
public Icon loadIcon(long id) throws SQLException, IOException, ClassNotFoundException {
try(Connection connection = getConnection()) {
String query = "select img from t1 where id = ?";
try(PreparedStatement statement = connection.prepareStatement(query)) {
statement.setLong(1, id);
try(ResultSet rs = statement.executeQuery()) {
if(rs.next()) {
Blob blob = rs.getBlob("img");
try(ObjectInputStream is = new ObjectInputStream(blob.getBinaryStream())) {
return (Icon) is.readObject();
}
}
return null;
}
}
}
}
}
This is a test application. Reading images from the database is performed with a fixed image ID equal to 1, just for demonstration. In real implementation, it's nice to compress the data to save space. And of course, the extraction of the recorded images (objects) should be limited to the absolute minimum.
public class MainFrame extends JFrame {
private JLabel imageLabel = new JLabel();
private JButton loadImageFromFileButton = new JButton("Load image from file");
private JButton storeImageIntoDBButton = new JButton("Store image into DB");
private JButton loadImageFromDBButton = new JButton("Load image from DB");
public MainFrame() throws HeadlessException {
super("JDBC Test");
createGUI();
}
private void createGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout(5, 20));
imageLabel.setPreferredSize(new Dimension(200, 200));
imageLabel.setHorizontalAlignment(JLabel.CENTER);
imageLabel.setVerticalAlignment(JLabel.CENTER);
loadImageFromFileButton.addActionListener(this::loadImageFromFile);
loadImageFromDBButton.addActionListener(this::loadImageFromDB);
storeImageIntoDBButton.addActionListener(this::storeImageIntoDB);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
buttonsPanel.add(loadImageFromFileButton);
buttonsPanel.add(Box.createHorizontalStrut(25));
buttonsPanel.add(loadImageFromDBButton);
buttonsPanel.add(Box.createHorizontalStrut(5));
buttonsPanel.add(storeImageIntoDBButton);
add(imageLabel, BorderLayout.CENTER);
add(buttonsPanel, BorderLayout.PAGE_END);
pack();
setLocationRelativeTo(null);
}
private void loadImageFromFile(ActionEvent event) {
JFileChooser chooser = new JFileChooser();
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
ImageIcon imageIcon = new ImageIcon(chooser.getSelectedFile().getAbsolutePath());
imageLabel.setIcon(imageIcon);
}
}
private void storeImageIntoDB(ActionEvent event) {
try {
Database db = new Database();
db.storeIcon(imageLabel.getIcon());
}
catch (SQLException | IOException e) {
e.printStackTrace();
}
}
private void loadImageFromDB(ActionEvent event) {
try {
Database db = new Database();
Icon icon = db.loadIcon(1L);
imageLabel.setIcon(icon);
}
catch (SQLException | IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
}
}