반응형
📌회원가입 요청 비즈니스 로직 처리
MemberJoinService.java
package service;
import vo.MemberBean;
import static db.JdbcUtil.*;
import java.sql.Connection;
import dao.MemberDAO;
public class MemberJoinService {
public boolean joinMember(MemberBean member) {
boolean joinSuccess = false; //회원 가입 성공 여부
MemberDAO memberDAO = MemberDAO.getInstance();
Connection con = getConnection();
memberDAO.setConnection(con);
int insertCount = memberDAO.insertMember(member); //삽입된 레코드가 1개 이상이면 가입된것으로 간주
if(insertCount > 0){
joinSuccess = true;
commit(con);
}
else{
rollback(con);
}
close(con);
return joinSuccess;
}
}
📌로그인 요청 비즈니스 로직 처리
MemberLoginService.java
package service;
import vo.MemberBean;
import static db.JdbcUtil.*;
import java.sql.Connection;
import dao.MemberDAO;
public class MemberLoginService {
public boolean login(MemberBean member) {
Connection con = getConnection();
MemberDAO memberDAO = MemberDAO.getInstance();
memberDAO.setConnection(con);
boolean loginResult = false; //로그인 성공 여부
String loginId = memberDAO.selectLoginId(member);
if(loginId != null){
loginResult = true;
}
close(con);
return loginResult;
}
}
📌회원 목록 List 비즈니스 로직
MemberListService.java
package service;
import java.sql.Connection;
import java.util.ArrayList;
import dao.MemberDAO;
import static db.JdbcUtil.*;
import vo.MemberBean;
public class MemberListService {
public ArrayList<MemberBean> getMemberList() {
Connection con = getConnection();
MemberDAO memberDAO = MemberDAO.getInstance();
memberDAO.setConnection(con);
ArrayList<MemberBean> memberList = memberDAO.selectMemberList();
close(con);
return memberList;
}
}
📌회원 상세 정보 비즈니스 로직
MemberViewService.java
package service;
import vo.MemberBean;
import static db.JdbcUtil.*;
import java.sql.Connection;
import dao.MemberDAO;
public class MemberViewService {
public MemberBean getMember(String viewId) {
Connection con = getConnection();
MemberDAO memberDAO = MemberDAO.getInstance();
memberDAO.setConnection(con);
MemberBean member = memberDAO.selectMember(viewId);
close(con);
return member;
}
}
📌회원 삭제 비즈니스 로직
MemberDeleteService.java
package service;
import static db.JdbcUtil.*;
import java.sql.Connection;
import dao.MemberDAO;
public class MemberDeleteService {
public boolean deleteMember(String deleteId) {
boolean deleteResult = false;//삭제 성공 여부
Connection con = getConnection();
MemberDAO memberDAO = MemberDAO.getInstance();
memberDAO.setConnection(con);
int deleteCount = memberDAO.deleteMember(deleteId);
if(deleteCount > 0){ //삭제 된 레코드가 1개 이상일 경우 삭제 요청 성공이라 판단
commit(con);
deleteResult = true;
}
else{
rollback(con);
}
close(con);
return deleteResult;
}
}
📌SQL 구문 전송하는 클래스 (DAO)
MemberDAO.java
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import vo.MemberBean;
import static db.JdbcUtil.*;
public class MemberDAO {
public static MemberDAO instance;
Connection con;
PreparedStatement pstmt;
ResultSet rs;
private MemberDAO() {}
//싱글톤 패턴
public static MemberDAO getInstance(){
if(instance == null){
instance = new MemberDAO();
}
return instance;
}
public void setConnection(Connection con){
this.con = con;
}
public String selectLoginId(MemberBean member){
String loginId = null;
String sql="SELECT MEMBER_ID FROM MEMBER1 WHERE MEMBER_ID=? AND MEMBER_PW=?";
try{
pstmt=con.prepareStatement(sql);
pstmt.setString(1, member.getMEMBER_ID());
pstmt.setString(2, member.getMEMBER_PW());
rs = pstmt.executeQuery();
if(rs.next()){
loginId = rs.getString("MEMBER_ID");
}
}catch(Exception ex){
System.out.println("loginMemeber Error: " + ex);
}finally{
close(rs);
close(pstmt);
}
return loginId;
}
public int insertMember(MemberBean member){
String sql="INSERT INTO MEMBER1 VALUES (?,?,?,?,?,?)";
int insertCount=0;
try{
pstmt=con.prepareStatement(sql);
pstmt.setString(1, member.getMEMBER_ID());
pstmt.setString(2, member.getMEMBER_PW());
pstmt.setString(3, member.getMEMBER_NAME());
pstmt.setInt(4, member.getMEMBER_AGE());
pstmt.setString(5, member.getMEMBER_GENDER());
pstmt.setString(6, member.getMEMBER_EMAIL());
insertCount=pstmt.executeUpdate();
}catch(Exception ex){
System.out.println("joinMember Error: " + ex);
}finally{
close(pstmt);
}
return insertCount;
}
public ArrayList<MemberBean> selectMemberList(){
String sql="SELECT * FROM MEMBER1";
ArrayList<MemberBean> memberList=new ArrayList<MemberBean>();
MemberBean mb = null;
try{
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
if(rs.next()){
do{
mb=new MemberBean();
mb.setMEMBER_ID(rs.getString("MEMBER_ID"));
mb.setMEMBER_PW(rs.getString("MEMBER_PW"));
mb.setMEMBER_NAME(rs.getString("MEMBER_NAME"));
mb.setMEMBER_AGE(rs.getInt("MEMBER_AGE"));
mb.setMEMBER_GENDER(rs.getString("MEMBER_GENDER"));
mb.setMEMBER_EMAIL(rs.getString("MEMBER_EMAIL"));
memberList.add(mb);
}while(rs.next());
}
}catch(Exception ex){
System.out.println("getListMember Error: " + ex);
}finally{
close(rs);
close(pstmt);
}
return memberList;
}
public MemberBean selectMember(String id){
String sql="SELECT * FROM MEMBER1 WHERE MEMBER_ID=?";
MemberBean mb = null;
try{
pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
rs=pstmt.executeQuery();
if(rs.next()){
mb=new MemberBean();
mb.setMEMBER_ID(rs.getString("MEMBER_ID"));
mb.setMEMBER_PW(rs.getString("MEMBER_PW"));
mb.setMEMBER_NAME(rs.getString("MEMBER_NAME"));
mb.setMEMBER_AGE(rs.getInt("MEMBER_AGE"));
mb.setMEMBER_GENDER(rs.getString("MEMBER_GENDER"));
mb.setMEMBER_EMAIL(rs.getString("MEMBER_EMAIL"));
}
}catch(Exception ex){
System.out.println("getDetailMember Error: " + ex);
}finally{
close(rs);
close(pstmt);
}
return mb;
}
public int deleteMember(String id){
String sql="DELETE MEMBER1 WHERE MEMBER_ID=?";
int deleteCount = 0;
try{
pstmt=con.prepareStatement(sql);
pstmt.setString(1, id);
deleteCount = pstmt.executeUpdate();
}catch(Exception ex){
System.out.println("deleteMember Error: " + ex);
}finally{
close(pstmt);
}
return deleteCount;
}
}
반응형
'JSP & Servlet > 실습' 카테고리의 다른 글
요청(request) URL 정보 추출하기 (0) | 2022.07.12 |
---|---|
[JSP & Servlet] 회원 관리 - View (0) | 2022.07.03 |
[JSP & Servlet] 회원 관리 - Action (0) | 2022.07.03 |
[JSP & Servlet] 회원 관리 - controller (0) | 2022.07.03 |
[JSP & Servlet] 회원 관리 - 테이블 생성 및 vo(Value Object) + 기본 세팅 (0) | 2022.07.02 |