1. 데이터 베이스 테이블 생성(커밋 필수)
2. 테이블 스페이스 생성, 권한부여
3. JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th>아이디</th>
<th>비밀번호</th>
<th>이름</th>
<th>이메일</th>
<th>가입일</th>
<th>삭제</th>
</tr>
<c:forEach items="${members}" var="member">
<tr>
<td>${member.id}</td>
<td>${member.pwd}</td>
<td>${member.name}</td>
<td>${member.email}</td>
<td>${member.joinDate}</td>
<td><a href="remove?id=${member.id}">삭제</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
4. DBconn
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConn { // 이제 갖다쓰면 된다! 일일이 안만들어도됨!
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@np.dullinsub.cloud:1521/xe", "jsp", "1234");
return conn;
}
}
5. Controller
package member.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import member.service.MemberService;
import member.service.MemberServiceimpl;
@WebServlet("/member/list")
public class MemberList extends HttpServlet{
private MemberService memberService = MemberServiceimpl.getInstance();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("members", memberService.list());
req.getRequestDispatcher("/WEB-INF/jsp/member/list.jsp").forward(req, resp);
}
}
6. MemverVo
package member.vo;
import java.sql.Date;
import lombok.Data;
@Data
public class MemberVo {// 인스턴스 변수 > 값이 생성시에 초기화 진행
private final String id; // 상수는 언제나 초기화를 해야 쓸 수 있지만 data annotation을 붙이면 생성자 등 멤버에 필요한 지원을 해준다.
private final String pwd;// final 이므로 세터가 없음. (한번 지정된 값 변경불가)
private final String name;
private final String email;
private final Date joinDate;
}
7. MemberDao
package member.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import member.vo.MemberVo;
import utils.DBConn;
public class MemberDao {
/*
* singleton
* */
private final static MemberDao dao = new MemberDao();
public static MemberDao getInstance() {
return dao;
}
private MemberDao() {}
/*
* singleton > 자기값을 가져오는 메서드 생성 > 기본생성자
* */
private Statement stmt;
private PreparedStatement pstmt;
private Connection conn;
public List<MemberVo> list() {
List<MemberVo>list = new ArrayList<>();
try {
conn = DBConn.getConnection();
String query = "SELECT * FROM T_MEMBER";
pstmt = conn.prepareStatement(query); // 전처리
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
int idx = 1;
MemberVo vo = new MemberVo(rs.getString(idx++),
rs.getString(idx++),
rs.getString(idx++),
rs.getString(idx++),
rs.getDate(idx++)
);
list.add(vo);
}
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
new MemberDao().list().forEach(System.out::println);
}
public void register(MemberVo memberVo) {
try {
conn = DBConn.getConnection();
pstmt = conn.prepareStatement("INSERT INTO T_MEMBER VALUES(?, ?, ?, ?, SYSDATE)");
int idx = 1;
pstmt.setString(idx++, memberVo.getId());
pstmt.setString(idx++, memberVo.getPwd());
pstmt.setString(idx++, memberVo.getName());
pstmt.setString(idx++, memberVo.getEmail());
pstmt.executeUpdate(); // 없으면 실행 안된다.
} catch (Exception e) {
e.printStackTrace(); // 항상 고정
}
}
}
8. MemberService
package member.service;
import java.util.List;
import member.vo.MemberVo;
public interface MemberService {
List<MemberVo> list();
void register(MemberVo memberVo);
void remove(String id);
}
9. MemberServiceImpl
package member.service;
import java.util.List;
import member.dao.MemberDao;
import member.vo.MemberVo;
public class MemberServiceimpl implements MemberService{
private static final MemberService memberService = new MemberServiceimpl();
public static MemberService getInstance() {
return memberService;
}
private MemberServiceimpl() {}
private MemberDao memberDao = MemberDao.getInstance();
@Override
public List<MemberVo> list() {
return memberDao.list();
}
@Override
public void register(MemberVo memberVo) {
memberDao.register(memberVo);
}
@Override
public void remove(String id) {
// TODO Auto-generated method stub
}
}
10. 결과화면
반응형
'JAVA_SERVLET' 카테고리의 다른 글
[JAVA_SERVLET] 파일 업로드 (다중업로드, 자동 요일별분류) (0) | 2022.03.19 |
---|---|
[JSP] 로그인 기능, 창 만들기 (bootStrap5 적용), 유효성 검사 (0) | 2022.03.15 |
[JAVA_SERVLET] 서블릿을 이용한 환율 계산기. (0) | 2022.03.15 |
[JAVA_SERVLET] .jsp 적용이 안된 경우 (0) | 2022.03.14 |