과제
1) Member 완성해오기
2) Board 만들어오기
이번 관제로 Spring에서 Member 관련 내용 완성
Board 관련 완성해오는 과제를 내주셨다.
아래 코드들은 Spring 과제들 관련 코드이다.
MemberDTO.java
import org.springframework.stereotype.Component;
@Component("memberDTO")
public class MemberDTO {
private int member_num;
private String member_id;
private String member_password;
private String member_date;
private boolean member_role;
public int getMember_num() {
return member_num;
}
public void setMember_num(int member_num) {
this.member_num = member_num;
}
public String getMember_id() {
return member_id;
}
public void setMember_id(String member_id) {
this.member_id = member_id;
}
public String getMember_password() {
return member_password;
}
public void setMember_password(String member_password) {
this.member_password = member_password;
}
public String getMember_date() {
return member_date;
}
public void setMember_date(String member_date) {
this.member_date = member_date;
}
public boolean isMember_role() {
return member_role;
}
public void setMember_role(boolean member_role) {
this.member_role = member_role;
}
@Override
public String toString() {
return "MemberDTO [member_num=" + member_num + ", member_id=" + member_id + ", member_password="
+ member_password + ", member_date=" + member_date + ", member_role=" + member_role + "]";
}
}
MemberDAO.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.nsd.app.biz.common.JDBCUtil;
@Repository
public class MemberDAO implements MemberService{
private final String SQL_ALL = "SELECT MEMBER_NUM, MEMBER_ID, MEMBER_PASSWORD, MEMBER_DATE, MEMBER_ROLE FROM MEMBER";
private final String SQL_ONE = "SELECT MEMBER_NUM, MEMBER_ID, MEMBER_PASSWORD, MEMBER_DATE, MEMBER_ROLE FROM MEMBER WHERE MEMBER_ID = ? AND MEMBER_PASSWORD = ?";
private final String SQL_INSERT = "";
private final String SQL_UPDATE = "";
private final String SQL_DELETE = "";
@Autowired
private MemberDTO member;
@Override
public List<MemberDTO> SelectAll(MemberDTO memberDTO) {
Connection conn=JDBCUtil.connect();
PreparedStatement pstmt=null;
List<MemberDTO> datas = new ArrayList<>();
try {
pstmt=conn.prepareStatement(SQL_ALL);
ResultSet rs=pstmt.executeQuery();
while(rs.next()) {
this.member = new MemberDTO();
this.member.setMember_num(rs.getInt("MEMBER_NUM"));
this.member.setMember_id(rs.getString("MEMBER_ID"));
this.member.setMember_password(rs.getString("MEMBER_PASSWORD"));
this.member.setMember_date(rs.getString("MEMBER_DATE"));
this.member.setMember_role(rs.getBoolean("MEMBER_ROLE"));
datas.add(this.member);
}
} catch (SQLException e) {
System.out.println("SQL문 실패");
}
JDBCUtil.disconnect(pstmt,conn);
return datas;
}
@Override
public MemberDTO SelectOne(MemberDTO memberDTO) {
Connection conn=JDBCUtil.connect();
PreparedStatement pstmt=null;
try {
pstmt=conn.prepareStatement(SQL_ONE);
System.out.println(memberDTO.getMember_id());
pstmt.setString(1, memberDTO.getMember_id());
System.out.println(memberDTO.getMember_password());
pstmt.setString(2, memberDTO.getMember_password());
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
this.member.setMember_num(rs.getInt("MEMBER_NUM"));
this.member.setMember_id(rs.getString("MEMBER_ID"));
this.member.setMember_password(rs.getString("MEMBER_PASSWORD"));
this.member.setMember_date(rs.getString("MEMBER_DATE"));
this.member.setMember_role(rs.getBoolean("MEMBER_ROLE"));
}
} catch (SQLException e) {
System.out.println("SQL문 실패");
}
JDBCUtil.disconnect(pstmt,conn);
return this.member;
}
@Override
public boolean insert(MemberDTO memberDTO) {
return false;
}
@Override
public boolean update(MemberDTO memberDTO) {
return false;
}
@Override
public boolean delete(MemberDTO memberDTO) {
return false;
}
}
MemberService.java
import java.util.List;
public interface MemberService {
List<MemberDTO> SelectAll(MemberDTO memberDTO);
MemberDTO SelectOne(MemberDTO memberDTO);
boolean insert(MemberDTO memberDTO);
boolean update(MemberDTO memberDTO);
boolean delete(MemberDTO memberDTO);
}
MemberServiceImpl.java
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("memberService")
public class MemberServiceImpl implements MemberService {
@Autowired
private MemberDAO memberDAO;
@Override
public List<MemberDTO> SelectAll(MemberDTO memberDTO) {
return this.memberDAO.SelectAll(memberDTO);
}
@Override
public MemberDTO SelectOne(MemberDTO memberDTO) {
return this.memberDAO.SelectOne(memberDTO);
}
@Override
public boolean insert(MemberDTO memberDTO) {
return this.memberDAO.insert(memberDTO);
}
@Override
public boolean update(MemberDTO memberDTO) {
return this.memberDAO.update(memberDTO);
}
@Override
public boolean delete(MemberDTO memberDTO) {
return this.memberDAO.delete(memberDTO);
}
}
LoginPageAction.java
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.nsd.app.biz.common.Action;
import com.nsd.app.biz.common.ActionForward;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@Component("LoginPageAction")
public class LoginPageAction implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) {
AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
ActionForward forward = (ActionForward)factory.getBean("forward");
forward.setPath("login.jsp");
forward.setRedirect(true);
return forward;
}
}
LoginAction.java
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.nsd.app.biz.common.Action;
import com.nsd.app.biz.common.ActionForward;
import com.nsd.app.biz.model.member.MemberDTO;
import com.nsd.app.biz.model.member.MemberService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@Component("LoginAction")
public class LoginAction implements Action{
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) {
AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
ActionForward forward = (ActionForward)factory.getBean("forward");
MemberService ms = (MemberService)factory.getBean("memberService");
MemberDTO data = (MemberDTO)factory.getBean("memberDTO");
System.out.println("LoginAction.java 로그 mid : "+request.getParameter("mid"));
System.out.println("LoginAction.java 로그 mpw : "+request.getParameter("mpw"));
data.setMember_id(request.getParameter("mid"));
data.setMember_password(request.getParameter("mpw"));
data = ms.SelectOne(data);
System.out.println(data);
if(data!=null) {
HttpSession session = request.getSession();
session.setAttribute("mid", data.getMember_id());
request.setAttribute("error", "success");
request.setAttribute("title", "로그인에 성공");
request.setAttribute("msg", "로그인 되었습니다.");
request.setAttribute("path", "main.do");
}
else {
request.setAttribute("error", "error");
request.setAttribute("title", "로그인에 실패하셨습니다..ㅠ");
request.setAttribute("msg", "로그인 실패");
request.setAttribute("path", "login.jsp");
}
forward.setPath("info.jsp");
forward.setRedirect(false);
return forward;
}
}
728x90
'국비 내용 정리 > 과제' 카테고리의 다른 글
Spring 기본 @(어노테이션) 활용 (0) | 2024.10.08 |
---|---|
[Spring] Page 작성 과제(BOARD 관련) (0) | 2024.10.05 |
국비 34일차 과제 (0) | 2024.08.19 |
jSP 쇼핑몰 제작(page : controller) (0) | 2024.08.11 |
jSP 쇼핑몰 제작(page : cart, join, product) (0) | 2024.08.11 |