171120 DAO, DTO - RYUDONGJIN/Memo_wiki GitHub Wiki

DAO : Data Access Object

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘์†ํ•ด์„œ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€, ์‚ญ์ œ, ์ˆ˜์ • ๋“ฑ์˜ ์ž‘์—…์„ ํ•˜๋Š” ํด๋ ˆ์Šค
  • ์ผ๋ฐ˜์ ์ธ JSP ๋˜๋Š” Servlet ํŽ˜์ด์ง€ ๋‚ด์— ์œ„์˜ ๋กœ์ง์„ ํ•จ๊ป˜ ๊ธฐ์ˆ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์œ ์ง€๋ณด์ˆ˜ ๋ฐ ์ฝ”๋“œ์˜ ๋ชจ๋“ˆํ™”๋ฅผ ์œ„ํ•ด ๋ณ„๋„์˜ DAO ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉ.

DTO : Data Transfer Object (java bean)

  • DAOํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๋ฐ˜์ ์ธ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์—ฌ ์ž‘์—…ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ•ด๋‹น ๋ฐ์ดํ„ฐ์˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉ.
package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
//์˜ค๋ผํด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์—ฐ๊ฒฐํ•˜๊ณ  select, insert, update, delete ์ž‘์—…์„ ์‹คํ–‰ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค
public class MemberDAO {
	//์˜ค๋ผํด์— ์ ‘์†ํ•˜๋Š” ์†Œ์Šค๋ฅผ ์ž‘์„ฑ(๊ทธ์ „์— ํ†ฐ์บฃ lib์— ojdbc6.jarํŒŒ์ผ ๋„ฃ๊ธฐ)
	/*String id = "system";//์ ‘์†id
	String pw = "123456";//์ ‘์†pw
	String url = "jdbc:oracle:thin:@localhost:1522:XE";//์ ‘์†url 
*/
	Connection con;//๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •
	PreparedStatement pstmt; //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š” ๊ฐ์ฒด
	ResultSet rs; //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ด๋ฐ›์•„ ์ž๋ฐ”์— ์ €์žฅํ•ด์ฃผ๋Š” ๊ฐ์ฒด	
	//๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ฉ”์†Œ๋“œ
	public void getCon() {
		//์ปค๋„ฅ์…˜ํ’€์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผ
		try {
			//์™ธ๋ถ€์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด๋“œ๋ ค์•ผ ํ•˜๊ธฐ์—
			Context initctx = new InitialContext();
			//ํ†ฐ์บฃ ์„œ๋ฒ„์— ์ •๋ณด๋ฅผ ๋‹ด์•„๋†“์€ ๊ณณ์œผ๋กœ ์ด๋™
			Context envctx = (Context) initctx.lookup("java:comp/env");
			//๋ฐ์ดํ„ฐ ์†Œ์Šค ๊ฐ์ฒด๋ฅผ ์„ ์–ธ
			DataSource ds = (DataSource) envctx.lookup("jdbc/pool");
			//๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ปค๋„ฅ์…˜์„ ์—ฐ๊ฒฐํ•ด์ฃผ์‹œ์˜ค
			con = ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*try {
			//1. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์„ ์–ธ(ํด๋ž˜์Šค๋ฅผ ๋“ฑ๋ก == ์˜ค๋ผํด์šฉ์„ ์‚ฌ์šฉ)
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//2. ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘์†
			con = DriverManager.getConnection(url, id, pw);
		} catch (Exception e) {
			e.printStackTrace();
		} */
	}
	//๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํ•œ ์‚ฌ๋žŒ์˜ ํšŒ์› ์ •๋ณด๋ฅผ ์ €์žฅํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ
	public void insertMember(MemberBean mbean) {
		try{
			getCon(); //๋ฉ”์†Œ๋“œ ๋งŒ๋“  ๊ฒƒ์„ ์•ˆ์ชฝ์—์„œ ํ˜ธ์ถœ
			//3. ์ ‘์† ํ›„ ์ฟผ๋ฆฌ์ค€๋น„ํ•˜์—ฌ
			String sql = "INSERT INTO MEMBER VALUES(?,?,?,?,?,?,?,?)";
			//4. ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •
			pstmt = con.prepareStatement(sql);
			//PreparedStatement: JSP์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๋Š” ํด๋ž˜์Šค
			//5. ?์— ๋งž๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋งตํ•‘
			pstmt.setString(1, mbean.getId());
			pstmt.setString(2, mbean.getPw1());
			pstmt.setString(3, mbean.getEmail());
			pstmt.setString(4, mbean.getTel());
			pstmt.setString(5, mbean.getHobby());
			pstmt.setString(6, mbean.getJob());
			pstmt.setString(7, mbean.getAge());
			pstmt.setString(8, mbean.getInfo());
			//6. ์˜ค๋ผํด์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜์‹œ์˜ค
			pstmt.executeUpdate();//insert, update, delete ์‹œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ
			//7. ์ž์› ๋ฐ˜๋‚ฉ
			con.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	//๋ชจ๋“  ํšŒ์›์˜ ์ •๋ณด๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
	public Vector<MemberBean> allSelectMember(){
		//๊ฐ€๋ณ€๊ธธ์ด๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
		Vector<MemberBean> v = new Vector<>();
		//๋ฌด์กฐ๊ฑด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋ฐ˜๋“œ์‹œ ํ•ด์•ผํ•œ๋‹ค.
		try {
			//์ปค๋„ฅ์…˜ ์—ฐ๊ฒฐ
			getCon();
			//์ฟผ๋ฆฌ ์ค€๋น„
			String sql = "SELECT * FROM MEMBER";
			//์ปค๋ฆฌ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š” ๊ฐ์ฒด ์„ ์–ธ
			pstmt = con.prepareStatement(sql);
			//์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰์‹œํ‚จ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•ด์„œ ๋ฐ›์•„์คŒ(์˜ค๋ผํด ํ…Œ์ด๋ธ”์˜ ๊ฒ€์ƒ‰๋œ ๊ฒฐ๊ณผ๋ฅผ ์ž๋ฐ”๊ฐ์ฒด์— ์ €์žฅ)
			rs = pstmt.executeQuery();
			//๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ rs์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•ด๋†“์•„์•ผํ•œ๋‹ค.
			while(rs.next()) { //<-์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ๋งŒํผ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๊ฒ ๋‹ค๋ผ๋Š”๋œป
				MemberBean bean = new MemberBean();//์นผ๋Ÿผ์œผ๋กœ ๋‚˜๋‰˜์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ๋นˆํด๋ ˆ์Šค์— ์ €์žฅ
				bean.setId(rs.getString(1));
				bean.setPw1(rs.getString(2));
				bean.setEmail(rs.getString(3));
				bean.setTel(rs.getString(4));
				bean.setHobby(rs.getString(5));
				bean.setJob(rs.getString(6));
				bean.setAge(rs.getString(7));
				bean.setInfo(rs.getString(8));
				//ํŒจํ‚ค์ง•๋œ memberBeanํด๋ž˜์Šค๋ฅผ ๋ฒกํ„ฐ์— ์ €์žฅ
				v.add(bean); //0๋ฒˆ์ง€๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ
			}
			//์ž์› ๋ฐ˜๋‚ฉ
			con.close();
		} catch (Exception e) {}
		//๋‹ค ์ €์žฅ๋œ ๋ฒกํ„ฐ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด
		return v;
	}
	//ํ•œ ์‚ฌ๋žŒ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ ์ž‘์„ฑ
	public MemberBean oneSelectMember(String id) {
		//ํ•œ์‚ฌ๋žŒ์— ๋Œ€ํ•œ ์ •๋ณด๋งŒ ๋ฆฌํ„ดํ•˜๊ธฐ์— beanํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
		MemberBean bean = new MemberBean();
		try {
			//์ปค๋„ฅ์…˜ ์—ฐ๊ฒฐ
			getCon();
			//์ฟผ๋ฆฌ์ค€๋น„
			String sql = "SELECT * FROM MEMBER WHERE id = ?";
			pstmt = con.prepareStatement(sql);
			//?๊ฐ’์„ ๋งคํ•‘
			pstmt.setString(1, id);
			//์ฟผ๋ฆฌ ์‹คํ–‰
			rs = pstmt.executeQuery();
			if(rs.next()) {//๋ ˆ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด
				bean.setId(rs.getString(1));
				bean.setPw1(rs.getString(2));
				bean.setEmail(rs.getString(3));
				bean.setTel(rs.getString(4));
				bean.setHobby(rs.getString(5));
				bean.setJob(rs.getString(6));
				bean.setAge(rs.getString(7));
				bean.setInfo(rs.getString(8));
			}
			//์ž์› ๋ฐ˜๋‚ฉ
			con.close();
		} catch (Exception e) {}
		return bean;
	}
	//ํ•œ ์‚ฌ๋žŒ์˜ ํŒจ์Šค์›Œ๋“œ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	public String getPass(String id) {
		//์ŠคํŠธ๋ง์œผ๋กœ ๋ฆฌํ„ด์„ ํ•ด์•ผํ•˜๊ธฐ์— ์ŠคํŠธ๋ง๋ณ€์ˆ˜ ์„ ์–ธ
		String pass ="";
		try {
			getCon();
			//์ฟผ๋ฆฌ ์ค€๋น„
			String sql = "SELECT PASS1 FROM MEMBER WHERE id=?";
			pstmt = con.prepareStatement(sql);
			//?๊ฐ’์„ ๋งตํ•‘
			pstmt.setString(1, id);
			//์ฟผ๋ฆฌ ์‹คํ–‰
			rs = pstmt.executeQuery();
			if(rs.next()) {
				pass = rs.getString(1); //ํŒจ์Šค์›Œ๋“œ ๊ฐ’์ด ์ €์žฅ๋œ ์ปฌ๋Ÿผ ์ธ๋ฑ์Šค
			}
			//์ž์›๋ฐ˜๋‚ฉ
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ด
		return pass;
	}
	//ํ•œ ํšŒ์›์˜ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	public void updateMember(MemberBean bean) {
		try {
			getCon();
			//์ฟผ๋ฆฌ์ค€๋น„
			String sql = "UPDATE MEMBER SET email=?, tel=? WHERE id=?";
			//์ฟผ๋ฆฌ์‹คํ–‰ ๊ฐ์ฒด ์„ ์–ธ
			pstmt = con.prepareStatement(sql);
			//?๊ฐ’์„ ๋งตํ•‘
			pstmt.setString(1, bean.getEmail());
			pstmt.setString(2, bean.getTel());
			pstmt.setString(3, bean.getId());
			//์ฟผ๋ฆฌ ์‹คํ–‰
			pstmt.executeUpdate();
			//์ž์› ๋ฐ˜๋‚ฉ
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//ํ•œ ํšŒ์›์„ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	public void deleteMember(String id) {
		try {
			getCon();
			//์ฟผ๋ฆฌ์ค€๋น„
			String sql = "DELETE FROM MEMBER WHERE id=?";
			//์ฟผ๋ฆฌ์‹คํ–‰ ๊ฐ์ฒด ์„ ์–ธ
			pstmt = con.prepareStatement(sql);
			//?๊ฐ’์„ ๋งตํ•‘
			pstmt.setString(1, id);
			//์ฟผ๋ฆฌ ์‹คํ–‰
			pstmt.executeUpdate();
			//์ž์› ๋ฐ˜๋‚ฉ
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