PlayData/MySQL

[Day 18 | MySQL] DAO, VO๋“ฑ (์ดํ•ด ing)

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 12. 2. 17:47

์›น ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„ ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. 

 

 

 

 

์Šคํ‚ค๋งˆ๋ผ๋Š” ๋‹จ์–ด. 

๋‚ด๊ฐ€ ์•„๋Š” ์Šคํ‚ค๋งˆ๋Š” ์‹ฌ๋ฆฌํ•™ ์šฉ์–ด ๋ฟ์ด์—ˆ๋Š”๋ฐ.. 

 

์Šคํ‚ค๋งˆ๋ž€ ๋ฌด์—‡์ธ๊ฐ€? 

- ์ž, ๋ ๋ ˆ๋น ์ˆ‘์„ ์•ž์œผ๋กœ ๋ถˆ๊ณ ๊ธฐ๋ฒ„๊ฑฐ๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค ! --> ์šฉ์–ด ์‚ฌ์ „ ์ •์˜ 

- ์šฐ๋ฆฌ ํšŒ์‚ฌ์—์„œ ์ธ์‚ฌ ์‹œ์Šคํ…œ์„ ๋งŒ๋“œ๋Š”๋ฐ, ์ง์›์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” DB์• ์„œ ํ…Œ์ด๋ธ” ์ •์˜ํ•˜๋Š” ๊ฒƒ : ์Šคํ‚ค๋งˆ๋ฅผ ์ •ํ•˜๋Š” ๊ฒƒ. 

- RDB(๊ด€๊ณ„ํ˜•๋””๋น„)์˜ ๋ชฉ์  : ๋น„์ฆˆ๋‹ˆ์Šค ์ƒ์—์„œ ํ•„์š”ํ•œ ๊ฒƒ์„ ํ…Œ์ด๋ธ”ํ™” ํ•˜๋Š” ๊ฒƒ. 

- ๊ตญ์–ด ์‚ฌ์ „ : ์Šคํ‚ค๋งˆ ์ „์ง‘ 

- ์‚ฌ๋ฌผ / ์šฉ์–ด / ๊ฐ์ฒด ์‚ฌ์ „ ์ •์˜ 

     - ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค: Table์ด ์ฃผ ์Šคํ‚ค๋งˆ. (๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—๋Š” ๋ทฐ, stored procedure ๋“ฑ์ด ์žˆ๋Š”๋ฐ๋„) 

     - Java : VO / DTO 

 

๊ฒ€์ƒ‰๋งŒ ์ž˜ํ•˜๋ฉด ์‹œ์ค‘์— ๋„๋ ค์žˆ๋Š” ๊ฒƒ์ด ์ฝ”๋“œ.

MCP(Move Copy Paste) ๋Š” ๊ทธ๋ฆผ ์ดํ•ด๋ฅผ ์ž˜ ํ•ด์•ผํ•จ. 

Dept Class ์ •์˜

  1.  DB Connection Class๋ฅผ ๋งŒ๋“ค๊ณ , : DAO
    1. // JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋“œ
    2. // Database Connection ์—ฐ๊ฒฐ
    3. // Statement ์ƒ์„ฑ
    4. // SQL ์ „์†ก --> executeQuery(), executeUpdate()
    5. // ResultSet(๊ฒฐ๊ณผ) ๋ฐ›๊ธฐ
    6. // Database Connection ํ•ด์ œ
  2. Data ๊ฐ์ฒด: ex) Dept Class๋ฅผ ๋งŒ๋“ค๊ณ , : VO / DTO 
    1. //๋ฉค๋ฒ„ ํ•„๋“œ
      private int deptno
      private String dname;
      private String loc;
      ๋ฉค๋ฒ„ ์ƒ์„ฑ์ž
    2. Dept(){ }
      Dept(deptno, dname, loc){ }
    3. ๋ฉค๋ฒ„ ๋ฉ”์†Œ๋“œ 
      getDeptno( ){ }; getDname( ){ }; getLoc( ){ }
      // setDeptno( ){ }; getDname( ){ }; setLoc( ){ }
  3. CRUD ์ฒ˜๋ฆฌ ๊ฐ์ฒด: Dept CRUD Class : DAO 

    DB Connection ํ˜ธ์ถœ: 

                3. // Statement ์ƒ์„ฑ

                4. // SQL ์ „์†ก --> executeQuery(), executeUpdate()

                5. // ResultSet(๊ฒฐ๊ณผ) ๋ฐ›๊ธฐ

 

         selectDept( ){ }  ๋ถ€์„œ๋ช… --> ๋ถ€์„œ๋ฒˆํ˜ธ, ๋ถ€์„œ๋ช…, ๋ถ€์„œ์œ„์น˜ ๋“ฑ๋“ฑ 
         insertDept( ) { } 
         undateDept( ) { }
         deleteDept( ) { }
         listDept( ) { } : ๋ถ€์„œ๋ช…๋งŒ ๋‚˜์˜ค๋Š” ๊ฒƒ --> ์ด ๋ถ€์„œ๋ช…์„ ํด๋ฆญํ•˜๋ฉด (๊ฒŒ์‹œํŒ ์ƒ๊ฐํ•˜๋ฉด ์ข‹์Œ) 

 

