๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Java] Lecture - Day 15 ๋ณธ๋ฌธ

Development Study/Java

[Java] Lecture - Day 15

f1r3_r41n 2024. 4. 26. 00:21
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ–ฅ Basic Java - Day 15

๐Ÿ‘‰๐Ÿป [Chapter 16] JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

  • ์—ฌ๋Ÿฌ ์‘์šฉ ์‹œ์Šคํ…œ๋“ค์˜ ํ†ตํ•ฉ๋œ ์ •๋ณด๋“ค์„ ์ €์žฅํ•˜์—ฌ ์šด์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต์šฉ ๋ฐ์ดํ„ฐ๋“ค์˜ ์ง‘ํ•ฉ
  • ๋Œ€๊ทœ๋ชจ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ €์žฅ, ๊ฒ€์ƒ‰, ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ๋„๋กœ ์กฐ์งํ™”ํ•˜์—ฌ ์ €์žฅ

DBMS

  • DataBase Management System
  • ๋‹ค์ˆ˜์˜ ์‚ฌ์šฉ์ž๋“ค์ด ๋™์‹œ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ด€๋ฆฌ
  • Oracle, SQL Server, MySQL, DB2, MariaDB, ...
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ข…๋ฅ˜
    • ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      • ๋ฐ์ดํ„ฐ๋“ค์ด ๋‹ค์ˆ˜์˜ ํ…Œ์ด๋ธ”๋กœ ๊ตฌ์„ฑ
      • ํ…Œ์ด๋ธ”์˜ ๊ฐ ํ–‰์€ ํ•˜๋‚˜์˜ ๋ ˆ์ฝ”๋“œ
      • ๊ฐ ํ…Œ์ด๋ธ”์€ ํ‚ค(key)์™€ ๊ฐ’(value)์˜ ๊ด€๊ณ„๋กœ ํ‘œํ˜„
      • ํ‚ค๋Š” ํ…Œ์ด๋ธ”์˜ ์—ด ์ด๋ฆ„
    • ๊ฐ์ฒด ์ง€ํ–ฅ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      • ์ •๋ณด๋ฅผ ๊ฐ์ฒด์˜ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค
      • ๊ฐ์ฒด ๋ชจ๋ธ์„ ๊ทธ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ์šฉํ•˜๋ฏ€๋กœ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์˜ ๊ฐ์ฒด ๋ชจ๋ธ๊ณผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ชจ๋ธ์ด ์„œ๋กœ ๋ถ€ํ•ฉํ•จ

SQL

  • Structured Query Language
  • ๊ด€๊ณ„ํ˜• DBMS์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ ์ƒ์„ฑ, ์ž๋ฃŒ์˜ ๊ฒ€์ƒ‰๏น’๊ด€๋ฆฌ๏น’์ˆ˜์ •, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ฐ์ฒด ์ ‘๊ทผ ๊ด€๋ฆฌ ๋“ฑ์„ ์œ„ํ•ด ๊ณ ์•ˆํ•œ ์–ธ์–ด

JDBC

  • Java DataBase Connectivity
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ Java API
  • ๋‹ค์–‘ํ•œ DBMS์— ๋Œ€ํ•ด ์ผ๊ด€๋œ ์ž๋ฐ” API๋ฅผ ์ œ๊ณตํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ, ๊ฒ€์ƒ‰, ์ˆ˜์ •, ๊ด€๋ฆฌ ๋“ฑ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ
  • JDBC ๋“œ๋ผ์ด๋ฒ„ ๋งค๋‹ˆ์ €
    • ์ž๋ฐ” API์—์„œ ์ง€์›ํ•˜๋ฉฐ DBMS์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋“œ
  • JDBC ๋“œ๋ผ์ด๋ฒ„
    • DBMS๋งˆ๋‹ค ๊ณ ์œ ํ•œ JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์ œ๊ณต
    • JDBC ๋“œ๋ผ์ด๋ฒ„์™€ DBMS๋Š” ๊ฐ ํšŒ์‚ฌ์˜ ์ „์šฉ ํ”„๋กœํ† ์ฝœ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ฒ˜๋ฆฌ
  • MySQL Safe updates ๋ชจ๋“œ
    • ์‹ค์ˆ˜๋กœ ๋ฐ์ดํ„ฐ๋ฅผ update, delete ํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ •
    • Safe updates ๋ชจ๋“œ ํ•ด์ œ: [Edit - Preferences]
      • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์žฌ์—ฐ๊ฒฐ

JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ

  • JDBC API๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์ถ”๊ฐ€, ์‚ญ์ œ, ์ˆ˜์ •, ๊ฒ€์ƒ‰ ๋“ฑ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ
  • JDBC API(java.sql package)
    • DriverManager: ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋“œํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์—ฐ๊ฒฐ
    • Connection: ํŠน์ • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์—ฐ๊ฒฐ
    • Statement: SQL๋ฌธ์„ ์‹คํ–‰ํ•ด ์ž‘์„ฑ๋œ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
    • PreparedStatement: ์‚ฌ์ „์— ์ปดํŒŒ์ผ๋œ SQL๋ฌธ์„ ์‹คํ–‰
    • ResultSet: SQL์˜ Select๋ฌธ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์–ป์–ด์˜ฌ ๋•Œ ์‚ฌ์šฉ

JDBC ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ˆœ์„œ

  • JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•˜์—ฌ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    • Class.forName("driver_name");
  • DBMS์— ๋Œ€ํ•œ ์—ฐ๊ฒฐ ์ƒ์„ฑ
    • Connection conn = DriverManager.getConnection("url", "account", "pw");
  • Statement ์ƒ์„ฑ
    • Statement stmt = conn.createStatement();
  • ์งˆ์˜๋ฌธ ์‹คํ–‰: Select๋ฌธ์ผ ๋•Œ, ResultSet์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์Œ
    • ResultSet rs = stmt.executeQuery("select_query");
  • ResultSet ํ•ด์ง€
    • rs.close();
  • Statement ํ•ด์ง€
    • stmt.close();
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ํ•ด์ง€
    • conn.close();

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ

  • Class.forName("driver_name")
    • ์ง€์ • JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋Ÿฐํƒ€์ž„์— ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌ
    • MySQL JDBC ๋“œ๋ผ์ด๋ฒ„ ์ด๋ฆ„
      • version 8.0 ์ด์ƒ: com.mysql.cj.jdbc.Driver
      • version 5.x ์ดํ•˜: com.mysql.jdbc.Driver
    • ClassNotFoundException ์˜ค๋ฅ˜์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•จ
  • DriverManagement.getConnection("url", "account", "pw")
    • DriverManager: JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค
    • url ํ˜•์‹
      • jdbc:mysql://[hostname][:port]/dbname[?param1=value1][&param2=value2]...
        • serverTimezone=UTC
        • useUnicode=true
        • characterEncoding=euc-kr
// DB ์—ฐ๊ฒฐ์— ํ•„์š”ํ•œ ์ •๋ณด๋“ค ์ƒ์ˆ˜ ์ฒ˜๋ฆฌ
public class DBConstant {
    public static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    public static String URL = "jdbc:mysql://localhost:3306/sampledb";
    public static String ID = "root";
    public static String PWD = "root";
}
import java.sql.*;

public class DBConnectExam01 {
    public static void main(String[] args) {
        Connection conn = null;
        try {
            Class.forName(DBConstant.DRIVER_NAME);  // MySQL ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋“œ
            conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);  // JDBC ์—ฐ๊ฒฐ
            System.out.println("DB Connection is Done...");
        } catch(ClassNotFoundException | SQLException e) {
            System.out.println(e.toString());
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch(SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

SQL ์‹คํ–‰

  • Connection ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด
    • Statement
      • ๋‹จ์ˆœํ•œ sql ๋ฌธ์žฅ์„ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉ
      • ์„ฑ๋Šฅ๊ณผ ํšจ์œจ์„ฑ์ด ๋‚ฎ์Œ
      • Statement stmt = conn.createStatement();
    • PreparedStatement
      • ๋ฐ˜๋ณต๋˜๋Š” sql ๋ฌธ์žฅ ์ˆ˜ํ–‰์— ํšจ์œจ์ 
      • ๋ฏธ๋ฆฌ ์ปดํŒŒ์ผ๋œ sql๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— Statement์— ๋น„ํ•ด ์‹คํ–‰ ์†๋„๊ฐ€ ๋น ๋ฆ„
      • sql ํ‹€์„ ๋ฏธ๋ฆฌ ์ •ํ•ด๋‘๊ณ , ๋‚˜์ค‘์— ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ๋ฐฉ์‹
      • PreparedStatement pstmt = conn.prepareStatement(SQL);
  • Statement ๊ฐ์ฒด์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ์งˆ์˜๋ฌธ ์‹คํ–‰
    • ResultSet executeQuery(SQL)
      • DB์˜ ๊ตฌ์กฐ์™€ ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š” ์งˆ์˜์— ์ ํ•ฉ
      • select๋ฌธ
      • ResultSet rs = stmt.executeQuery("select_query");
    • int executeUpdate(SQL)
      • DB์˜ ๊ตฌ์กฐ์™€ ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š” ์งˆ์˜์— ์ ํ•ฉ
      • create, drop, insert, delete, update๋ฌธ
      • create, drop์˜ ๋ฐ˜ํ™˜๊ฐ’: 0
      • insert, delete, update์˜ ๋ฐ˜ํ™˜๊ฐ’: ์งˆ์˜๋ฌธ ์‹คํ–‰์œผ๋กœ ์˜ํ–ฅ์„ ๋ฐ›์€ ํ–‰์˜ ์ˆ˜
      • int rowCount = stmt.executeUpdate("query");
  • ResultSet
    • ํ…Œ์ด๋ธ” ํ˜•ํƒœ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถ”์ƒํ™”ํ•œ ์ธํ„ฐํŽ˜์ด์Šค
      • BOF(Begin Of File, Before the first Row)
      • ํ…Œ์ด๋ธ” ๊ฐ’: rs.next() ๋กœ ํ–‰ ์ด๋™
      • EOF(End Of File, After the last Row)
    • ํ˜„์žฌ ๋ฐ์ดํ„ฐ์˜ ํ–‰(๋ ˆ์ฝ”๋“œ ์œ„์น˜)์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ปค์„œ๋ฅผ ๊ด€๋ฆฌ
      • ์ดˆ๊ธฐ ๊ฐ’์€ ์ฒซ ๋ฒˆ์งธ ํ–‰ ์ด์ „์„ ๊ฐ€๋ฆฌํ‚ด
    • ์ œ๊ณต ๋ฉ”์„œ๋“œ
      • boolean first(): ์ปค์„œ ์ฒซ ๋ฒˆ์งธ ํ–‰์œผ๋กœ ์ด๋™
      • boolean last(): ์ปค์„œ ๋งˆ์ง€๋ง‰ ํ–‰์œผ๋กœ ์ด๋™
      • boolean next(): ์ปค์„œ ๋‹ค์Œ ํ–‰์œผ๋กœ ์ด๋™
      • boolean previous(): ์ปค์„œ ์ด์ „ ํ–‰์œผ๋กœ ์ด๋™
      • boolean absolute(int row): ์ปค์„œ ์ง€์ •๋œ ํ–‰ row๋กœ ์ด๋™
      • boolean isFirst(): ์ฒซ ๋ฒˆ์งธ ํ–‰์ด๋ฉด true ๋ฐ˜ํ™˜
      • boolean isLast(): ๋งˆ์ง€๋ง‰ ํ–‰์ด๋ฉด true ๋ฐ˜ํ™˜
      • void close(): ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ JDBC ๋ฆฌ์†Œ์Šค ์ฆ‰์‹œ ๋ฐ˜ํ™˜
      • Xxx getXxx(String columnLable): ํ˜„์žฌ ํ–‰์—์„œ ์ง€์ •๋œ ์—ด ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜
      • Xxx getXxx(int columnIndex): ํ˜„์žฌ ํ–‰์—์„œ ์ง€์ •๋œ ์—ด ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ ๋ฐ˜ํ™˜
import java.sql.*;

public class DBConnectExam02 {
    public static void ResultSetPrint(ResultSet rs) throws SQLException {
        while(rs.next()) {
            System.out.print(rs.getString("id"));
            System.out.print("\t" + rs.getString("name"));
            System.out.println("\t" + rs.getString("dept"));
        }
    }

    public static void main(String[] args) {
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;

        try {
            Class.forName(DBConstant.DRIVER_NAME);
            conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);
            System.out.println("DB Connection is Done...");

            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from student");

            ResultSetPrint(rs);
            System.out.println();

            rs = stmt.executeQuery("select * from student where name='์ด๊ธฐ์ž'");
            ResultSetPrint(rs);
        } catch(ClassNotFoundException | SQLException e) {
            System.out.println(e.toString());
        } finally {
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(stmt != null) {
                try {
                    stmt.close();
                } catch(SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null) {
                try {
                    conn.close();
                } catch(SQLException e) {
                    System.out.println(e.toString());
                    e.printStackTrace();
                }
            }
        }
    }
}
  • Statement๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰
import java.sql.*;

public class DBConnectExam03 {
    public static void ObjectClose(Connection conn, Statement stmt, ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt != null) {
            try {
                stmt.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn != null) {
            try {
                conn.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void ResultSetPrint(ResultSet rs) throws SQLException {
        while(rs.next()) {
            System.out.print(rs.getString("id"));
            System.out.print("\t" + rs.getString("name"));
            System.out.println("\t" + rs.getString("dept"));
        }
    }

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName(DBConstant.DRIVER_NAME);
            conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);
            System.out.println("DB Connection is Done...");

            stmt = conn.createStatement();

            // insert
            int rows = stmt.executeUpdate("insert into student (id, name, dept) " +
                    "values('0893012', '์•„๋ฌด๊ฐœ', '์ปดํ“จํ„ฐ๊ณตํ•™');");
            if(rows>0) {
                System.out.println("insert success");
                rs = stmt.executeQuery("select * from student");
                ResultSetPrint(rs);
            } else System.out.println("insert fail");

            // update
            rows = stmt.executeUpdate("update student set id='0189011' where name='์•„๋ฌด๊ฐœ'");
            if(rows>0) {
                System.out.println("update success");
                rs = stmt.executeQuery("select * from student");
                ResultSetPrint(rs);
            } else System.out.println("update fail");

            // delete
            rows = stmt.executeUpdate("delete from student where name='์•„๋ฌด๊ฐœ'");
            if(rows>0) {
                System.out.println("delete success");
                rs = stmt.executeQuery("select * from student");
                ResultSetPrint(rs);
            } else System.out.println("delete fail");
        } catch(ClassNotFoundException | SQLException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        } finally {
            ObjectClose(conn, stmt, rs);
        }
    }
}
  • Statement๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ
import java.sql.*;
import java.util.Scanner;

public class DBConnectExam04 {

    public static void ObjectClose(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
        if(pstmt != null) {
            try {
                pstmt.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn != null) {
            try {
                conn.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void ResultSetPrint(ResultSet rs) throws SQLException {
        while(rs.next()) {
            System.out.print(rs.getString("id"));
            System.out.print("\t" + rs.getString("name"));
            System.out.println("\t" + rs.getString("dept"));
        }
    }

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Scanner sc = new Scanner(System.in);

        try {
            Class.forName(DBConstant.DRIVER_NAME);
            conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);
            String sql = "select * from student where name=?";
            System.out.print("Search name: ");
            String name = sc.next();
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            rs = pstmt.executeQuery();
            ResultSetPrint(rs);
        } catch(ClassNotFoundException | SQLException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        } finally {
            ObjectClose(conn, pstmt, rs);
            sc.close();
        }
    }
}
  • PreparedStatement๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰
import java.sql.*;
import java.util.Scanner;

public class DBConnectExam05 {
    public static void ObjectClose(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
        if(pstmt != null) {
            try {
                pstmt.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn != null) {
            try {
                conn.close();
            } catch(SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void ResultSetPrint(ResultSet rs) throws SQLException {
        while(rs.next()) {
            System.out.print(rs.getString("id"));
            System.out.print("\t" + rs.getString("name"));
            System.out.println("\t" + rs.getString("dept"));
        }
    }

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Class.forName(DBConstant.DRIVER_NAME);
            conn = DriverManager.getConnection(DBConstant.URL, DBConstant.ID, DBConstant.PWD);

            String[] info = {"์ด์„ฑ๊ณ„", "0474012", "๊ตญ๋ฌธ๊ณผ"};
            String sql = "insert into student(name, id, dept) " +
                    "values(?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            for(int i=0; i<info.length; i++) {
                pstmt.setString(i+1, info[i]);
            }
            int rows = pstmt.executeUpdate();
            if(rows>0) {
                System.out.println("insert success");
                rs = pstmt.executeQuery("select * from student");
                ResultSetPrint(rs);
            } else System.out.println("insert fail");

        } catch(ClassNotFoundException | SQLException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        } finally {
            ObjectClose(conn, pstmt, rs);
        }
    }
}
  • PreparedStatement๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ
728x90
๋ฐ˜์‘ํ˜•

'Development Study > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] Lecture - Day 17  (1) 2024.06.06
[Java] Lecture - Day 16  (1) 2024.05.17
[Java] Lecture - Day 14  (0) 2024.04.05
[Java] Lecture - Day 13  (1) 2024.03.30
[Java] Lecture - Day 12  (0) 2024.03.22
Comments