본문 바로가기
중간평가/disassemble_2

중간평가 (DTO, DAO)

by pms93 2022. 8. 10.

<< MemberDTO Interface >>

package disassemble.DTO;

public interface IMemberDTO {
	public void setEmail(String email);
	public void setPassword(String password);
	public void setName(String name);
	public void setRegisterDate(String registerDate);

	public String getEmail();
	public String getPassword();
	public String getName();
	public String getRegisterDate();
}

 

<< MemberDTO >>

package disassemble.DTO;


//SQL> CREATE TABLE disassemble (
//		  2  email varchar2(20),
//		  3  pw varchar2(20),
//		  4  name varchar2(20),
//		  5  registerdate varchar2(30)
//		  6  );


public class MemberDTO implements IMemberDTO {

	private String email, password, name, RegisterDate;
	@Override
	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public void setPassword(String password) {
		this.password = password;
	}

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

	@Override
	public void setRegisterDate(String registerDate) {
		this.RegisterDate = registerDate;
	}

	@Override
	public String getEmail() {
		return this.email;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public String getRegisterDate() {
		return this.RegisterDate;
	}

}

 

<< RegisterRequest Interface >>

package disassemble.DTO;

public interface IRegisterRequest {
	public String getEmail();
	public String getName() ;
	public String getPassword();
	public String getConfirmPassword();
	
	public void setPassword(String password);
	public void setEmail(String email);
	public void setName(String name);
	public void setConfirmPassword(String confirmPassword);
}

 

<< RegisterRequest >>

package disassemble.DTO;

public class RegisterRequest implements IRegisterRequest {

	private String email, name, password, confirmPassword;
	
	@Override
	public String getEmail() {
		return this.email;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public String getPassword() {
		return this.password;
	}

	@Override
	public String getConfirmPassword() {
		return this.confirmPassword;
	}

	@Override
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public void setEmail(String email) {
		this.email = email;
	}

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

	@Override
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

}

 

<< MemberDAO Interface >>

package disassemble.DAO;

import java.util.Collection;

import disassemble.DTO.MemberDTO;

public interface IMemberDAO {
	public Collection<MemberDTO> selectAll() ;
	public MemberDTO selectEmail(String email);
	public void insert(MemberDTO member);
	public void update(MemberDTO member);
	public void delete(String email);
}

 

<< MemberDAO >>

package disassemble.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 java.util.Collection;
import disassemble.DTO.MemberDTO;

public class MemberDAO implements IMemberDAO{

	private Connection connect;
	private PreparedStatement ps;
	private ResultSet rs;
	
	public MemberDAO() {
		try {
			Class.forName("oracle.jdbc.OracleDriver");
		} catch (ClassNotFoundException e) {
			System.out.println("드라이버를 찾을 수 없습니다.");
			e.printStackTrace();
		}
		
		try {
			this.connect = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
					"oracle", "oracle");
		} catch (SQLException e) {
			System.out.println("DB연결 실패");
			e.printStackTrace();
		}
	}
	
	@Override
	public Collection<MemberDTO> selectAll() {
		String allQuery = "SELECT * FROM disassemble";
		ArrayList<MemberDTO> members = new ArrayList<>();
		
		try {
			ps = connect.prepareStatement(allQuery);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				MemberDTO member = new MemberDTO();
				member.setEmail(rs.getString("email"));
				member.setPassword(rs.getString("pw"));
				member.setName(rs.getString("name"));
				member.setRegisterDate(rs.getString("registerdate"));
				members.add(member);
			}
			
			return members;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public MemberDTO selectEmail(String email) {
		String selectQuery = "SELECT * FROM disassemble WHERE email = ?";
		
		try {
			ps = connect.prepareStatement(selectQuery);
			ps.setString(1, email);
			rs = ps.executeQuery();
			
			if (rs.next()) {
				MemberDTO member = new MemberDTO();
				member.setEmail(rs.getString("email"));
				member.setPassword(rs.getString("pw"));
				member.setName(rs.getString("name"));
				member.setRegisterDate(rs.getString("registerdate"));
				
				return member;
			}
			
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		return null;
	}

	@Override
	public void insert(MemberDTO member) {
		String insertQuery = "INSERT INTO disassemble VALUES(?, ?, ?, ?)";
		
		try {
			ps = connect.prepareStatement(insertQuery);
			ps.setString(1, member.getEmail());
			ps.setString(2, member.getPassword());
			ps.setString(3, member.getName());
			ps.setString(4, member.getRegisterDate());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(MemberDTO member) {
		String updateQuery = "UPDATE disassemble SET pw = ? WHERE email = ?";
		
		try {
			ps = connect.prepareStatement(updateQuery);
			ps.setString(1, member.getPassword());
			ps.setString(2, member.getEmail());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void delete(String email) {
		String deleteQuery = "DELETE FROM disassemble WHERE email = ?";
		
		try {
			ps = connect.prepareStatement(deleteQuery);
			ps.setString(1, email);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public void exit() {
		try {
			if (rs != null)
				rs.close();
			if (ps != null)
				ps.close();
			if (connect != null)
				connect.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

'중간평가 > disassemble_2' 카테고리의 다른 글

중간평가 (Service)  (0) 2022.08.10
중간평가 (Main)  (0) 2022.08.10