๐Ÿ“Œ JDBC + Swing ๊ธฐ๋ฐ˜ ๋ฉ”๋ชจ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ

My Pale Blue Dotยท2025๋…„ 3์›” 17์ผ
0

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
23/35
post-thumbnail

๐Ÿ“… ๋‚ ์งœ: 2025๋…„ 3์›” 17์ผ


๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ

์˜ค๋Š˜์€ JDBC์™€ Swing GUI๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฉ”๋ชจ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ–ˆ๋‹ค.
์ด ํ”„๋กœ๊ทธ๋žจ์€ ํŒŒ์ผ ์ €์žฅ/๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๊ธฐ๋Šฅ๊ณผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™์„ ํ†ตํ•œ ๋ฉ”๋ชจ ์ €์žฅ/์กฐํšŒ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.


1๏ธโƒฃ JDBC ๊ฐœ๋…

โœ… JDBC๋ž€?

  • JDBC (Java Database Connectivity) ๋Š” Java์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐํ•˜์—ฌ SQL์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” API์ด๋‹ค.
  • JDBC๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด MySQL, Oracle, PostgreSQL ๋“ฑ ๋‹ค์–‘ํ•œ DB์™€ ์—ฐ๋™์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

โœ… JDBC ์ฃผ์š” ๊ธฐ๋Šฅ

๊ธฐ๋Šฅ์„ค๋ช…
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐConnection ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ DB์™€ ์—ฐ๊ฒฐ
SQL ์‹คํ–‰Statement, PreparedStatement ๋ฅผ ์ด์šฉํ•˜์—ฌ SQL ๋ฌธ ์‹คํ–‰
๊ฒฐ๊ณผ ์กฐํšŒResultSet์„ ์‚ฌ์šฉํ•˜์—ฌ SELECT ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ
ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌcommit(), rollback() ์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ ๊ด€๋ฆฌ
์—ฐ๊ฒฐ ํ•ด์ œclose() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ๊ฒฐ ์ข…๋ฃŒ

2๏ธโƒฃ JDBC + Swing ๋ฉ”๋ชจ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ

โœ… ๊ธฐ๋Šฅ

  • GUI(Swing) ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฉ”๋ชจ๋ฅผ ์ž…๋ ฅ/๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ.
  • ํŒŒ์ผ๋กœ ๋ฉ”๋ชจ ์ €์žฅ ๋ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๊ธฐ๋Šฅ ์ง€์›.
  • MySQL์„ ์ด์šฉํ•œ ๋ฉ”๋ชจ ์ €์žฅ (INSERT), ์กฐํšŒ (SELECT) ๊ธฐ๋Šฅ ํฌํ•จ.

โœ… ์‚ฌ์šฉ ๊ธฐ์ˆ 

๊ธฐ์ˆ ์„ค๋ช…
Java SwingGUI ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„
JDBCMySQL DB ์—ฐ๊ฒฐ ๋ฐ SQL ์‹คํ–‰
ํŒŒ์ผ I/O (FileWriter, FileReader)๋ฉ”๋ชจ ์ €์žฅ/๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๊ธฐ๋Šฅ

3๏ธโƒฃ JDBC + Swing ๋ฉ”๋ชจ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ

๐Ÿ“Œ 1. Memo ํด๋ž˜์Šค (๋ฐ์ดํ„ฐ ๋ชจ๋ธ)

class Memo {
    private int id;
    private String text;
    private LocalDateTime createdAt;

    // ๊ธฐ๋ณธ ์ƒ์„ฑ์ž
    Memo() {}

    // ๋ชจ๋“  ์ธ์ž๋ฅผ ๋ฐ›๋Š” ์ƒ์„ฑ์ž
    public Memo(int id, String text, LocalDateTime createdAt) {
        this.id = id;
        this.text = text;
        this.createdAt = createdAt;
    }

    // Getter & Setter ๋ฉ”์„œ๋“œ
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getText() { return text; }
    public void setText(String text) { this.text = text; }
    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }

    // toString(): ๊ฐ์ฒด ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜
    @Override
    public String toString() {
        return String.format("Memo [id=%d, text=%s, createdAt=%s]", id, text, createdAt);
    }
}

โœ… ๊ธฐ๋Šฅ:
โ€ข Memo ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  DB์—์„œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ.
โ€ข toString()์„ ์žฌ์ •์˜ํ•˜์—ฌ System.out.println(memo); ํ•˜๋ฉด ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ์ถœ๋ ฅ๋จ.

โธป

๐Ÿ“Œ 2. C07GUI ํด๋ž˜์Šค (GUI + JDBC ์—ฐ๋™)

class C07GUI extends JFrame implements ActionListener, KeyListener {
    // GUI ์š”์†Œ ์„ ์–ธ
    private JTextArea area1;   
    private JTextField txt1;   
    private JButton btnSaveFile, btnLoadFile, btnInsert, btnSelect;
    
    // JDBC ๊ด€๋ จ ๋ณ€์ˆ˜ ์„ ์–ธ
    static String id = "root";
    static String pw = "1234";
    static String url = "jdbc:mysql://localhost:3306/testdb";
    static Connection conn = null;
    static PreparedStatement pstmt = null;
    static ResultSet rs = null;

    // GUI ์ƒ์„ฑ์ž
    C07GUI(String title) {
        super(title);  
        setBounds(100, 100, 410, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // ํŒจ๋„ ์„ค์ •
        JPanel panel = new JPanel();
        panel.setLayout(null);

        // ํ…์ŠคํŠธ ์˜์—ญ (๋ฉ”๋ชจ ์ž…๋ ฅ & ์ถœ๋ ฅ)
        area1 = new JTextArea();
        JScrollPane scroll1 = new JScrollPane(area1);
        scroll1.setBounds(10, 10, 250, 280);

        // ๋ฒ„ํŠผ ์ƒ์„ฑ ๋ฐ ๋ฐฐ์น˜
        btnSaveFile = new JButton("ํŒŒ์ผ ์ €์žฅ");
        btnSaveFile.setBounds(270, 10, 110, 30);
        btnLoadFile = new JButton("ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ");
        btnLoadFile.setBounds(270, 50, 110, 30);
        btnInsert = new JButton("DB ์ €์žฅ (INSERT)");
        btnInsert.setBounds(270, 90, 110, 30);
        btnSelect = new JButton("DB ์กฐํšŒ (SELECT)");
        btnSelect.setBounds(270, 130, 110, 30);

        // ์ž…๋ ฅ ํ•„๋“œ (JTextField)
        txt1 = new JTextField();
        txt1.setBounds(10, 300, 250, 30);

        // ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ๋“ฑ๋ก
        btnSaveFile.addActionListener(this);
        btnLoadFile.addActionListener(this);
        btnInsert.addActionListener(this);
        btnSelect.addActionListener(this);
        txt1.addKeyListener(this);

        // ํŒจ๋„์— ์š”์†Œ ์ถ”๊ฐ€
        panel.add(scroll1);
        panel.add(btnSaveFile);
        panel.add(btnLoadFile);
        panel.add(btnInsert);
        panel.add(btnSelect);
        panel.add(txt1);

        // ํ”„๋ ˆ์ž„์— ํŒจ๋„ ์ถ”๊ฐ€ ๋ฐ ํ‘œ์‹œ
        add(panel);
        setVisible(true);

        // ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์„ค์ •
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, id, pw);
            System.out.println("โœ… DB ์—ฐ๊ฒฐ ์„ฑ๊ณต");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

โœ… ๊ธฐ๋Šฅ:
โ€ข JTextArea, JTextField, JButton์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋ชจ ์ž…๋ ฅ ๋ฐ ํŒŒ์ผ ์ €์žฅ/๋ถˆ๋Ÿฌ์˜ค๊ธฐ GUI ๊ตฌํ˜„.
โ€ข JDBC๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DB ์—ฐ๊ฒฐ ์„ค์ •.

โธป

๐Ÿ“Œ 3. C07SwingAddDBMain (๋ฉ”์ธ ์‹คํ–‰)

public class C07SwingAddDBMain {
    public static void main(String[] args) {
        new C07GUI("๋ฉ”๋ชจ ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ");
    }
}

โœ… ๊ธฐ๋Šฅ:
โ€ข C07GUI ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰.

โธป

๐Ÿ”ฅ ํ•™์Šต ์ •๋ฆฌ

ํ•™์Šต ๋‚ด์šฉ์„ค๋ช…
JDBC ๊ธฐ๋ณธ ๊ฐœ๋… ํ•™์ŠตDB ์—ฐ๊ฒฐ, SQL ์‹คํ–‰, ๊ฒฐ๊ณผ ์กฐํšŒ ๋ฐ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ
Java Swing ํ™œ์šฉGUI ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๋ฐ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
ํŒŒ์ผ ์ž…์ถœ๋ ฅ ๊ธฐ๋Šฅ ์ถ”๊ฐ€FileWriter, FileReader ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋ชจ ์ €์žฅ ๋ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
DB์™€ ์—ฐ๋™ํ•˜์—ฌ ๋ฉ”๋ชจ ์ €์žฅ/์กฐํšŒ ๊ธฐ๋Šฅ ๊ตฌํ˜„INSERT, SELECT ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ DB์—์„œ ๋ฉ”๋ชจ ๊ด€๋ฆฌ

โธป

๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ
โ€ข JDBC ๊ณต์‹ ๋ฌธ์„œ
โ€ข Java Swing ๊ณต์‹ ๋ฌธ์„œ

profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€