10: empPhotoGUI
package com.github.ryan6073.toLearn;
import javax.swing.*;
import java.io.*;
import java.sql.*;
import java.util.Objects;
import javax.swing.border.*;
import javax.swing.filechooser.FileNameExtensionFilter;


public class empPhotoGUI {
    JFrame frame = new JFrame();
    JLabel pictureLabel = new JLabel("");
    Connection connection = null;

    public static void main(String[] args) {
        new empPhotoGUI();
    }

    public empPhotoGUI() {
        connectToDB();
        showGUI();
    }

    private Connection connectToDB() {
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            connection = DriverManager.getConnection(
                    "jdbc:db2://192.168.80.128:50000/sample",
                    "db2admin",
                    "db2admin"
            );
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "连接数据库失败:" + e.getMessage());
        }
        return connection;
    }

    private void showGUI() {
        frame.setTitle("empPhotoGUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(100, 100, 400, 350);

        // 设置图片显示区域
        JPanel labelPanel = new JPanel();
        labelPanel.setBounds(10, 53, 330, 260);
        pictureLabel.setBounds(10, 10, 310, 240);
        labelPanel.add(pictureLabel);

        // 设置按钮区域
        JPanel buttonPanel = new JPanel();
        JButton insertButton = new JButton("插入");
        JButton queryButton = new JButton("查询");
        queryButton.addActionListener(e -> handleQueryButton());
        insertButton.addActionListener(e -> handleInsertButton());
        buttonPanel.add(queryButton);
        buttonPanel.add(insertButton);
        buttonPanel.setBounds(10, 10, 330, 30);
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));

        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(0, 0, 5, 0));
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.add(labelPanel);
        contentPane.add(buttonPanel);
        frame.setContentPane(contentPane);
        frame.setVisible(true);
    }

    // 查询按钮的事件处理
    private void handleQueryButton() {
        String employeeNumber = JOptionPane.showInputDialog("请输入员工编号:\n ");
        if (employeeNumber == null) {
            return;
        }
        Statement statement;
        if (!Objects.equals(employeeNumber, "")) {
            try {
                statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery("" +
                        "SELECT picture FROM JLU.emp_photo WHERE " +
                        "empno = '" + employeeNumber + "'");
                File fileOutput = null;
                while (resultSet.next()) {
                    Blob blob = resultSet.getBlob(1);
                    InputStream inputStream = blob.getBinaryStream();
                    fileOutput = new File(employeeNumber + ".jpg");
                    FileOutputStream stream = new FileOutputStream(fileOutput);
                    int c;
                    while ((c = inputStream.read()) != -1) {
                        stream.write(c);
                    }
                    stream.close();
                }
                if (fileOutput == null) {
                    JOptionPane.showMessageDialog(null, "没有找到该员工的照片");
                    return;
                }
                ImageIcon icon =new ImageIcon(fileOutput.getAbsolutePath());
                icon.setImage(icon.getImage().getScaledInstance(
                        310,
                        240,
                        100
                ));
                pictureLabel.setIcon(icon);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    }

    // 插入按钮的事件处理
    private void handleInsertButton() {
        PreparedStatement preparedStatement;
        try {
            preparedStatement = connection.prepareStatement(
                    "INSERT INTO JLU.emp_photo VALUES (?, 'jpeg', ?)");
            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "image(.jpg/.jpeg)",
                    "jpg",
                    "jpeg"
            );
            String employeeNumber = JOptionPane.showInputDialog("请输入员工编号:\n ");
            if (employeeNumber == null) {
                return;
            }
            JFileChooser filechooser = new JFileChooser(".");
            filechooser.setMultiSelectionEnabled(false);
            filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            filechooser.setFileFilter(filter);
            int state = filechooser.showOpenDialog(frame.getContentPane());
            if (state == JFileChooser.APPROVE_OPTION) {
                File path = filechooser.getSelectedFile();
                File file = new File(path.getPath());
                BufferedInputStream imageInput = new BufferedInputStream(
                        new FileInputStream(file)
                );
                preparedStatement.setString(1, employeeNumber);
                preparedStatement.setBinaryStream(2, imageInput, file.length());
                preparedStatement.executeUpdate();
                connection.commit();
                JOptionPane.showMessageDialog(null, "插入成功");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }
}

运行结果:
查询:

img.png

插入:

img_1.png

img_2.png

img_3.png