์ค๋์ JDBC์ Swing GUI๋ฅผ ํ์ฉํ์ฌ ๋ฉ๋ชจ ๊ด๋ฆฌ ํ๋ก๊ทธ๋จ์ ๊ฐ๋ฐํ๋ค.
์ด ํ๋ก๊ทธ๋จ์ ํ์ผ ์ ์ฅ/๋ถ๋ฌ์ค๊ธฐ ๊ธฐ๋ฅ๊ณผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๋์ ํตํ ๋ฉ๋ชจ ์ ์ฅ/์กฐํ ๊ธฐ๋ฅ์ ํฌํจํ๊ณ ์๋ค.
๊ธฐ๋ฅ | ์ค๋ช |
---|---|
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ | Connection ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ DB์ ์ฐ๊ฒฐ |
SQL ์คํ | Statement , PreparedStatement ๋ฅผ ์ด์ฉํ์ฌ SQL ๋ฌธ ์คํ |
๊ฒฐ๊ณผ ์กฐํ | ResultSet ์ ์ฌ์ฉํ์ฌ SELECT ๊ฒฐ๊ณผ ๊ฐ์ ธ์ค๊ธฐ |
ํธ๋์ญ์ ์ฒ๋ฆฌ | commit() , rollback() ์ ํตํด ๋ฐ์ดํฐ ๋ณ๊ฒฝ ๊ด๋ฆฌ |
์ฐ๊ฒฐ ํด์ | close() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ฐ๊ฒฐ ์ข
๋ฃ |
๊ธฐ์ | ์ค๋ช |
---|---|
Java Swing | GUI ์ธํฐํ์ด์ค ๊ตฌํ |
JDBC | MySQL DB ์ฐ๊ฒฐ ๋ฐ SQL ์คํ |
ํ์ผ I/O (FileWriter, FileReader) | ๋ฉ๋ชจ ์ ์ฅ/๋ถ๋ฌ์ค๊ธฐ ๊ธฐ๋ฅ |
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 ๊ณต์ ๋ฌธ์