MVC 패턴
Service
- 컨트롤러에 Service단을 두는게 보통
- 서비스를 통해 DAO를 호출하여 데이터를 가져옴...
💡 메소드 하나 = 트랜잭션하나
프로젝트 구조
DAO
- 실제 DB와 연결해서 SQL문을 수행하여 결과를 작성하여 Service에 전달
IMemberDAO 인터페이스
- MemberDaoImpl 클래스가 구현해야할 내용물을 선언해둔다.
import java.util.List;
import kr.or.ddit.member.vo.MemberVO;
public interface IMemberDAO {
public int insertMember(MemberVO mv);
public int updateMember(MemberVO mv);
public int deleteMember(String memId);
public List<MemberVO> getAllMemberList();
public boolean checkMember(String memId);
public List<MemberVO> searchMemberList(MemberVO mv);
}
MemberDaoImpl 클래스
- sql문을 변수에 담아 pstmt의 파라미터로 사용
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import kr.or.ddit.member.vo.MemberVO;
import kr.or.ddit.util.JDBCUtil;
import kr.or.ddit.util.JDBCUtil2;
import kr.or.ddit.util.JDBCUtil3;
public class MemberDaoImpl implements IMemberDAO{
private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
private ResultSet rs;
@Override
public int insertMember(MemberVO mv) {
int cnt =0;
try {
conn = JDBCUtil3.getConnection();
String sql ="insert into mymember " +
" (mem_id,MEM_NAME, mem_tel, mem_addr) " +
" values "
+" (?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mv.getMemId());
pstmt.setString(2, mv.getMemName());
pstmt.setString(3, mv.getMemTel());
pstmt.setString(4, mv.getMemAddr());
cnt = pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
JDBCUtil2.close(conn, stmt, pstmt, rs);
}
return cnt;
}
@Override
public int updateMember(MemberVO mv) {
int cnt =0;
try {
conn = JDBCUtil2.getConnection();
String sql = " update mymember " +
" set mem_name = ?, " +
" mem_tel = ?, " +
" mem_addr = ? " +
" where mem_id = ? " ;
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mv.getMemName());
pstmt.setString(2, mv.getMemTel());
pstmt.setString(3, mv.getMemAddr());
pstmt.setString(4, mv.getMemId());
cnt = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil2.close(conn, stmt, pstmt, rs);
}
return cnt;
}
@Override
public int deleteMember(String memId) {
int cnt =0;
try {
conn = JDBCUtil.getConnection();
String sql =" delete from mymember where mem_id = ? ";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, memId);
cnt = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil2.close(conn, stmt, pstmt, rs);
}
return cnt;
}
@Override
public List<MemberVO> getAllMemberList() {
List<MemberVO> memList = new ArrayList<MemberVO>();
try {
conn = JDBCUtil2.getConnection();
String sql = "select * from mymember";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
MemberVO mv = new MemberVO();
mv.setMemId(rs.getString("mem_id"));
mv.setMemName(rs.getString("mem_name"));
mv.setMemTel(rs.getString("mem_tel"));
mv.setMemAddr(rs.getString("mem_addr"));
memList.add(mv);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil2.close(conn,stmt,pstmt,rs);
}
return memList;
}
@Override
public boolean checkMember(String memId) {
boolean chk = false;
try {
conn = JDBCUtil2.getConnection();
String sql = "select count(*) as cnt from mymember "
+ " where mem_id = ? ";
pstmt = conn.prepareStatement(sql);
pstmt.setNString(1, memId);
rs = pstmt.executeQuery();
int cnt=0;
while(rs.next()) {
cnt = rs.getInt("CNT");
}
if(cnt>0) {
chk=true;
}
} catch (SQLException ex) {
ex.printStackTrace();
}finally {
JDBCUtil2.close(conn, stmt, pstmt, rs);
}
return chk;
}
@Override
public List<MemberVO> searchMemberList(MemberVO mv) {
List<MemberVO> memList = new ArrayList<MemberVO>();
try {
conn = JDBCUtil3.getConnection();
String sql="select * from mymember where 1=1 ";
if(mv.getMemId() != null && !mv.getMemId().equals("")) {
sql += " and mem_id = ? ";
}
if(mv.getMemName() != null && !mv.getMemName().equals("")) {
sql += " and mem_name = ? ";
}
if(mv.getMemTel() != null && !mv.getMemTel().equals("")) {
sql += " and mem_tel = ? ";
}
if(mv.getMemAddr() != null && !mv.getMemAddr().equals("")) {
sql += " and mem_addr like '%' ||?|| '%' ";
}
pstmt = conn.prepareStatement(sql);
int index=1;
if(mv.getMemId() != null && !mv.getMemId().equals("")) {
pstmt.setString(index++, mv.getMemId());
}
if(mv.getMemName() != null && !mv.getMemName().equals("")) {
pstmt.setString(index++, mv.getMemName());
}
if(mv.getMemTel() != null && !mv.getMemTel().equals("")) {
pstmt.setString(index++, mv.getMemTel());
}
if(mv.getMemAddr() != null && !mv.getMemAddr().equals("")) {
pstmt.setString(index++, mv.getMemAddr());
}
rs = pstmt.executeQuery();
while(rs.next()) {
MemberVO mv2 = new MemberVO();
mv2.setMemId(rs.getString("mem_id"));
mv2.setMemName(rs.getString("mem_name"));
mv2.setMemTel(rs.getString("mem_tel"));
mv2.setMemAddr(rs.getString("mem_addr"));
memList.add(mv2);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil3.close(conn, stmt, pstmt, rs);
}
return memList;
}
}
Service
IMemberService
import java.util.List;
import kr.or.ddit.member.vo.MemberVO;
public interface IMemberService {
public int insertMember(MemberVO mv);
public int updateMember(MemberVO mv);
public int deleteMember(String memId);
public List<MemberVO> getAllMemberList();
public boolean checkMember(String memId);
public List<MemberVO> searchMemberList(MemberVO mv);
}
MemberServiceImpl
import java.util.List;
import kr.or.ddit.member.dao.IMemberDAO;
import kr.or.ddit.member.dao.MemberDaoImpl;
import kr.or.ddit.member.vo.MemberVO;
public class MemberServiceImpl implements IMemberService{
private IMemberDAO memDAO;
public MemberServiceImpl() {
memDAO = new MemberDaoImpl();
}
@Override
public int insertMember(MemberVO mv) {
return memDAO.insertMember(mv);
}
@Override
public int updateMember(MemberVO mv) {
return memDAO.updateMember(mv);
}
@Override
public int deleteMember(String memId) {
return memDAO.deleteMember(memId);
}
@Override
public List<MemberVO> getAllMemberList() {
return memDAO.getAllMemberList();
}
@Override
public boolean checkMember(String memId) {
return memDAO.checkMember(memId);
}
@Override
public List<MemberVO> searchMemberList(MemberVO mv) {
return memDAO.searchMemberList(mv);
}
}
VO
- CRUD 메소드에 각자 매개변수를 주면 나중에 유지보수가 힘들다
- 따라서 VO 멤버객체를 따로 만들어줄것!
MemberVO
public class MemberVO {
private String memId;
private String memName;
private String memTel;
private String memAddr;
public String getMemId() {
return memId;
}
public void setMemId(String memId) {
this.memId = memId;
}
public String getMemName() {
return memName;
}
public void setMemName(String memName) {
this.memName = memName;
}
public String getMemTel() {
return memTel;
}
public void setMemTel(String memTel) {
this.memTel = memTel;
}
public String getMemAddr() {
return memAddr;
}
public void setMemAddr(String memAddr) {
this.memAddr = memAddr;
}
@Override
public String toString() {
return "MemberVO [memId=" + memId + ", memName=" + memName + ", memTel=" + memTel + ", memAddr=" + memAddr
+ "]";
}
}
Main
package kr.or.ddit.member;
import java.util.List;
import java.util.Scanner;
import kr.or.ddit.member.service.IMemberService;
import kr.or.ddit.member.service.MemberServiceImpl;
import kr.or.ddit.member.vo.MemberVO;
public class MemberInfoMain {
private IMemberService memService;
private Scanner scan = new Scanner(System.in);
public MemberInfoMain() {
memService = new MemberServiceImpl();
}
public void displayMenu(){
System.out.println();
System.out.println("----------------------");
System.out.println(" === 작 업 선 택 ===");
System.out.println(" 1. 자료 입력");
System.out.println(" 2. 자료 삭제");
System.out.println(" 3. 자료 수정");
System.out.println(" 4. 전체 자료 출력");
System.out.println(" 5. 자료 검색");
System.out.println(" 6. 작업 끝.");
System.out.println("----------------------");
System.out.print("원하는 작업 선택 >> ");
}
public void start(){
int choice;
do{
displayMenu();
choice = scan.nextInt();
switch(choice){
case 1 :
insertMember();
break;
case 2 :
deleteMember();
break;
case 3 :
updateMember();
break;
case 4 :
displayMemberAll();
break;
case 5 :
searchMember();
break;
case 6 :
System.out.println("작업을 마칩니다.");
break;
default :
System.out.println("번호를 잘못 입력했습니다. 다시입력하세요");
}
}while(choice!=6);
}
private void searchMember() {
scan.nextLine();
System.out.println();
System.out.println("검색할 정보를입력하세요");
System.out.print("회원 ID>> ");
String memId=scan.nextLine().trim();
System.out.print("회원 이름>> ");
String memName = scan.nextLine().trim();
System.out.print("회원 전화번호>> ");
String memTel = scan.nextLine().trim();
System.out.print("회원 주소>> ");
String memAddr = scan.nextLine().trim();
MemberVO mv = new MemberVO();
mv.setMemId(memId);
mv.setMemId(memId);
mv.setMemTel(memTel);
mv.setMemAddr(memAddr);
List<MemberVO> memList = memService.searchMemberList(mv);
System.out.println();
System.out.println("---------------------------------------");
System.out.println(" ID\t이름\t전화번호\t\t주소");
System.out.println("---------------------------------------");
if(memList.size()==0) {
System.out.println("회원정보가 존재하지 않습니다");
} else {
for(MemberVO mv2 : memList) {
System.out.println(mv2.getMemId()+"\t"+mv2.getMemName()+"\t"+mv2.getMemTel()+"\t\t"+mv2.getMemAddr());
}
}
System.out.println("---------------------------------------");
System.out.println("검색 작업 끝");
}
private void deleteMember() {
System.out.println();
System.out.println("삭제할 회원정보를 입력하세요");
System.out.print("회원 ID : ");
String memId = scan.next();
int cnt = memService.deleteMember(memId);
if(cnt >0) {
System.out.println(memId+"회원정보를 삭제했습니다");
}else {
System.out.println(memId+"회원정보를 삭제 실패");
}
}
private void updateMember() {
boolean chk = false;
String memId ="";
do {
System.out.println();
System.out.println("추가할 회원정보를 입력하세요");
System.out.print("회원 ID : ");
memId = scan.next();
chk = checkMember(memId);
if(chk==false) {
System.out.println("회원ID가 "+memId+"인 회원 존재하지 않습니다.");
System.out.println("다시 입력하세요");
}
} while(chk==false);
System.out.println("회원이름 >> ");
String memName = scan.next();
System.out.println("회원 전화번호 >> ");
String memTel = scan.next();
scan.nextLine();
System.out.println("회원주소 >> ");
String memAddr = scan.nextLine();
MemberVO mv = new MemberVO();
mv.setMemId(memId);
mv.setMemName(memName);
mv.setMemTel(memTel);
mv.setMemAddr(memAddr);
int cnt = memService.updateMember(mv);
if(cnt>0) {
System.out.println(memId+"회원이 정보를 수정했습니다");
}else {
System.out.println(memId+"회원의 정보 수정 실패");
}
}
private void displayMemberAll() {
System.out.println();
System.out.println("---------------------------------------");
System.out.println(" ID\t이름\t전화번호\t\t주소");
System.out.println("---------------------------------------");
List<MemberVO> memList = memService.getAllMemberList();
if(memList.size()==0) {
System.out.println("회원정보가 존재하지 않습니다");
} else {
for(MemberVO mv : memList) {
System.out.println(mv.getMemId()+"\t"+mv.getMemName()+"\t"+mv.getMemTel()+"\t\t"+mv.getMemAddr());
}
}
System.out.println("---------------------------------------");
System.out.println("출력작업 끝");
}
private void insertMember() {
boolean chk = false;
String memId ="";
do {
System.out.println();
System.out.println("추가할 회원정보를 입력하세요");
System.out.print("회원 ID : ");
memId = scan.next();
chk = checkMember(memId);
if(chk==true) {
System.out.println("회원ID가 "+memId+"인 회원은 이미 존재합니다");
System.out.println("다시 입력하세요");
}
} while(chk==true);
System.out.println("회원이름 >> ");
String memName = scan.next();
System.out.println("회원 전화번호 >> ");
String memTel = scan.next();
scan.nextLine();
System.out.println("회원주소 >> ");
String memAddr = scan.nextLine();
MemberVO mv = new MemberVO();
mv.setMemId(memId);
mv.setMemName(memName);
mv.setMemTel(memTel);
mv.setMemAddr(memAddr);
int cnt = memService.insertMember(mv);
if(cnt>0) {
System.out.println(memId+"회원 추가 작업 성공");
}else {
System.out.println(memId+"회원 추가 작업 실패");
}
}
private boolean checkMember(String memId) {
return memService.checkMember(memId);
}
public static void main(String[] args) {
MemberInfoMain memObj = new MemberInfoMain();
memObj.start();
}
}