-
JAVA ์๋ฐ JDBC ์ฐ์ต - ์ํ์ ๋ณด ์กฐํํ๊ธฐJAVA 2020. 8. 20. 09:08
๐ฏ ์๋ฐ JDBC ์ฐ์ต๋ฌธ์ ๋ฅผ ํ์ด๋ณธ๋ค.
JDBC ์ํ์ ๋ณด ์กฐํํ๊ธฐ
JDBC๋ฅผ ์ด์ฉํ์ฌ ๋งค์ถ์ผ์์, ์ํ๋ณ ๋งค์ถ์ ๋ฐ์ดํฐ ์กฐํ๋ฅผ ํ๋๋ก ๊ตฌํํ๋ค.
1. SQL Developer์์ test ๊ณ์ ๋ง๋ค๊ธฐ
create user test IDENTIFIED by test; alter user test account unlock; grant connect, resource to test;
sys์์ ๋ช ๋ น๋ฌธ์ ์คํํ๊ณ ์ ๊ณ์ ์ ์์ฑํ๊ณ ์ ์ํ๋ค.
2. ํ ์ด๋ธ ๋ง๋ค๊ธฐ
DROP TABLE D7_ORDER; DROP TABLE D7_PRODUCT; CREATE TABLE D7_PRODUCT( PID VARCHAR(20) PRIMARY KEY, PNAME VARCHAR(50) NOT NULL, PRICE NUMBER NOT NULL ); CREATE TABLE D7_ORDER( OID VARCHAR(10) PRIMARY KEY, PID VARCHAR(20) NOT NULL, QUANTITY NUMBER NOT NULL, ODATE DATE NOT NULL, CONSTRAINT FK_ORDER_PRODUCT FOREIGN KEY (PID) REFERENCES D7_PRODUCT (PID) ); INSERT INTO D7_PRODUCT VALUES('P001', '์ด์๋ น์ค', 23000); INSERT INTO D7_PRODUCT VALUES('P002', '๋ฆฌ์', 8800); INSERT INTO D7_PRODUCT VALUES('P003', '์ฃฝ์ผ์น์ฝ', 5500); INSERT INTO D7_PRODUCT VALUES('P004', 'ํ๋ฆฌ์ค', 3400); INSERT INTO D7_PRODUCT VALUES('P005', '์จ๋๋ฐ๋', 9900); INSERT INTO D7_PRODUCT VALUES('P006', '์คํ๋', 11500); INSERT INTO D7_PRODUCT VALUES('P007', 'ํ ํฌ', 9500); INSERT INTO D7_PRODUCT VALUES('P008', '๋๋ด', 2000); INSERT INTO D7_PRODUCT VALUES('P009', '์๋ผ์คํด', 15000); INSERT INTO D7_PRODUCT VALUES('P010', '์กฐ์ง์', 1000); INSERT INTO D7_PRODUCT VALUES('P011', '์กฐ์ง์-๋ธ๋ฃจ', 1500); INSERT INTO D7_PRODUCT VALUES('P012', 'ํจ์ฌ์ด๋ค', 1000); INSERT INTO D7_PRODUCT VALUES('P013', '์ฝ์นด์ฝ๋ผ', 1500); INSERT INTO D7_PRODUCT VALUES('P014', '์คํ๋ผ์ดํธ', 2000); INSERT INTO D7_PRODUCT VALUES('P015', 'ํํ', 1000); INSERT INTO D7_PRODUCT VALUES('P016', '์จ', 25000); INSERT INTO D7_PRODUCT VALUES('P017', '์๋ คํ', 55000); INSERT INTO D7_ORDER VALUES('O-001', 'P001', 3, '2014/01/04'); INSERT INTO D7_ORDER VALUES('O-002', 'P004', 1, '2014/01/04'); INSERT INTO D7_ORDER VALUES('O-003', 'P001', 10, '2014/01/04'); INSERT INTO D7_ORDER VALUES('O-004', 'P001', 30, '2014/01/05'); INSERT INTO D7_ORDER VALUES('O-005', 'P011', 2, '2014/01/05'); INSERT INTO D7_ORDER VALUES('O-006', 'P012', 1, '2014/01/05'); INSERT INTO D7_ORDER VALUES('O-007', 'P011', 3, '2014/01/06'); INSERT INTO D7_ORDER VALUES('O-008', 'P012', 13, '2014/01/07'); INSERT INTO D7_ORDER VALUES('O-009', 'P013', 3, '2014/01/07'); INSERT INTO D7_ORDER VALUES('O-010', 'P014', 2, '2014/01/07'); INSERT INTO D7_ORDER VALUES('O-011', 'P014', 2, '2014/01/08'); INSERT INTO D7_ORDER VALUES('O-012', 'P001', 1, '2014/01/08'); INSERT INTO D7_ORDER VALUES('O-013', 'P007', 4, '2014/01/08'); INSERT INTO D7_ORDER VALUES('O-014', 'P008', 5, '2014/01/08'); INSERT INTO D7_ORDER VALUES('O-015', 'P001', 10, '2014/01/20'); INSERT INTO D7_ORDER VALUES('O-016', 'P007', 20, '2014/01/20'); INSERT INTO D7_ORDER VALUES('O-017', 'P014', 5, '2014/01/20'); INSERT INTO D7_ORDER VALUES('O-018', 'P013', 3, '2014/01/21'); INSERT INTO D7_ORDER VALUES('O-019', 'P007', 2, '2014/02/02'); INSERT INTO D7_ORDER VALUES('O-020', 'P008', 1, '2014/02/02'); INSERT INTO D7_ORDER VALUES('O-021', 'P008', 1, '2014/02/04'); INSERT INTO D7_ORDER VALUES('O-022', 'P009', 1, '2014/02/04'); INSERT INTO D7_ORDER VALUES('O-023', 'P001', 5, '2014/02/04'); INSERT INTO D7_ORDER VALUES('O-024', 'P012', 5, '2014/03/04'); INSERT INTO D7_ORDER VALUES('O-025', 'P001', 2, '2014/03/04'); COMMIT; SELECT TO_CHAR(ODATE, 'YYYY-MM-DD') ์์ ์ผ, SUM(O.QUANTITY*P.PRICE) ๋งค์ถ FROM D7_ORDER O, D7_PRODUCT P GROUP BY ODATE ORDER BY 1 ; SELECT PNAME ์ํ๋ช , SUM(O.QUANTITY*P.PRICE) ๋งค์ถ FROM D7_ORDER O, D7_PRODUCT P GROUP BY PNAME ORDER BY 2 DESC ;
test ๊ณ์ ์ ์ ์ฝ๋๋ฅผ ๋ถ์ฌ๋ฃ๊ณ ์คํํ๋ค. ์ฌ๋ฌ๊ฐ์ ํ ์ด๋ธ์ด ์์ฑ๋๋ค.
3. ์ดํด๋ฆฝ์ค์์ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String userid = "test"; String passwd = "test"; Connection con= null; ResultSet rs= null; PreparedStatement pstmt= null;
๊ธฐ๋ณธ์ธํ
try { Class.forName(driver); con = DriverManager.getConnection(url, userid, passwd); String sql = "";
try๋ฌธ ์์ํ๊ณ ๋๋ผ์ด๋ฒ ๋ก๋ฉํ ๋ค์ sql ๋ณ์๋ฅผ ์ ์ธํ๋ค.
Scanner scan = new Scanner(System.in); System.out.print("๋งค์ถ ์กฐํ ๋ฉ๋ด - [ ๋งค์ถ ์ผ์์ : 1, ์ํ๋ณ ๋งค์ถ์ : 2 ]: "); int menu = scan.nextInt();
๋๊ฐ์ง์ ๋งค์ถ ์กฐํ ๋ฐฉ๋ฒ์ ์ ํํ ์ ์๋๋ก ์ค์บ๋๋ฅผ ์์ฑํด ์ซ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค.
if(menu ==1) { sql = "SELECT TO_CHAR(ODATE, 'YYYY-MM-DD') ์์ ์ผ, SUM(O.QUANTITY*P.PRICE) ๋งค์ถ " + "FROM D7_ORDER O JOIN D7_PRODUCT P USING(PID) GROUP BY ODATE ORDER BY 1"; }
์ซ์๊ฐ 1๋ฒ์ผ ๊ฒฝ์ฐ ๋งค์ถ ์ผ์์์ผ๋ก ์์ ์ผ๊ณผ ๋งค์ถ์ ์กฐํํ ์ ์๋๋ก ์ฟผ๋ฆฌ๋ฌธ์ ์์ฑํ๋ค. ANSI ๋ค์ธ๋ด ์กฐ์ธ๋ฐฉ๋ฒ์ USING์ ์ ์ฌ์ฉํด ๊ณตํต์ปฌ๋ผ์ ๋ช ์ํ๋ค.
์ซ์๋ฅผ char๋ก ํ๋ณํํ ์ ์๋ TO_CHAR(์ฌ์ฉํ ๋ฐ์ดํฐ, ์ํ๋ ์ถ๋ ฅ ํํ) ํจ์๋ฅผ ์ฌ์ฉํด ODATE ์ ๋ณด๋ฅผ ์ ๋ ฅํ๊ณ ๋ณ์นญ์ ์์ ์ผ๋ก ์ง์ ํ๋ค. ๋งค์ถ์ ๋ณด๋ ์ฃผ๋ฌธ์๋๊ณผ ์ํ๊ฐ๊ฒฉ์ ๊ณฑํด SUMํจ์๋ก ํฉ๊ณ๋ฅผ ๊ตฌํ๋ค.
else if(menu ==2) { sql = "SELECT PNAME ์ํ๋ช , SUM(O.QUANTITY*P.PRICE) ๋งค์ถ " + "FROM D7_ORDER O JOIN D7_PRODUCT P USING(PID) GROUP BY PNAME ORDER BY 2 DESC"; }
์ซ์๊ฐ 2๋ฒ์ผ ๊ฒฝ์ฐ ์ํ๋ณ ๋งค์ถ์์ผ๋ก ์ํ๋ช ๊ณผ ๋งค์ถ์ ์กฐํํ ์ ์๋๋ก ์ฟผ๋ฆฌ๋ฌธ์ ์์ฑํ๋ค.
pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); System.out.println("--------------------------------------"); System.out.println(((menu==1)?"์์ ์ผ":"์ํ๋ช ")+"\t\t\t๋งค์ถ"); System.out.println("--------------------------------------"); while (rs.next()) { System.out.println(rs.getString(1) + "\t\t" + rs.getString(2)); }
(menu==1)?"์์ ์ผ":"์ํ๋ช "
์กฐ๊ฑด๋ฌธ ๋์ ๊ฐ๋จํ ์ผํญ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ค. ๊ดํธ ์ ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์ฐ ?๊ธฐํธ ๋ค์ ์ฒซ์๋ฆฌ ๊ฐ์ ์คํํ๊ณ ๊ฑฐ์ง์ผ ๊ฒฝ์ฐ ๋๋ฒ์งธ ์๋ฆฌ ๊ฐ์ ์คํํ๋ค. ๋ฐ๋ผ์ menu๊ฐ์ด 2์ผ ๊ฒฝ์ฐ์ ์ํ๋ช ์ ์ถ๋ ฅํ๊ฒ ๋๋ค.
} catch (ClassNotFoundException e) { e.printStackTrace(); }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.printStackTrace(); } }
์์ธ์ฒ๋ฆฌํ๊ธฐ
#์ ์ฒด ์ฝ๋ ๋ณด๊ธฐ import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class workshop08 { public static void main(String[] args) throws SQLException { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String userid = "test"; String passwd = "test"; Connection con= null; ResultSet rs= null; PreparedStatement pstmt= null; try { Class.forName(driver); con = DriverManager.getConnection(url, userid, passwd); String sql = ""; Scanner scan = new Scanner(System.in); System.out.print("๋งค์ถ ์กฐํ ๋ฉ๋ด - [ ๋งค์ถ ์ผ์์ : 1, ์ํ๋ณ ๋งค์ถ์ : 2 ]: "); int menu = scan.nextInt(); if(menu ==1) { sql = "SELECT TO_CHAR(ODATE, 'YYYY-MM-DD') ์์ ์ผ, SUM(O.QUANTITY*P.PRICE) ๋งค์ถ " + "FROM D7_ORDER O JOIN D7_PRODUCT P USING(PID) GROUP BY ODATE ORDER BY 1"; }else if(menu ==2) { sql = "SELECT PNAME ์ํ๋ช , SUM(O.QUANTITY*P.PRICE) ๋งค์ถ " + "FROM D7_ORDER O JOIN D7_PRODUCT P USING(PID) GROUP BY PNAME ORDER BY 2 DESC"; } pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); System.out.println("--------------------------------------"); System.out.println(((menu==1)?"์์ ์ผ":"์ํ๋ช ")+"\t\t\t๋งค์ถ"); System.out.println("--------------------------------------"); while (rs.next()) { System.out.println(rs.getString(1) + "\t\t" + rs.getString(2)); } System.out.println("--------------------------------------"); } catch (ClassNotFoundException e) { e.printStackTrace(); }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.printStackTrace(); } } }
EMP_JDBC ํ๋ก์ ํธ
package com.entity; public class EmpDTO { private int empno; private String ename; private String job; private int mgr; private String hiredate; private double sal; private double comm; private int deptno; public EmpDTO() {} public EmpDTO(int empno, String ename, String job, int mgr, String hiredate, double sal, double comm, int deptno) { this.empno = empno; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.deptno = deptno; } @Override public String toString() { return "EmpDTO [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]"; } public int getEmpno() { return empno; } public void setEmpno(int empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getMgr() { return mgr; } public void setMgr(int mgr) { this.mgr = mgr; } public String getHiredate() { return hiredate; } public void setHiredate(String hiredate) { this.hiredate = hiredate; } public double getSal() { return sal; } public void setSal(double sal) { this.sal = sal; } public double getComm() { return comm; } public void setComm(double comm) { this.comm = comm; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } }
์ด๋ฒ์ scott๊ณ์ ์ empํ ์ด๋ธ๋ก ์ฐ์ตํด๋ณธ๋ค. empํ ์ด๋ธ์ ๋ชจ๋ธ ํด๋์ค๋ก ์ฌ์ฉํ DTO ํด๋์ค๋ฅผ ๋ง๋ ๋ค.
package com.exception; public class DataNotFoundException extends Exception { public DataNotFoundException(String mesg) { super(mesg); } }
์์ธ๋ฅผ ์ฒ๋ฆฌํ ์ฌ์ฉ์์ ์ ์์ธํด๋์ค๋ฅผ ๋ง๋ ๋ค.
package com.common.util; import java.sql.*; public class JdbcTemplate { // ๋๋ผ์ด๋ฒ ๋ก๋ฉ ๋ฐ db์ฐ๊ฒฐ, con๋ฆฌํด, con.close() public static String driver = "oracle.jdbc.driver.OracleDriver"; public static String url = "jdbc:oracle:thin:@localhost:1521:orcl"; public static String userid = "scott"; public static String passwd = "tiger"; public JdbcTemplate() { // ๋๋ผ์ด๋ฒ๋ก๋ฉ try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() { // connect์ฐ๊ฒฐ ํ tx(ํธ๋์ญ์ )๋ฅผ false๋ก ์ค์ ๋ฆฌํด con.setAutoCommit(false); Connection conn = null; try { conn = DriverManager.getConnection(url, userid, passwd); conn.setAutoCommit(false); // ์์ ํ ๋ฆฌํด } catch (Exception e) { System.out.println("[JdbcTemplate.getConnection] : " + e.getMessage()); e.getStackTrace(); } return conn; // connection ๋ฆฌํด } public static void close(Connection conn) { if (isConnected(conn)) try { if (isConnected(conn)) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public static boolean isConnected(Connection conn) { boolean validConnection = true; // conn ์ด null ์ด๊ฑฐ๋ conn.isClose()๊ฐ ์ฐธ์ด๋ฉด validConnection์ false ๋ก ์ค์ try { if (conn == null || conn.isClosed()) validConnection = false; } catch (SQLException e) { validConnection = false; e.printStackTrace(); } return validConnection; } public static void close(PreparedStatement pstmt) { // dao์์ pstmt๋ฅผ ๋๊ฒจ์ค์ PreparedStatement๋ฅผ close() //try-catch ์ฒ๋ฆฌ try { if (pstmt != null) { pstmt.close(); } } catch (SQLException e) { e.getStackTrace(); } } public static void close(ResultSet rset) { //// dao์์ pstmt๋ฅผ ๋๊ฒจ์ค์ PreparedStatement๋ฅผ close() close - try - catch ์ฒ๋ฆฌ try { if (rset != null) rset.close(); } catch (SQLException e) { e.getStackTrace(); } } public static void commit(Connection conn) { try { if (isConnected(conn)) { conn.commit(); System.out.println("[JdbcTemplate.commit] : DB Successfully Committed!"); } } catch (SQLException e) { e.printStackTrace(); } } public static void rollback(Connection conn) { try { if (isConnected(conn)) { conn.rollback(); System.out.println("[JdbcTemplate.rollback] : DB Successfully Rollbacked!"); } } catch (SQLException e) { e.printStackTrace(); } } }
ํ ํ๋ฆฟ ํด๋์ค๋ ํ์ํ ์ปค๋ฅ์ ์ฐ๊ฒฐ๊ณผ ์์๋ฐ๋ฉ, ์ปค๋ฐ ๋๋ ๋กค๋ฐฑ ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ค.
package com.biz; import java.sql.Connection; import java.util.ArrayList; import com.common.util.JdbcTemplate; import com.dao.EmpDAO; import com.entity.EmpDTO; import com.exception.DataNotFoundException; public class EmpBiz { EmpDAO dao; public EmpBiz() { dao = new EmpDAO(); // ํด๋์ค ํ์ ์ง์ x ์ง์ ํ๊ฒ ๋๋ฉด ์ด ์์์๋ง ์ฌ์ฉํ๊ฒ๋๋ค } public ArrayList<EmpDTO> selectAllEmp() { // JdbcTemplate.getConnection()์์ con ์ป๊ธฐ Connection con = JdbcTemplate.getConnection(); // dao.selectAllEmp(con); ํธ์ถ con ์ ๋ฌ ArrayList<EmpDTO> list = dao.selectAllEmp(con); // dbcTemplate ์ด์ฉ close(con)์ผ๋ก ์ปค๋ฅ์ ๋๊ธฐ JdbcTemplate.close(con); return list; } public EmpDTO selectDetailEmp(String eno) throws DataNotFoundException { // JdbcTemplate.getConnection()์์ con ์ป๊ธฐ Connection con = JdbcTemplate.getConnection(); // dao.selectDetailEmp(con); ํธ์ถ EmpDTO dto = dao.selectDetailEmp(con, eno); // dbcTemplate ์ด์ฉ close JdbcTemplate.close(con); return dto; } public void empUpdate(EmpDTO empDTO) throws DataNotFoundException { Connection con = JdbcTemplate.getConnection(); dao.empUpdate(con, empDTO); JdbcTemplate.commit(con); JdbcTemplate.close(con); } public void empDelete(String empno) throws DataNotFoundException { Connection con = JdbcTemplate.getConnection(); dao.empDelete(con, empno); JdbcTemplate.commit(con); JdbcTemplate.close(con); } }
๋น์ฆ ํด๋์ค๋ ํ ํ๋ฆฟ ํด๋์ค์์ ์ปค๋ฅ์ ์ ์ป์ด์ค๊ณ ์ปค๋ฅ์ ์ DAO ํด๋์ค์ ๋๊ฒจ์ฃผ๋ ์ญํ ์ ํ๋ค.
package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.common.util.JdbcTemplate; import com.entity.EmpDTO; import com.exception.DataNotFoundException; public class EmpDAO { public ArrayList<EmpDTO> selectAllEmp(Connection con) { // select ํ jdbcTemplate ์ด์ฉ close ArrayList<EmpDTO> list = new ArrayList<>(); PreparedStatement pstmt = null; ResultSet rs = null; try { String sql = "SELECT EMPNO, ENAME, JOB, MGR, TO_CHAR(HIREDATE, 'YYYY-MM-DD') HIREDATE, " + "SAL, COMM, DEPTNO FROM EMP DORDER BY EMPNO DESC"; pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { int empno = rs.getInt("empno"); String ename = rs.getString("ename"); String job = rs.getString("job"); int mgr = rs.getInt("mgr"); String hiredate = rs.getString("hiredate"); double sal = rs.getDouble("sal"); double comm = rs.getDouble("comm"); int deptno = rs.getInt("deptno"); EmpDTO notice = new EmpDTO(empno, ename, job, mgr, hiredate, sal, comm, deptno); list.add(notice); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcTemplate.close(rs); JdbcTemplate.close(pstmt); } return list; } // ์ฌ์๋ฒํธ๋ก ์ฐพ๊ธฐ public EmpDTO selectDetailEmp(Connection con, String eno) throws DataNotFoundException { // select ํ jdbcTemplate ์ด์ฉ close EmpDTO result = null; PreparedStatement pstmt = null; ResultSet rs = null; try { String sql = "SELECT EMPNO, ENAME, JOB, MGR, TO_CHAR(HIREDATE, 'YYYY-MM-DD') HIREDATE, " + "SAL, COMM, DEPTNO FROM EMP WHERE EMPNO=?"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, Integer.parseInt(eno)); rs = pstmt.executeQuery(); if (rs.next()) { int empno = rs.getInt("empno"); String ename = rs.getString("ename"); String job = rs.getString("job"); int mgr = rs.getInt("mgr"); String hiredate = rs.getString("hiredate"); double sal = rs.getDouble("sal"); double comm = rs.getDouble("comm"); int deptno = rs.getInt("deptno"); result = new EmpDTO(empno, ename, job, mgr, hiredate, sal, comm, deptno); } } catch (SQLException e) { e.printStackTrace(); } finally { JdbcTemplate.close(rs); JdbcTemplate.close(pstmt); } return result; } public void empUpdate(Connection con, EmpDTO empDTO) { PreparedStatement pstmt = null; try { String sql = "update emp set job = ?, sal=? where empno =?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, empDTO.getJob()); pstmt.setDouble(2, empDTO.getSal()); pstmt.setInt(3, empDTO.getEmpno()); int n = pstmt.executeUpdate(); System.out.println("์คํ๋ ๋ ์ฝ๋ ๊ฐฏ์:" + n); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcTemplate.close(pstmt); } } public void empDelete(Connection con, String empno) throws DataNotFoundException { PreparedStatement pstmt = null; try { String sql = "delete from emp where empno = ?"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, Integer.parseInt(empno)); int n = pstmt.executeUpdate(); System.out.println("์คํ๋ ๋ ์ฝ๋ ๊ฐฏ์:" + n); } catch (SQLException e) { e.printStackTrace(); } finally { JdbcTemplate.close(pstmt); } } }
DAO ํด๋์ค๋ sql๋ฌธ์ ์คํํ๋ ์ญํ ์ ๋ด๋นํ๋ค.
public class EmpTest { public static void main(String[] args) { EmpBiz empBiz = new EmpBiz(); ArrayList<EmpDTO> list = empBiz.selectAllEmp(); for (EmpDTO dto : list) { System.out.println(dto); } try { EmpDTO emp = empBiz.selectDetailEmp("7369"); System.out.println(emp); } catch (DataNotFoundException e) { e.printStackTrace(); } try { empBiz.empDelete("107"); } catch (DataNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }// end main }// end class
๋ฉ์ธ์์๋ ๋น์ฆ ํด๋์ค๋ฅผ ํธ์ถํ๋ค.
์ด ํจํด์ ๋ง์ด๋ฐํฐ์ค(mybatis)์์ ์ฌ์ฉํ ํจํด๊ณผ ๋น์ทํ๋ค.
๐ NEXT : ๋ง์ด๋ฐํฐ์ค ์ฐ์ตํ๊ธฐ
๋ฐ์ํ'JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JAVA ์๋ฐ ๋ง์ด๋ฐํฐ์ค mybatis ๋์ SQL (0) 2020.08.23 JAVA ์๋ฐ ๋ง์ด๋ฐํฐ์ค mybatis ์ฌ์ฉํ๊ธฐ (0) 2020.08.21 JAVA ์๋ฐ JDBC ๋ฌธ์ ์ฐ์ต (0) 2020.08.19 JAVA ์๋ฐ JDBC ์ค๋ผํด DB ์ฐ๋ (0) 2020.08.18 JAVA ์๋ฐ ์ ์ถ๋ ฅ I/O, ํ์ผ์ฒ๋ฆฌ (0) 2020.08.17