----------------------------------------

  1. Main ๊ธฐ๋ฐ˜ Class 
    main() { 
                    // ๋ถ€์„œ๊ด€๋ฆฌ ์‹œ์Šคํ…œ ์‹œ์ž‘ 

                    while() {
                                    1. ๋ถ€์„œ๋ชฉ๋ก
                                    2. ๋ถ€์„œ๋ณด๊ธฐ
                                    3. ๋ถ€์„œ์ž…๋ ฅ
                                    4. ๋ถ€์„œ์ˆ˜์ •
                                    5. ๋ถ€์„œ์‚ญ์ œ
                   }

                    // ๋ถ€์„œ๊ด€๋ฆฌ ์‹œ์Šคํ…œ ์ข…๋ฃŒ

 


DBConn.java

package com.my;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConn {
	
	public static Connection getConnection ( ) throws SQLException {
		
	//1.  JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋“œ
	//2.  Database Connection ์—ฐ๊ฒฐ
		Connection conn = null;
		
		String jdbcDriver = "com.mysql.cj.jdbc.Driver";
		String jdbcUrl = "jdbc:mysql://localhost:3306/empdb?serverTimezone=UTC";
		String dbUser  = "root";
		String dbPwd   = "1234";
		
		conn = DriverManager.getConnection(jdbcUrl,dbUser,dbPwd ); // ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์•ˆํ•ด์ฃผ๋ฉด ๋นจ๊ฐ„์ค„, ์—ฌ๊ธฐ์„  ํŠธ๋ผ์ด์บ์น˜ ์•ˆ์“ฐ๊ณ  throws 
	
		
		return conn;
	

	
	}
}

DeptVO.java

package com.my;

// ๋ฐ์ดํ„ฐ ๊ฐ์ฒด 
public class DeptVO {
	
	//๋ฉค๋ฒ„ ํ•„๋“œ
	private int deptno;
	private String dname;
	private String loc;
	
	// ์ƒ์„ฑ์ž 
	DeptVO(){ } // ์€๋‹‰๋œ ๊ฒƒ
	DeptVO(int deptno, String dname, String loc){
		this.deptno = deptno; this.dname = dname; this.loc = loc;  }
	
	// ๋ฉค๋ฒ„ ๋ฉ”์†Œ๋“œ ( ๋ฉ”๋‰ด source์—์„œ --> Generate getters and setter์—์„œ ์ž๋™ ์ƒ์„ฑ)  
	public int getDeptno() {	return deptno; }
	public String getDname() {	return dname;  }
	public String getLoc() {	return loc;		}	
	@Override
	public String toString() {
		return "DeptVO [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", toString()=" + super.toString()
				+ "]";
	}

}

DeptDAO.java

package com.my;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DeptDAO {

	// Singleton
	private static DeptDAO dao = new DeptDAO();
	private DeptDAO(){ }
	public static DeptDAO getInstance() { return dao; }
	/////////////////
	DeptVO selectDept(int deptno, Connection conn) {
		
		DeptVO dept = null;
		try {
			//String sql = "select * from dept where deptno = "+ deptno;
			String sql = "select * from dept10 where deptno = ?";
			//Statement stmt = conn.createStatement();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, deptno); 
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()) {
				dept = new DeptVO(rs.getInt(1),rs.getString(2),rs.getString("loc"));
				System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
			}
			pstmt.close();
			//stmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return dept;
	}
	
	int insertDept(DeptVO deptObj, Connection conn) {
		
		int resultCount = 0;
		DeptVO dept = null;

		try {
			String sql = "insert into dept10 values(?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, deptObj.getDeptno()); 
			pstmt.setString(2, deptObj.getDname());
			pstmt.setString(3, deptObj.getLoc());
			
			resultCount = pstmt.executeUpdate();
			
			pstmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return resultCount;
	}

	int updateDept(DeptVO deptObj,Connection conn) {
		
		int resultCount = 0;
		
		try {
			String sql = "update dept10 set loc = ? where deptno = ?"; ;
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(2, deptObj.getDeptno()); 
			pstmt.setString(1, deptObj.getLoc());
			resultCount = pstmt.executeUpdate();
			pstmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return resultCount;
	}
	
	int deleteDept(int  deptno,Connection conn ) { 
		int resultCount = 0;
		
		try {
			String sql = "delete from dept10 where deptno = ?"; ;
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, deptno); 
			resultCount = pstmt.executeUpdate();
			pstmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return resultCount;
	}
	
	List<DeptVO> listDept(Connection conn) {
		
		List<DeptVO> deptList = new ArrayList<DeptVO>();

		try {
			
			String sql = "select * from dept10";
			
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()) {
				System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
				deptList.add(new DeptVO(rs.getInt(1),rs.getString(2),rs.getString(3)));
			}
			pstmt.close();
		}
		catch (SQLException e) {
			System.out.println("์—ฐ๊ฒฐ ์•ˆ๋จ");
		}
		return deptList;
	}
	
}

DeptMain.java

// DAO, VO ๋ถ„๋ฆฌํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋Š๋‚Œ์ด ์ค‘์š”ํ•˜๋‹ค
package com.my;

import java.sql.Connection;
import java.util.List;
import java.util.Scanner;

public class DeptMain {

	static String jdbcDriver = "com.mysql.cj.jdbc.Driver";
	static DeptDAO dDao = DeptDAO.getInstance();
	
	static void selectD(int deptno,Connection conn) {
		DeptVO dSel = dDao.selectDept(deptno, conn);
		System.out.println(dSel.toString());
	}
	
	static void insertD(DeptVO deptObj,Connection conn) { 
		int resultCount = dDao.insertDept(deptObj, conn);
		if(resultCount > 0 ) {System.out.println("์ž…๋ ฅ ์™„๋ฃŒ "); }
		else {System.out.println("๋ฌธ์ œ ๋ฐœ์ƒ "); }
	}
	
	static void updateD(DeptVO deptObj,Connection conn) { 
		
		int resultCount = dDao.updateDept(deptObj,conn);
		if(resultCount > 0 ) {System.out.println("์ˆ˜์ • ์™„๋ฃŒ "); }
		else {System.out.println("๋ฌธ์ œ ๋ฐœ์ƒ "); }
	}
	
	static void deleteD(int deptno,Connection conn ) { 
		
		int resultCount = dDao.deleteDept(deptno,conn ); 
		if(resultCount > 0 ) {System.out.println("์‚ญ์ œ ์™„๋ฃŒ "); }
		else {System.out.println("์˜ค๋ฅ˜ ๋ฐœ์ƒ "); }
	
	}
	
	static void listD(Connection conn){
		List<DeptVO> deptList = dDao.listDept(conn);
		for(DeptVO dept: deptList) { 
			System.out.println(dept.toString()); }
	}

	public static void main(String[] args) {
		
		System.out.println("=========== ๋ถ€์„œ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ ์‹œ์ž‘ =============");
		Scanner sc = new Scanner(System.in);
		
		try {
			Class.forName(jdbcDriver);
			Connection conn = DBConn.getConnection();
			
			int iDeptno = 0; String sDname = null; String sLoc = null;
			DeptVO deptObj = null;
			
			while(true) {
				System.out.println("----------------------------------------------");
				System.out.println("1. ์กฐํšŒ, 2.์ž…๋ ฅ 3. ์ˆ˜์ •, 4. ์‚ญ์ œ, 5. ์ „์ฒด์กฐํšŒ, 9.์ข…๋ฃŒ ==> ");
				
				int choiceNum = sc.nextInt();
				
				switch (choiceNum) {
					case 1:
						System.out.println("[1. ์กฐํšŒ :  ๊ฒ€์ƒ‰ํ•  ๋ถ€์„œ๋ฒˆํ˜ธ ์ž…๋ ฅ => ");
						int deptno = sc.nextInt();
						selectD(deptno,conn);
						break;
					case 2:
						System.out.println("[2.์ž…๋ ฅ : ๋ถ€์„œ ๋ฒˆํ˜ธ ์ž…๋ ฅ => "); iDeptno = sc.nextInt(); 
						System.out.println("[2.์ž…๋ ฅ : ๋ถ€์„œ ์ด๋ฆ„ ์ž…๋ ฅ => "); sDname = sc.next();
						System.out.println("[2.์ž…๋ ฅ : ๋ถ€์„œ ์œ„์น˜ ์ž…๋ ฅ => "); sLoc = sc.next();
						deptObj = new DeptVO(iDeptno,sDname,sLoc);
						insertD(deptObj,conn);
						break;
					case 3:
						System.out.println("[3.์ˆ˜์ • : ๋ถ€์„œ ๋ฒˆํ˜ธ ์ž…๋ ฅ => "); iDeptno = sc.nextInt(); 
						System.out.println("[3.์ˆ˜์ • : ๋ณ€๊ฒฝ ์ง€์—ญ ์ž…๋ ฅ => "); sLoc = sc.next();
						deptObj = new DeptVO(iDeptno,sDname,sLoc);
						updateD(deptObj,conn);
						break;
					case 4:
						System.out.println("[4.์‚ญ์ œ : ๋ถ€์„œ ๋ฒˆํ˜ธ ์ž…๋ ฅ => "); iDeptno = sc.nextInt(); 
						deleteD(iDeptno,conn);
						break;
					case 5:
						System.out.println("[5.์ „์ฒด ์กฐํšŒ : ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „์ฒด ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค ");
						listD(conn);
						break;
					case 9:
						System.out.println("[9. ์ข…๋ฃŒ : ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค . ^^");
						System.out.println("=========== ์•ˆ๋…•ํžˆ ๊ฐ€์„ธ์š” =============");
						conn.close(); 
						return;
					default:
						System.out.println("1~5 ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”. ");
				}
			}
				
		}
		catch(Exception e) {
			System.out.println("๋ฌธ์ œ ๋ฐœ์ƒ ");
		}
		sc.close();
	}

}

 

 

DAO์—์„œ์˜ ํ•จ์ˆ˜๋ฅผ main์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

์™”๋‹ค๊ฐ”๋‹ค ๋ณต๋ถ™์„ ์—ฌ๊ธฐ์ €๊ธฐํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ์น˜๋‹ˆ๊นŒ ๋ชจ๋ฅด๊ฒ ๋‹ค! ์†๋„ ๋„ˆ๋ฌด ๋น ๋ฅธ๋ฐ. ๋”ฐ๋ผ ์ฝ”๋“œ ๋ฐ›์•„์“ฐ๊ธฐ๋„ ๋ถˆ๊ฐ€. 

์Œ ๊ต์œก ๊ณผ์ • ์†๋„ ๋ฏธ์นœ๊ฑด๊ฐ€. ๋งค์šฐ ํญ์ฃผํ•œ๋‹ค๋Š” ๋Š๋‚Œ์„ ํ•˜๋ฃจ์ข…์ผ ๋ฐ›์•˜๋‹ค.

 

 

์ง„์งœ ์ดˆ์ง‘์ค‘ํ•ด์„œ ๋”ฐ๋ผ์ณค๋Š”๋ฐ, 

๋‚ด ํ™”๋ฉด ๋ณด๋Š” ์‚ฌ์ด์— 

์—ฌ๊ธฐ ์ด๊ฑฐ๋Š” ์ €๊ธฐ๋กœ ๋‚ ๋ฆด๊ฒŒ์š” ํ•ด์„œ ์˜ฎ๊ฒจ๋ฒ„๋ฆฐ ์ฝ”๋“œ๋“ค์ด ํ•œ๋‘๊ฐœ๊ฐ€ ์•„๋‹ˆ์–ด์„œ, 

๊ทธ๋Ÿด ๋•Œ๋งˆ๋‹ค ์ฐธ์„ ์ธ์„ ๋ฐฑ๊ฐœ์ •๋„ ์ƒˆ๊ธด ๊ฒƒ ๊ฐ™๋‹ค! ใ…Žใ…Žใ…Ž

 

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ๋”ฐ๋ผ ์น˜๊ธฐ ํฌ๊ธฐ. 

์„ค๋ช…์„ ๋“ค์œผ๋ฉด ๋‚ด ์ฝ”๋“œ๋ฅผ ๋ชป์น˜๊ณ , ๋‚ด ์ฝ”๋“œ ์น˜๊ณ  ์žˆ์œผ๋ฉด ์ˆ˜์—…์€ ์ € ๋ฉ€๋ฆฌ ๊ฐ€๋ฒ„๋ฆฌ๊ณ  ์—†๋‹ค! 

 

์ปด๊ณต ์ „๊ณต์ž ๋ถ„๋“ค์€ ์ด์ œ ์ข€ ์žฌ๋ฐŒ์–ดํ•˜์‹œ๋Š”๊ฒŒ ๋ณด์ด๊ณ .. 

๊ทผ๋ฐ ๋Œ€ํ•™ 4๋…„๋™์•ˆ ๊ด€๋ จ ์ „๊ณต ๋ฐฐ์› ์„ํ…๋ฐ ์ด ์ˆ˜์—…์€ ์™œ ๋“ฃ๋Š”๊ฑฐ์ง€ ใ… _ใ… ????

 

 

 

๊ณผ์ œ: