这是一个在 mysql blob 中序列化图像(图标)的简单示例。
创建表t1(id整型主键auto_increment,img longblob不为空);
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;
}
}
}
}
}
这是一个测试应用程序。从数据库中读取图像是使用等于 1 的固定图像 ID 执行的,仅用于演示。在实际实现中,最好压缩数据以节省空间。当然,记录的图像(对象)的提取应该限制在绝对最小值。
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));
}
}