[DB] Embedded SQL๊ณผ Interactive SQL
Embedded SQL์ ์ดํ๋ฆฌ์ผ์ด์ ๋ด๋ถ์ SQL ๋ฌธ์ฅ์ ์๋ฒ ๋ฉํ์ฌ DB์ ์ ๊ทผํ๋ ๋ฐฉ์์ด๋ค.
Interactive SQL์ ์ฌ์ฉ์๊ฐ ์ง์ ํฐ๋ฏธ๋์ด๋ ์ฝ์์์ SQL ๋ฌธ์ฅ์ ์ ๋ ฅํ์ฌ ์คํํ๋ ๋ฐฉ์์ด๋ค.
Embedded SQL์ ํธ์คํธ ์ธ์ด ์์ค ์ฝ๋์ SQL์ ์ง์ ์ฝ์ ํ์ฌ ์ปดํ์ผ ์์ ์ ์ฒ๋ฆฌ๋๋ ๋ฐฉ์์ด๋ฉฐ, Java์ JDBC๋ Embedded SQL์ด ์๋๋ผ SQL์ ๋ฐํ์์ API๋ฅผ ํตํด ์คํํ๋ ๋ฐฉ์์ ๋๋ค.
# ์ฃผ์ ์ฐจ์ด์
## 1. ํธ์คํธ ์ธ์ด ๋ณ์ ์ฌ์ฉ
- Embedded SQL์ C, Java์ ๊ฐ์ ํธ์คํธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ๋ด์์ ๋ณ์๋ฅผ SQL ๊ตฌ๋ฌธ์ ์ผ๋ถ๋ถ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
- Interactive SQL์ ์ฌ์ฉ์ ์ ๋ ฅ๊ณผ ์์๊ฐ๋ง์ ์ฌ์ฉํ์ฌ ํธ์คํธ ์ธ์ด ๋ณ์ ๊ฐ๋ ์ด ์๋ค.
int empId = 1001;
String empName = "";
String query = "SELECT name FROM employee WHERE id = ?";
try (Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setInt(1, empId); // ํธ์คํธ ์ธ์ด ๋ณ์
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
empName = rs.getString("name");
}
System.out.println("์ง์ ์ด๋ฆ: " + empName);
} catch (SQLException e) {
e.printStackTrace();
}
- ํธ์คํธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ empId๋ฅผ SQL ์ฟผ๋ฆฌ์ ์ฝ์ ํ๋ ํํ
SELECT id, name FROM employee;
- ๋ณ์๋ฅผ ์ง์ ์ฌ์ฉํ ์ ์๊ณ , ์ง์ ๊ฐ์ ์ ๋ ฅํ์ฌ ์ฌ์ฉ
## 2. ์๋ฌ์ ์ํ ์ฒ๋ฆฌ
- Embedded SQL์ ์คํ ์ค ๋ฐ์ํ ์๋ฌ์ ์ํ ์ ๋ณด๋ฅผ ๋ณ๋์ ๋ฐ์ดํฐ ๊ตฌ์กฐ(SQLCA, SQL Communications Area)์ ์ ์ฅํ์ฌ ์ดํ๋ฆฌ์ผ์ด์ ์์ ํด๋น ๊ตฌ์กฐ๋ฅผ ์ฐธ์กฐํ์ฌ ์๋ฌ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค.
- Interactive SQL์ ์๋ฌ ๋ฐ ์ํ ๋ฉ์์ง๊ฐ ์ฆ๊ฐ ํ๋ฉด์ ํ์๋๋ค.
### Java JDBC์ ์๋ฌ ์ฒ๋ฆฌ
try (Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employee (id, name) VALUES (?, ?)")) {
pstmt.setInt(1, empId);
pstmt.setString(2, empName);
pstmt.executeUpdate();
} catch (SQLException e) {
System.err.println("SQL Error: " + e.getMessage()); // ์๋ฌ ๋ฐ์ ์ ์ฒ๋ฆฌ
}
- SQLException์ ํตํด ์๋ฌ ์ฒ๋ฆฌ
### C SQLCA์ ์๋ฌ ์ฒ๋ฆฌ
EXEC SQL INSERT INTO employee (id, name) VALUES (:emp_id, :emp_name);
if (SQLCA.SQLCODE != 0) {
printf("Error occurred: %s\n", SQLCA.SQLERRM.sqlerrmc);
}
- SQLCA๋ฅผ ํตํด ์๋ฌ ์ฒ๋ฆฌ
### Interactive SQL์ ์๋ฌ ํ์
- ์๋ฌ ๋ฐ ์ํ ๋ฉ์์ง๊ฐ ์ฆ๊ฐ์ ์ผ๋ก ํ๋ฉด์ ํ์
## 3. ์ปค์(Cursors)์ ์ฌ์ฉ
์ปค์๋ ํ์ฌ ์ฒ๋ฆฌ ์ค์ธ ํ์ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ ์ญํ ์ด๋ค.
- Embedded SQL์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ํ๋์ ํ(row)์ฉ ์์ฐจ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋๋ก ์ปค์๋ฅผ ์ ๊ณตํ๋ค.
- Interactive SQL์ ์ผ๋ฐ์ ์ผ๋ก ํ ๋ฒ์ ์คํ์ผ๋ก ์ ์ฒด ๊ฒฐ๊ณผ ์งํฉ์ ํ๋ฉด์ ์ถ๋ ฅํ๋ค. ์ปค์๋ฅผ ํตํ ํ๋์ ํ ์ ๊ทผ์ ์ ๊ณตํ์ง ์๋๋ค.
### Java JDBC์ ์ปค์ ์ฌ์ฉ
String query = "SELECT id, name FROM employee";
try (Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) { // ResultSet์ ์ปค์์ ์ญํ ์ ํจ
int empId = rs.getInt("id");
String empName = rs.getString("name");
System.out.printf("Employee ID: %d, Name: %s%n", empId, empName);
}
} catch (SQLException e) {
e.printStackTrace();
}
- ResultSet์ ๋ด๋ถ์ ์ผ๋ก ์ปค์๋ฅผ ์ฌ์ฉํ์ฌ ํ๋์ ๋ฐ์ดํฐ ํ์ ์ฒ๋ฆฌํ ์ ์๋๋ก ํจ
### Interactive SQL์ ์ปค์ ์ฌ์ฉ ๋ถ๊ฐ
- SQL ๊ฒฐ๊ณผ๊ฐ ์ฆ์ ํ๋ฉด์ ๋ชจ๋ ์ถ๋ ฅ๋๋ฉฐ, ํ๋์ ๋ฐ์ดํฐ ํ์ ๊ฐ๋ณ ์ฒ๋ฆฌํ ์ ์๋ ์ปค์ ๊ธฐ๋ฅ์ด ์์
## 4. ํผ(Forms) ๋ฌธ์ฅ ํ์ฉ
Form์ ๋ด์ฉ์ด SQL์ ๋ฐ์๋ ์ ์๋ ๊ฒ์ form ๋ฌธ์๋ฅผ ํ์ฉํ๋ค๊ณ ํ๋ค.
- Embedded SQL์ ์ฌ์ฉ์ ์ ๋ ฅ์ด๋ ๋ฐ์์ SQL์ ๋ฐ์ํ ์ ์๋ค.
- Interactive SQL์ ๋ฐ์ดํฐ ์ ์ถ๋ ฅ์ ์ํ ํ๋ฉด ์ ์ด๊ฐ ์๊ณ , ๊ธฐ๋ณธ์ ์ผ๋ก ํฐ๋ฏธ๋ ์ ์ถ๋ ฅ๋ง ์ง์ํ๋ค.
### Java JDBC์์ ํผ ๋ฌธ์ฅ ํ์ฉ
private void saveEmployee() {
int empId = Integer.parseInt(idField.getText());
String empName = nameField.getText();
String query = "INSERT INTO employee (id, name) VALUES (?, ?)";
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass");
PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setInt(1, empId);
pstmt.setString(2, empName);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Employee saved successfully!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
- idField.getText(), nameField.getText() ์ ๊ฐ์ด ํผ ํ๋์ ๋ด์ฉ์ SQL์ ๋ฐ์ ๊ฐ๋ฅ
### Interactive SQL์ ํผ ๋ฌธ์ฅ ํ์ฉ ๋ถ๊ฐ
- ์ ๋ ฅํ ๊ณ ์ SQL๋ฌธ๋ง ์คํ๋จ
## 5. ๋์ ํ๋ก๊ทธ๋๋ฐ
- Embedded SQL์ ๋ฐํ์ ์ค ๋์ ์ผ๋ก SQL๋ฌธ์ ์คํํ๊ณ , ์์ฑํ ์ ์๋ค. ์ ์ฐํ ์ดํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ๊ฐ๋ฅํ๊ฒ ํ๋ค.
- Interactive SQL์ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๊ณ ์ SQL๋ฌธ๋ง ์คํ ๊ฐ๋ฅํ๋ค.
### Java JDBC์ ์์
import java.sql.*;
public class DynamicSQLExample {
public static void main(String[] args) {
String tableName = "employee"; // ๋ฐํ์์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
String columnName = "name"; // ๋ฐํ์์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
int id = 1001;
// ๋์ ์ผ๋ก SQL ์์ฑ
String sql = String.format("SELECT %s FROM %s WHERE id = ?", columnName, tableName);
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass");
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String result = rs.getString(columnName);
System.out.println(columnName + ": " + result);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- ๋ค์ํ ํ ์ด๋ธ์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋์ ์ฟผ๋ฆฌ ๊ตฌ์ฑ
### Interactive SQL ๋์ ํ๋ก๊ทธ๋๋ฐ ๋ถ๊ฐ
- ๊ณ ์ SQL๋ง ์คํ ๊ฐ๋ฅ
6. ๋ค์ค ์ธ์ (Multiple Sessions) ์ง์
- Embedded SQL์ ํ ์ดํ๋ฆฌ์ผ์ด์ ์์ ์๋ก ๋ค๋ฅธ DB์ ๋์ ์ ์ํ๊ฑฐ๋ ๊ฐ์ DB์ ์ฌ๋ฌ ์ธ์ ์ ์ด ์ ์๋ค.
- Interactive SQL์ ์ผ๋ฐ์ ์ผ๋ก ํ ๋ฒ์ ํ๋์ ์ธ์ ์์ ํ๋์ DB์๋ง ์ ๊ทผ ๊ฐ๋ฅํ๋ค.
### Java JDBC์์ ๋ค์ค ์ธ์ ์ฌ์ฉ
public static void main(String[] args) {
String dbUrl1 = "jdbc:mysql://localhost:3306/db1";
String dbUrl2 = "jdbc:mysql://localhost:3306/db2";
try (Connection conn1 = DriverManager.getConnection(dbUrl1, "user1", "pass1");
Connection conn2 = DriverManager.getConnection(dbUrl2, "user2", "pass2")) {
// ์ธ์
1 ์ฌ์ฉ
PreparedStatement pstmt1 = conn1.prepareStatement(
"INSERT INTO employee (id, name) VALUES (?, ?)");
pstmt1.setInt(1, 1001);
pstmt1.setString(2, "Alice");
pstmt1.executeUpdate();
System.out.println("Inserted into db1");
// ์ธ์
2 ์ฌ์ฉ
PreparedStatement pstmt2 = conn2.prepareStatement(
"INSERT INTO employee (id, name) VALUES (?, ?)");
pstmt2.setInt(1, 2002);
pstmt2.setString(2, "Bob");
pstmt2.executeUpdate();
System.out.println("Inserted into db2");
} catch (SQLException e) {
e.printStackTrace();
}
}
- ํ ์ดํ๋ฆฌ์ผ์ด์ ์์ ์ธ์ 1๊ณผ ์ธ์ 2์ ๋์ ์ ์ ๊ฐ๋ฅ
### Interactive SQL์ ๋จ์ผ ์ธ์ ๋ง ๊ฐ๋ฅ
- ํ๋์ ์ฝ์, ํ๋์ ์ธ์ ์ด๋ผ๊ณ ๋ณผ ์ ์์