-
JAVA ์๋ฐ JDBC ๋ฌธ์ ์ฐ์ตJAVA 2020. 8. 19. 22:41
๐ฏ ์๋ฐ JDBC ์ฐ์ต๋ฌธ์ ๋ฅผ ํ์ด๋ณธ๋ค.
0. ๊ธฐ๋ณธ์ธํ
String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String userid = "scott"; String passwd = "tiger"; Connection con= null; ResultSet rs= null; PreparedStatement pstmt= null;
public JDBC_Method_divide_Test() throws ClassNotFoundException, SQLException { Class.forName(driver); }
์ฐ๋์ ์ํ ๊ธฐ๋ณธ ์ ๋ณด๋ฅผ ์ ๋ ฅํ๊ณ ๋๋ผ์ด๋ฒ๋ฅผ ๋ก๋ฉํ๋ค.
1. search ๋ฉ์๋ ๋ง๋ค๊ธฐ
public String search(String loc, String name) { //์ง์ญ๋ง ๋์ด์จ ๊ฒฝ์ฐ ์ง์ญ๋ง ๊ฒ์ ๊ฒฐ๊ณผ ๋ฆฌํด //์ด๋ฆ๋ง ๋์ค์จ ๊ฒฝ์ฐ ์ด๋ฆ๋ง ๊ฒ์ ๊ฒฐ๊ณผ ๋ฆฌํด //์ด๋ฆ, ์ง์ญ์ด ๋์ด์จ ๊ฒฝ์ฐ ๋ ๊ฐ์ง๋ฅผ ์ด์ฉ ๊ฒ์ ๊ฒฐ๊ณผ ๋ฆฌํด }
์ ์ธ๊ฐ์ง ์ํฉ์ ๋ํ ์กฐ๊ฑด๋ฌธ์ ์ฑ์์ผ ํ๋ค. searchํจ์๋ ์ธ์๋ก ์ง์ญ๊ณผ ์ด๋ฆ์ ๋ฐ๋๋ค.
#1 ์ฒซ๋ฒ์งธ if public String search(String loc, String dname) { String data = null; try { con = DriverManager.getConnection(url, userid, passwd); if(dname==null && loc != null) { String sql = "select loc from dept where loc = ?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, loc); rs = pstmt.executeQuery(); while(rs.next()) { String loc1 = rs.getString(1); data = loc1; }
์ง์ญ๊ฐ๋ง ๋ฐ์ ๊ฒฝ์ฐ ๋ฐ์ ์ง์ญ์ ๋ํ ๊ฒ์๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ sql๋ฌธ์ ์์ฑํ๋ค.
#2 ๋๋ฒ์งธ else if }else if(loc == null && dname !=null) { String sql = "select dname from dept where dname = ?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, dname); rs = pstmt.executeQuery(); while(rs.next()) { String dname1 = rs.getString("dname"); data = dname1; }
์ด๋ฆ๋ง ๋ฐ์ ๊ฒฝ์ฐ ๋ฐ์ ํด๋น ์ด๋ฆ์ ๋ํ ๊ฒ์๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
#3 else }else { String sql = "select loc, dname from dept where loc=? and dname=?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, loc); pstmt.setString(2, dname); rs = pstmt.executeQuery(); while(rs.next()) { String loc1 = rs.getString("loc"); String dname1 = rs.getString("dname"); // System.out.println(loc1+"\t"+dname1); data = loc1 +"\t"+ dname1; } }
์ด๋ฆ๊ณผ ์ง์ญ ๋ ๋ค ๋ฐ์ ๊ฒฝ์ฐ ๋ ์ ๋ณด ๋ชจ๋ ๋ฝ์์ฃผ๋ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
#์์ธ์ก๊ธฐ }catch (SQLException e) { e.printStackTrace(); } finally { try { if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); }catch(SQLException e) { e.getStackTrace(); } }return data; }
try๋ฌธ์ด ๋๋ฌ์ผ๋ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๋ค. ์คํ๋ ์ญ์์ผ๋ก ์์์ ๋ฐ๋ฉํ๊ณ ์ต์ข ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ค.
# ๋ฉ์ธํจ์์ ํ๋ฆฐํธ public static void main(String[] args) throws SQLException, ClassNotFoundException { JDBC_Method_divide_Test test= new JDBC_Method_divide_Test(); String result1 = test.search("์ ์ฃผ", null); String result2 = test.search(null, "์์ "); String result3 =test.search("์ ์ฃผ", "์์ "); System.out.println(result1); System.out.println(result2); System.out.println(result3);
์ธ์๋ฆฌ์คํธ๋ฅผ ๋ง์ถฐ์ ์ธ๊ฐ์ง ์กฐ๊ฑด์ ํด๋นํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
2. getAllDept ๋ฉ์๋ ๋ง๋ค๊ธฐ
public String getAllDept(){ //๋ถ์์ ์ฒด select //๊ฒฐ๊ณผ๋ฅผ ๋ฉ์ธ์ผ๋ก ๋ฆฌํด ๋ฉ์ธ์์ ์ ์ฒด ๋ฐ์ดํฐ ์ถ๋ ฅ }
deptํ ์ด๋ธ์ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ ๋ค.
public String getAllDept(){ String data = ""; try { con = DriverManager.getConnection(url, userid, passwd); String sql = "select * from dept"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { int deptno = rs.getInt(1); String dname = rs.getString(2); String loc = rs.getString(3); data += deptno + "\t" + dname + "\t" + loc +"\n"; } } catch (SQLException e) { e.printStackTrace(); } finally { try { if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); }catch(SQLException e) { e.getStackTrace(); } }return data;
#๋ฉ์ธ System.out.println(test.getAllDept());
๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฆฌํดํ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค๊ณ ๋ฉ์ธ์์ ํธ์ถํ๋ค.
# ArrayList ์ฌ์ฉ public ArrayList<String> getAllDept(){ ArrayList<String> list = new ArrayList<String>(); try { con = DriverManager.getConnection(url, userid, passwd); String sql = "select * from dept"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { String data = ""; data += rs.getInt(1); data += rs.getString(2); data += rs.getString(3); data += "\n"; list.add(data); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); }catch(SQLException e) { e.getStackTrace(); } }return list; }
ArrayList<String> list = test.getAllDept(); for(String x : list) { System.out.println(x); }
ArrayList๋ฅผ ์ฌ์ฉํด์ ํจ์๋ฅผ ๋ง๋ค์ด๋ณธ๋ค. selectํ ๋ฐ์ดํฐ๋ฅผ ๋ฆฌ์คํธ์ ์ถ๊ฐํ๊ณ ๋ฆฌ์คํธ๋ฅผ ๋ฆฌํดํ๋ค. ๋ฉ์ธํจ์ ์์์ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํด ๋ฆฌ์คํธ์ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ๋ค.
3. searchDeptByLoc ๋ฉ์๋ ๋ง๋ค๊ธฐ
public String searchDeptByLoc(String loc){ //์ฃผ์๋ก select //๊ฒ์ํ ๋ด์ฉ์ ? ๋ก ๋ฆฌํด ๋ฉ์ธ์์ ์ ์ฒด ๋ฐ์ดํฐ ์ถ๋ ฅ }
์ง์ญ๊ฐ์ ๋ฐ์์ ๊ฒ์๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ ํจ์๋ฅผ ๋ง๋ ๋ค.
public String searchDeptByLoc(String loc){ String data = ""; try { con = DriverManager.getConnection(url, userid, passwd); String sql = "select * from dept where loc=?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, loc); rs = pstmt.executeQuery(); while (rs.next()) { int deptno = rs.getInt(1); String dname = rs.getString(2); String loc1 = rs.getString(3); data += deptno + "\t" + dname + "\t" + loc1 +"\n"; } } catch (SQLException e) { e.printStackTrace(); }finally { try { if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); }catch(SQLException e) { e.getStackTrace(); } } return data; }
System.out.println(test.searchDeptByLoc("์ ์ฃผ"));
4. searchDeptByName ๋ฉ์๋ ๋ง๋ค๊ธฐ
public String searchDeptByName(String dname){ //๋ถ์์ด๋ฆ์ผ๋ก๋ก select //๊ฒ์ํ ๋ด์ฉ์ ? ๋ก ๋ฆฌํด ๋ฉ์ธ์์ ์ ์ฒด ๋ฐ์ดํฐ ์ถ๋ ฅ }
๋ถ์ ์ด๋ฆ์ผ๋ก ๋ฐ์์ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํดํ๋ ํจ์๋ฅผ ๋ง๋ ๋ค.
public String searchDeptByName(String dname){ String data = ""; try { con = DriverManager.getConnection(url, userid, passwd); String sql = "select * from dept where dname=?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, dname); rs = pstmt.executeQuery(); while (rs.next()) { int deptno = rs.getInt(1); String dname1 = rs.getString(2); String loc1 = rs.getString(3); data += deptno + "\t" + dname1 + "\t" + loc1 +"\n"; } } catch (SQLException e) { e.printStackTrace(); }finally { try { if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); }catch(SQLException e) { e.getStackTrace(); } } return data; }
System.out.println(test.searchDeptByName("๊ฐ๋ฐ"));
5. updateDept ๋ฉ์๋ ๋ง๋ค๊ธฐ
public int updateDept(String dname, String loc, int deptno){ //๋ถ์๋ฒํธ๋ก dname, loc์ ๋ฐ์ดํธ ํ //์ ๋ฐ์ดํธ ๊ฒฐ๊ณผ ๊ฐฏ์ ๋ฆฌํด }
ํน์ ๋ถ์๋ฒํธ์ ํด๋นํ๋ ๋ถ์๋ช ๊ณผ ์ง์ญ์ updateํ๋ ํจ์๋ฅผ ๋ง๋ ๋ค.
public int updateDept(String dname, String loc, int deptno){ int num = 0; try { con = DriverManager.getConnection(url, userid, passwd); String sql = "update dept set dname=?, loc=? where deptno=?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, dname); pstmt.setString(2, loc); pstmt.setInt(3, deptno); num = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); }catch(SQLException e) { e.getStackTrace(); } }return num; }
selec๋ฌธ๊ณผ ๋ค๋ฅด๊ฒ ์คํ๊ฒฐ๊ณผ ๊ฐฏ์๋ง ๋ฆฌํดํ๋ค.
System.out.println(test.updateDept("์์ ", "ํ๊ต", 80));
6. insertDept ๋ฉ์๋ ๋ง๋ค๊ธฐ
public int insertDept(String dname, String loc, int deptno){ //๋ถ์ํ๋ ์ถ๊ฐํ //insert ๊ฒฐ๊ณผ ๊ฐฏ์ ๋ฆฌํด }
๋ถ์๋ฒํธ์ ๋ถ์๋ช , ์ง์ญ๋ช ์ ๋ฐ์์ ์๋ก์ด ๋ ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ ๋ฉ์๋๋ฅผ ๋ง๋ ๋ค.
public int insertDept(String dname, String loc, int deptno){ int num = 0; try { con = DriverManager.getConnection(url, userid, passwd); String sql = "insert into dept (deptno, dname, loc) values(?,?,?)"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, deptno); pstmt.setString(2, dname); pstmt.setString(3, loc); num = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); }catch(SQLException e) { e.getStackTrace(); } }return num; }
System.out.println(test.insertDept("๊ฒฝ์", "๊ฐ๋จ", 88));
JDBC Dept DTO์ฐ์ต
// Deptํ ์ด๋ธ์ ํ๋์ ๋ ์ฝ๋ ์ ์ฅ์ฉ๋ package com.dto; public class Dept { private int deptno; private String dname; private String loc; public Dept() {} public Dept(int deptno, String dname, String loc) { this.deptno = deptno; this.dname = dname; this.loc = loc; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } @Override public String toString() { return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]"; } }
Deptํ ์ด๋ธ์ ํ๋์ ๋ชจ๋ธ ํด๋์ค๋ก ๋ง๋ ๋ค.
//์ฌ์ฉ์์ ์ ์์ธํด๋์ค package com.exception; public class RecordNotFoundException extends Exception { public RecordNotFoundException(String mesg) { super(mesg); } }
์์ธํด๋์ค๋ฅผ ํ๋ ๋ง๋ ๋ค.
package com.service; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import com.dao.OracleTxDAO; import com.dto.Dept; import com.exception.RecordNotFoundException; public class OracleTxService { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String userid = "scott"; String passwd = "tiger"; OracleTxDAO dao; public OracleTxService() { dao = new OracleTxDAO(); // ์ ์ฒด์์ ์จ์ผํ๋๊น ๋ฉค๋ฒ๋ณ์๋ก ์์ฑ try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } }// end OracleDAO public ArrayList<Dept> select() throws SQLException { Connection con = null; ArrayList<Dept> list = null; try { con = DriverManager.getConnection(url, userid, passwd); list = dao.select(con); //์ปค๋ฅ์ ์ ์๋น์ค์์ ์ฒ๋ฆฌํ๊ธฐ๋ก ํ์ผ๋๊น ์ฒ๋ฆฌ๋๊ฑฐ dao๋ก ๋ณด๋ด } finally { //์ฌ๊ธฐ์๋ ์์ธ์์ก์. ์์๋ฐ๋ฉ๋ง ์ฒ๋ฆฌ. if (con != null) con.close(); } return list; }// end select public void insert(Dept xx) { Connection con = null; try { con = DriverManager.getConnection(url, userid, passwd); dao.insert(con, xx); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }// end insert public void update(Dept xx2) throws RecordNotFoundException { Connection con = null; try { con = DriverManager.getConnection(url, userid, passwd); dao.update(con, xx2); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }// end update public void delete(int i) { Connection con = null; try { con = DriverManager.getConnection(url, userid, passwd); dao.delete(con, i); } catch (SQLException e) { System.out.println(e.getMessage()); // e.printStackTrace(); } finally { try { if (con != null) con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }// end deletes }
์๋น์ค ํด๋์ค๋ฅผ ๋ง๋ ๋ค. ์๋น์ค ํด๋์ค์์๋ ์ฃผ๋ก ์ปค๋ฅ์ ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ค.
package com.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.dto.Dept; import com.exception.RecordNotFoundException; public class OracleTxDAO { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String userid = "scott"; String passwd = "tiger"; public void update(Connection con, Dept xx) throws RecordNotFoundException { PreparedStatement pstmt = null; try { String sql = "update dept set " + " dname=? , loc=? where deptno=?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, xx.getDname()); pstmt.setString(2, xx.getLoc()); pstmt.setInt(3, xx.getDeptno()); int num = pstmt.executeUpdate(); System.out.println("์คํ๋ ๋ ์ฝ๋ ๊ฐฏ์:" + num); if (num == 0) { throw new RecordNotFoundException(xx.getDeptno() + " ๋ ์ฝ๋์๋ค."); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } }// end upate public void delete(Connection con, int i) { PreparedStatement pstmt = null; try { String sql = "delete from dept where deptno=?"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, i); int num = pstmt.executeUpdate(); System.out.println("์คํ๋ ๋ ์ฝ๋ ๊ฐฏ์:" + num); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } public void insert(Connection con, Dept xx) { PreparedStatement pstmt = null; try { String sql = "insert into dept (deptno,dname,loc)" + " values (?,?,? )"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, xx.getDeptno()); pstmt.setString(2, xx.getDname()); pstmt.setString(3, xx.getLoc()); int num = pstmt.executeUpdate(); System.out.println("์คํ๋ ๋ ์ฝ๋ ๊ฐฏ์:" + num); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } }// end insert public ArrayList<Dept> select(Connection con) throws SQLException { ArrayList<Dept> list = new ArrayList<Dept>(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select deptno x,dname,loc from dept "; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { int deptno = rs.getInt("x"); String dname = rs.getString(2); String loc = rs.getString("loc"); Dept dto = new Dept(deptno, dname, loc); list.add(dto); } if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); return list; }// end select }
DAOํด๋์ค์์ ์๋น์ค ํด๋์ค์์ ๋ฐ์์จ ์ปค๋ฅ์ ์ ์ฌ์ฉํด ๊ฐ ํจ์์ ํด๋นํ๋ SQL๋ฌธ์ ์์ฑํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ์ธ์ผ๋ก ๋ฆฌํดํ๋ค.
//main import java.sql.SQLException; import java.util.ArrayList; import com.dto.Dept; import com.exception.RecordNotFoundException; import com.service.OracleTxService; public class OracleTxMain { public static void main(String[] args) { OracleTxService service = new OracleTxService(); //4๋ฒ service.delete(99); // 3๋ฒ Dept xx2 = new Dept(99, "์์ ", "์์ธ"); try { service.update(xx2); } catch (RecordNotFoundException e1) { System.out.println(e1.getMessage()); } // 2๋ฒ int deptno = 99; String dname = "๊ฐ๋ฐ"; String loc = "์ ์ฃผ"; Dept xx = new Dept(deptno, dname, loc); service.insert(xx); // 1๋ฒ try { ArrayList<Dept> list = service.select(); for (Dept dept : list) { System.out.println(dept); } } catch (SQLException e) { e.printStackTrace(); } } }
๋ฉ์ธ์์ ์๋น์ค ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
select()ํจ์๋ง ํ๋ํ๋ ๋ฏ์ด๋ณด๊ธฐ
public OracleTxService() { dao = new OracleTxDAO(); try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); }// end OracleDAO }
์๋น์ค ํด๋์ค์์ ์์ฑ์๋ฅผ ๋จผ์ ๋ง๋ ๋ค. ์๋น์ค ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด์ dao ํด๋์ค๋ ์ธ ์ ์๊ฒ ๊ฐ์ฒด์์ฑ ํด์ฃผ๊ณ ๋๋ผ์ด๋ฒ๋ฅผ ๋ก๋ฉํ๋ค.
public ArrayList<Dept> select() throws SQLException { //db์ฐ๊ฒฐ Connection con = null; ArrayList<Dept> list = null; try { con = DriverManager.getConnection(url, userid, passwd); list = dao.select(con); } finally { if(con!=null)con.close(); } return list; }// end select
select ํจ์ ์์์ ์ปค๋ฅ์ ์ฐ๊ฒฐํด์ฃผ๊ณ , ์ด๋ ์ด๋ฆฌ์คํธ๋ฅผ ๋ง๋ ๋ค. ๋ฆฌ์คํธ์ dao์ ์ ๋ ํธํจ์ ํธ์ถํ๋ฉด์ ์ปค๋ฅ์ ์ฐ๊ฒฐ์ ๋ด์์ ๋ณด๋ด์ฃผ๊ณ ์์๋ฐ๋ฉ ํด์ฃผ๊ณ ๋ฆฌ์คํธ ๋ฆฌํดํ๊ณ ํจ์๋ฅผ ์ข ๋ฃํ๋ค.
์ด์ dao๋ก ๊ฐ๋ค.
public ArrayList<Dept> select(Connection con)throws SQLException { ArrayList<Dept> list = new ArrayList<Dept>(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select deptno x, dname, loc from dept"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()) { int deptno = rs.getInt("x"); String dname = rs.getString("dname"); String loc = rs.getString(3); Dept dto = new Dept(deptno, dname, loc); list.add(dto); }if(rs != null) rs.close(); if(pstmt!=null) pstmt.close(); return list; }//end select }
์ ๋ ํจ์ ์ธ์๋ก con์ ๋ฐ๋๋ค. ๋จผ์ ์ด๋ ์ด๋ฆฌ์คํธ ๊ฐ์ฒด ์์ฑํด์ฃผ๊ณ con์ ๋ฐ์์์ผ๋๊น ๋๋จธ์ง pstmt, rs๋ฅผ ์ธํ ํ๋ค. ์ํ๋ฌธ ์์ฑํ๊ณ , pstmt๋ก ์ฟผ๋ฆฌ๋ฌธ ์คํํ๊ณ , ์คํ๋ ๊ฑธ rs์ ์ ์ฅํด์ฃผ๊ณ , ๋ฐ๋ณต๋ฌธ ๋๋ ค์ Deptํด๋์ค์ ์์ฑ์๋ฅผ ํธ์ถํด์ dto๋ผ๋ ๋ณ์์ ๋ฃ์ด์ค๋ค. ๊ทธ๋ ๊ฒ ๋ถ๋ฌ์จ ์ ๋ณด๋ฅผ ๋ฆฌ์คํธ์ ์ถ๊ฐํ ํ ์์๋ฐ๋ฉํ๊ณ ๋ฆฌ์คํธ ๋ฆฌํดํ๊ณ ํจ์๋ฅผ ์ข ๋ฃํ๋ค. ์ด์ ๋ฆฌํด๋ ๋ฆฌ์คํธ๋ ์ด ํจ์๋ฅผ ํธ์ถํ ์๋น์ค ํด๋์ค๋ก ๊ฐ๋ค๊ฐ ๋ฉ์ธ์ผ๋ก ๊ฐ๊ฒ๋๋ค.
์ด์ ๋ฉ์ธ์ผ๋ก ๊ฐ๋ค.
OracleTxService service = new OracleTxService();
๋ฉ์ธ์์๋ ์๋น์ค ํด๋์ค๋ง ์ฌ์ฉํ๋ฉด ๋๋๊น ์๋น์ค ๊ฐ์ฒด๋ฅผ ๋จผ์ ์์ฑํ๋ค.
try { ArrayList<Dept> list = service.select(); for(Dept dept : list) { System.out.println(dept); } } catch (SQLException e) { e.printStackTrace(); }
์ด์ ์ด๋ ์ด๋ฆฌ์คํธ ํ์ ์ผ๋ก ์๋น์ค์ ์ ๋ ํธํจ์๋ฅผ ํธ์ถํด์ฃผ๊ณ ๋ฐ์์จ ๋ฆฌ์คํธ๋ฅผ ๋ฐ๋ณต๋ฌธ ๋๋ ค์ ๋ค ์ถ๋ ฅํ๋ค. ์์ธ๋ฅผ ์ก๊ณ ์ ๋ ํธ ํจ์ ์คํ์ ๋๋๋ค. ๋ฉ์ธ์์๋ ํจ์ ํธ์ถ๋ง ํ๊ณ ์์๋ฐ๋ฉ์ ์๋น์ค๋ dao์์ ๋ค ์ฒ๋ฆฌํด์คฌ๊ธฐ๋๋ฌธ์ ์์ธ๋ง ์ก๊ณ ๊ฐ๋จํ๊ฒ ๋๋๋ค.
๐ NEXT : JDBC ๋ฌธ์ ๋ ์ฐ์ตํด๋ณด๊ธฐ
๋ฐ์ํ'JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JAVA ์๋ฐ ๋ง์ด๋ฐํฐ์ค mybatis ์ฌ์ฉํ๊ธฐ (0) 2020.08.21 JAVA ์๋ฐ JDBC ์ฐ์ต - ์ํ์ ๋ณด ์กฐํํ๊ธฐ (0) 2020.08.20 JAVA ์๋ฐ JDBC ์ค๋ผํด DB ์ฐ๋ (0) 2020.08.18 JAVA ์๋ฐ ์ ์ถ๋ ฅ I/O, ํ์ผ์ฒ๋ฆฌ (0) 2020.08.17 JAVA ์๋ฐ ํต์ฌํด๋์ค (0) 2020.08.16