day11

์ƒ์€๐Ÿ‘ธยท2024๋…„ 1์›” 14์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ์„ธ๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
11/11
post-thumbnail

๐Ÿ“– DB์—์„œ JAVA๋กœ ๋ฐ์ดํ„ฐ SELECT, INSERT, UPDATE, DELETE ํ•˜๊ธฐ

1. DBConnectionManager ํด๋ž˜์Šค, StudentDAO ํด๋ž˜์Šค, StudentDTO ํด๋ž˜์Šค, Main ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•˜๋‹ค!

2. DBConnectionManager ํด๋ž˜์Šค ์—๋Š” ๊ณตํ†ต์ ์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š” ์—ฐ๊ฒฐ, ์—ฐ๊ฒฐ์ข…๋ฃŒ ๋ฉ”์†Œ๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ !

public class DBConnectionManager {
	
	public static Connection connectDB() {
		//DB ์—ฐ๊ฒฐ ๊ด€๋ จ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋กœ๋”ฉ
		Connection conn = null;
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");

			String db_url = "jdbc:oracle:thin:@localhost:1521:orcl";
			String db_id = "scott";
			String db_pw = "tiger";

			conn = DriverManager.getConnection(db_url, db_id, db_pw);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return conn;
	}
	
	public static void closeDB(Connection conn, PreparedStatement psmt, ResultSet rs) {
		try {
			if(conn != null) {
				conn.close();
			}

			if(psmt != null) {
				psmt.close();
			}

			if(rs != null) {
				rs.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //DB Connection ์—ฐ๊ฒฐ ์ข…๋ฃŒ 
	}
}

3. StudentDTO ํด๋ž˜์Šค ์—๋Š” student ํ…Œ์ด๋ธ”๊ณผ ๊ด€๋ จ๋œ ํ•„๋“œ๋ณ€์ˆ˜, ์ƒ์„ฑ์ž, getter setter๋ฉ”์†Œ๋“œ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค!

public class StudentDTO {
	//ํ•„๋“œ๋ณ€์ˆ˜
	public int studno;
	public String name;
	public String id;
	public int grade;
	public String jumin;
	public String birthday; //๋‚ ์งœ
	public String tel;
	public int height;
	public int weight;
	public int deptno1;
	public int deptno2;
	public int profno;
	
	//์ƒ์„ฑ์ž
	public StudentDTO() {
		
	}
	public StudentDTO(int studno, String name, String id, int grade, String jumin, String birthday, String tel, int height,
			int weight, int deptno1, int deptno2, int profno) {
		super();
		this.studno = studno;
		this.name = name;
		this.id = id;
		this.grade = grade;
		this.jumin = jumin;
		this.birthday = birthday;
		this.tel = tel;
		this.height = height;
		this.weight = weight;
		this.deptno1 = deptno1;
		this.deptno2 = deptno2;
		this.profno = profno;
	}

	//๋ฉ”์†Œ๋“œ
	public int getStudno() {
		return studno;
	}

	public void setStudno(int studno) {
		this.studno = studno;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public String getJumin() {
		return jumin;
	}

	public void setJumin(String jumin) {
		this.jumin = jumin;
	}

	public String getbirthday() {
		return birthday;
	}

	public void setbirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}

	public int getDeptno1() {
		return deptno1;
	}

	public void setDeptno1(int deptno1) {
		this.deptno1 = deptno1;
	}

	public int getDeptno2() {
		return deptno2;
	}

	public void setDeptno2(int deptno2) {
		this.deptno2 = deptno2;
	}

	public int getProfno() {
		return profno;
	}

	public void setProfno(int profno) {
		this.profno = profno;
	}
	
	
}

4. StudentDAO ํด๋ž˜์Šค์—๋Š” ์ด์ œ SELECT, INSERT ํ•ด์˜ค๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋„ฃ์–ด์ค„๊ฑด๋ฐ ๋จผ์ € ์ „์ฒด์ ์œผ๋กœ ์“ฐ์ด๋Š” ํ•„๋“œ๋ณ€์ˆ˜ conn, psmt, rs๋ฅผ ์จ์ฃผ๊ณ !

๐Ÿšจ
UPDATE, DELETE๋Š” INSERT์™€ ๋˜‘๊ฐ™๋‹ค!! sql ๋ถ€๋ถ„๋งŒ ๋‹ค๋ฅด๊ฒŒ ์“ฐ๊ณ ์‹ถ์€๋ฐ๋กœ ์“ฐ๋Š”๊ฑฐ๊ณ  psmt.setInt(1, grade); ์ด๋Ÿฐ ๋ถ€๋ถ„๋„ ๋‹ฌ๋ผ์ง€๋Š”๊ฑฐ๊ณ !

๐Ÿšจ
SELECT ๋Š” rs = psmt.executeQuery();
UPDATE, DELETE, INSERT๋Š” result = psmt.executeUpdate();

๐Ÿšจ
SELECT : find
INSERT : save
UPDATE : modify
DELETE : remove ๋กœ ๋ณ€์ˆ˜๋ช…์„ ์ง“๋Š”๋‹ค~!

public class StudentDAO {

	//ํ•„๋“œ๋ณ€์ˆ˜
	Connection conn;
	PreparedStatement psmt;
	ResultSet rs;
    
    // 1. ๊ธฐ๋ณธ student ์ „์ฒด ์กฐํšŒ ๋ฉ”์†Œ๋“œ 
    (SELECT)
	public List<StudentDTO> findStudentList()
	
	//2. ํ•™๋…„(grade)์„ ๋ฐ›์•„์„œ ํ•ด๋‹น ํ•™๋…„ student ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ฉ”์†Œ๋“œ 
    (SELECT)
	public List<StudentDTO> findStudentList(int grade)
	
    //3. StudentDTO ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ๋ฐ›์•„์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ(Insert)ํ•˜๋Š” ๋ฉ”์†Œ๋“œ 
    (INSERT)
	public int saveStudent (StudentDTO studentDTO)
	
	
}

// 1. ๊ธฐ๋ณธ student ์ „์ฒด ์กฐํšŒ ๋ฉ”์†Œ๋“œ

	public List<StudentDTO> findStudentList() {
		//DBConnectionManager ๋งŒ๋“ค์–ด์ค€ connection ์„ ํ™œ์šฉ
		conn = DBConnectionManager.connectDB();

		String sql =  " SELECT studno, name, id, grade, jumin, TO_CHAR(birthday, 'YYYY-MM-DD') birthday, tel, height, weight, deptno1, deptno2, profno "
				+ "FROM student " ;

		List<StudentDTO> studentList = null;

		try {
			psmt = conn.prepareStatement(sql);
			//Connection ํ™œ์šฉํ•ด์„œ sql ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด

			rs = psmt.executeQuery(); //์ค€๋น„๋œ sql ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰!
			studentList = new ArrayList<StudentDTO>(); // ๋ฆฌ์ŠคํŠธ์ƒ์„ฑ

			while(rs.next()) { 
				
				int studno = rs.getInt("studno");
				String name = rs.getString("name");
				String id = rs.getString("id");
				int grade = rs.getInt("grade");
				String jumin = rs.getString("jumin");
				String birthday = rs.getString("birthday"); //DB์—์„œ TO_CHAR๋กœ String์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค์Œ ์‚ฌ์šฉ!
				String tel = rs.getString("tel");
				int height = rs.getInt("height");
				int weight = rs.getInt("weight");
				int deptno1 = rs.getInt("deptno1");
				int deptno2 = rs.getInt("deptno2");
				int profno = rs.getInt("profno");
				
				StudentDTO stu = new StudentDTO(studno, name, id, grade, jumin, birthday, tel, height, weight, deptno1, deptno2, profno); 
				//ํ•œ์ค„ ๋ฐ์ดํ„ฐ ๊ฐ€์ง€๊ณ  ์˜ด 
				
				studentList.add(stu);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnectionManager.closeDB(conn, psmt, rs);
		}

		return studentList;
	}

//2. ํ•™๋…„(grade)์„ ๋ฐ›์•„์„œ ํ•ด๋‹น ํ•™๋…„ student ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ฉ”์†Œ๋“œ

	public List<StudentDTO> findStudentList(int grade) {
		//๊ฒฐ๊ณผ๊ฐ€ ํ•œ๊ฐœ์ธ์ง€ ์—ฌ๋Ÿฌ๊ฐœ(List)์ธ์ง€

		//DBConnectionManager ๋งŒ๋“ค์–ด์ค€ connection ์„ ํ™œ์šฉ
		conn = DBConnectionManager.connectDB();

		String sql =  " SELECT studno, name, id, grade, jumin, TO_CHAR(birthday, 'YYYY-MM-DD') birthday, tel, height, weight, deptno1, deptno2, profno "
				+ " FROM student "
				+ " WHERE grade = ? " ;
 
		List<StudentDTO> studentList2 = null;

		try {
			psmt = conn.prepareStatement(sql);
			//Connection ํ™œ์šฉํ•ด์„œ sql ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด
			
			psmt.setInt(1, grade);
			
			rs = psmt.executeQuery(); //์ค€๋น„๋œ sql ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰!
						
			studentList2 = new ArrayList<StudentDTO>();

			while(rs.next()) { 
				int studno = rs.getInt("studno");
				String name = rs.getString("name");
				String id = rs.getString("id");
				int grade2 = rs.getInt("grade");
				String jumin = rs.getString("jumin");
				String birthday = rs.getString("birthday"); //DB์—์„œ TO_CHAR๋กœ String์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค์Œ ์‚ฌ์šฉ!
				String tel = rs.getString("tel");
				int height = rs.getInt("height");
				int weight = rs.getInt("weight");
				int deptno1 = rs.getInt("deptno1");
				int deptno2 = rs.getInt("deptno2");
				int profno = rs.getInt("profno");
				
				StudentDTO stu = new StudentDTO(studno, name, id, grade2, jumin, birthday, tel, height, weight, deptno1, deptno2, profno); 
				//ํ•œ์ค„ ๋ฐ์ดํ„ฐ ๊ฐ€์ง€๊ณ  ์˜ด 
				
				studentList2.add(stu);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnectionManager.closeDB(conn, psmt, rs);
		}

		return studentList2;
	}

//3. StudentDTO ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ๋ฐ›์•„์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ(Insert)ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

	public int saveStudent (StudentDTO studentDTO) {
		//DBConnectionManager ๋งŒ๋“ค์–ด์ค€ connection ์„ ํ™œ์šฉ
		conn = DBConnectionManager.connectDB();

		String sql =  " INSERT INTO student "
					+ " VALUES ( ?, ?, ?, ?, ?, TO_DATE(?, 'YYYY-MM-DD'), ?, ?, ?, ?, ? ,? ) ";

		int result = 0;

		try {
			psmt = conn.prepareStatement(sql);
			//Connection ํ™œ์šฉํ•ด์„œ sql ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด

			psmt.setInt(1, studentDTO.getStudno());
			psmt.setString(2, studentDTO.getName());
			psmt.setString(3, studentDTO.getId());
			psmt.setInt(4, studentDTO.getGrade());
			
			psmt.setString(5, studentDTO.getJumin()); 
			psmt.setString(6, studentDTO.getbirthday()); //String //TO_DATE('2023-05-01', 'YYYY-MM-DD')
			psmt.setString(7, studentDTO.getTel());
			
			psmt.setInt(8, studentDTO.getHeight());
			psmt.setInt(9, studentDTO.getWeight());
			psmt.setInt(10, studentDTO.getDeptno1());
			psmt.setInt(11, studentDTO.getDeptno2());
			psmt.setInt(12, studentDTO.getProfno());
					
			result = psmt.executeUpdate(); //์ค€๋น„๋œ sql ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰!

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnectionManager.closeDB(conn, psmt, rs);
		}
		
		return result;
	}

// 4. ์ˆ˜์ • UPDATE -> modify ์˜ˆ์‹œ

public int modifyPersonInfo(PersonInfoDTO personInfo) { //ํฌ์žฅ๋ฐ•์Šค(DTO)๋ถˆ๋Ÿฌ์˜ค๋ฉด ๊ทธ์•ˆ์— ์žˆ๋Š” id name ์‚ฌ์šฉ๊ฐ€๋Šฅ!
		//ํ•ด๋‹น ์•„์ด๋””์— ๋งž๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ์ˆ˜์ •!
		
		conn = DBConnectionManager.connectDB();

		String sql =  " UPDATE t_person_info " 
					+ " SET name = ? " 
					+ " WHERE id = ? ";

		int result = 0;

		try {
			psmt = conn.prepareStatement(sql);
			//Connection ํ™œ์šฉํ•ด์„œ sql ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด
			
			psmt.setString(1, personInfo.getName()); //ํฌ์žฅ๋ฐ•์Šค(DTO) ์•ˆ์— ์žˆ๋Š” id
			psmt.setInt(2, personInfo.getId()); //ํฌ์žฅ๋ฐ•์Šค(DTO) ์•ˆ์— ์žˆ๋Š” name
			
			result = psmt.executeUpdate(); // 1, 0
			//rs = psmt.executeQuery(); //์ค€๋น„๋œ sql ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰! -> SELECT๋ฌธ์ผ๋•Œ!
			/*
			SELECT ์ฟผ๋ฆฌ : psmt.excuteQuery(); -> ๊ฒฐ๊ณผ๋กœ ResultSet
			INSERT, UPDATE, DELETE ์ฟผ๋ฆฌ : psmt.excuteUpdate();
										-> ๊ฒฐ๊ณผ : ์ ์šฉ๋œ ํ–‰์˜ ์ˆซ์ž
			 */

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnectionManager.closeDB(conn, psmt, rs);
		}
		
		return result;
	}

// 5. ์‚ญ์ œ DELETE -> remove ์˜ˆ์‹œ

public int removePersonInfoById(int id) { 
		//ํ•ด๋‹น ์•„์ด๋””์— ๋งž๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฆ„์„ ์ˆ˜์ •!
		
		conn = DBConnectionManager.connectDB();

		String sql =  " DELETE FROM t_person_info "
					+ " WHERE id = ? ";

		int result = 0;

		try {
			psmt = conn.prepareStatement(sql);
			//Connection ํ™œ์šฉํ•ด์„œ sql ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด
			
			psmt.setInt(1, id);
			
			result = psmt.executeUpdate(); // 1, 0
			//rs = psmt.executeQuery(); //์ค€๋น„๋œ sql ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰! -> SELECT๋ฌธ์ผ๋•Œ!
			/*
			SELECT ์ฟผ๋ฆฌ : psmt.excuteQuery(); -> ๊ฒฐ๊ณผ๋กœ ResultSet
			INSERT, UPDATE, DELETE ์ฟผ๋ฆฌ : psmt.excuteUpdate();
										-> ๊ฒฐ๊ณผ : ์ ์šฉ๋œ ํ–‰์˜ ์ˆซ์ž
			 */

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnectionManager.closeDB(conn, psmt, rs);
		}
		
		return result;
	}
profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€