171117 JDBC - RYUDONGJIN/Memo_wiki GitHub Wiki

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

Connection con;//๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •
PreparedStatement pstmt; //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š” ๊ฐ์ฒด
ResultSet rs; //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ด๋ฐ›์•„ ์ž๋ฐ”์— ์ €์žฅํ•ด์ฃผ๋Š” ๊ฐ์ฒด	
  1. JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋“œ - DriverManager Class.forName("oracle.jdbc.driver.OracleDriver");
  2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ - Connection con = DriverManager.getConnection(url, id, pw);
  3. SQL๋ฌธ ์‹คํ–‰ - PreparedStatement pstmt = con.prepareStatement(sql);
  4. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ํ•ด์ œ - ResultSet pstmt.executeUpdate() / rs = pstmt.executeQuery()

Statement ๊ฐ์ฒด

  • executeQuery() : SQL๋ฌธ ์‹คํ–‰ ํ›„ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฒฐ๊ณผ๊ฐ’ ์ƒ๊ธฐ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ select
  • executeUpdate() : SQL๋ฌธ ์‹คํ–‰ ํ›„ ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ๋งŒ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ insert, delete, update

PreparedStatement ๊ฐ์ฒด

  • Statement๊ฐ์ฒด์˜ ์ค‘๋ณต์ฝ”๋“œ ๋‹จ์ ์„ ๋ณด์™„ํ•ด์ค€ ๊ฐ์ฒด (?)์‚ฌ์šฉ

executeQuery() ์‹คํ–‰ ํ›„ ๋ฐ˜ํ™˜๋˜๋Š” ๋ ˆ์ฝ”๋“œ์…‹

  • ResultSet -> next(), previous(), first(), last(), get๋ฉ”์†Œ๋“œ(getString, getInt...)