각진 세상에 둥근 춤을 추자
[JSP] JDBC 프로그래밍 실습 1: Member 관리 본문
실습의 자세한 내용은 아래 페이지 참조
2022.10.14 - [JSP] - [JSP] JDBC 프로그래밍
[실습] Member 관리
아래 사진과 같이 Member 목록을 관리하는 프로그램을 구현해 본다.
(1) 프로젝트 생성 - Java Resources - src/main/java - bean 패키지 생성 - MemberBean.java 파일 생성
pakage bean;
public class MemberBean {
private String uid;
private String name;
private String hp;
private String pos;
private Int dep;
private String rdate;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHp() {
return hp;
}
public void setHp(String hp) {
this.hp = hp;
}
public String getPos() {
return pos;
}
public void setPos(String pos) {
this.pos = pos;
}
public int getDep() {
return dep;
}
public void setDep(int dep) {
this.dep = dep;
}
public String getRdate() {
return rdate;
}
public void setRdate(String rdate) {
this.rdate = rdate;
}
}
(2) 프로젝트 생성 - src - main - webapp - member 폴더 생성
(3) member 폴더 - list.jsp 생성
<%@page import="bean.MemberBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%
// 데이터 베이스 처리
String host = "jdbc:mysql://127.0.0.1:3306/데이터베이스명";
String user = "root";
String pass = "서버 접속 비밀번호";
List<Memberbean> members = new ArrayList<>();
try {
// 1단계: 드라이버 로드
Class.forName("com.mysql.cj.jdbc.Driver");
// 2단계: 데이터베이스 연결
Connection conn = DriverManager.getConnection(host,user,pass);
// 3단계: Statement 생성
Statement stmt = conn.createStatement();
// 4단계: SQL문 전송 + 5-1단계: 결과 받기
ResultSet rs = stmt.executeQuery("SELECT * FROM `member`");
// 5-2단계: 다음 결과 받기
while(rs.next()){
MemberBean mb = new MemberBean();
mb.setUid(rs.getString(1));
mb.setName(rs.getString(2));
mb.setHp(rs.getString(3));
mb.setPos(rs.getString(4));
mb.setDep(rs.getInt(5));
mb.setRdate(rs.getString(6));
members.add(mb);
}
// 6단계: 연결 해제
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>member::list</title>
</head>
<body>
<h3>member 목록</h3>
<a href="../1_JDBCTest.jsp">처음으로</a>
<a href="./register.jsp">member 등록하기</a>
<table border="1">
<tr>
<th>아이디</th>
<th>이름</th>
<th>휴대폰</th>
<th>직급</th>
<th>부서</th>
<th>입사일</th>
<th>관리</th>
</tr>
<% for (MemberBean mb : members) { %>
<tr>
<td><%= mb.getUid %></td>
<td><%= mb.getName %></td>
<td><%= mb.getHp %></td>
<td><%= mb.getPos %></td>
<td>
<%
switch(mb.getDep()){
case 101:
out.print.ln("영업 1부");
break;
case 102:
out.print.ln("영업 2부");
break;
case 103:
out.print.ln("영업 3부");
break;
case 104:
out.print.ln("영업 4부");
break;
case 105:
out.print.ln("영업 5부");
break;
}
%>
</td>
<td><%= mb.getRdate().substring(2,10) %></td>
<td>
<a href="./modify.jsp?uid=<%= mb.getUid() %>">수정</a>
<a href="./delete.jsp?uid=<%= mb.getUid() %>">삭제</a>
</td>
</tr>
<% } %>
</table>
</body>
</html>
(4) member 폴더 - register.jsp 생성
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>member::register</title>
</head>
<body>
<h3>member 등록</h3>
<a href="../1_JDBCTest.jsp">처음으로</a>
<a href="./list.jsp">member 목록</a>
<form action="./registerProc.jsp" method="post">
<table border="1">
<tr>
<td>아이디</td>
<td><input type="text" name="uid"/></td>
</<tr>
<tr>
<td>이름</td>
<td><input type="text" name="name"/></td>
</<tr>
<tr>
<td>휴대폰</td>
<td><input type="text" name="hp"/></td>
</<tr>
<tr>
<td>직급</td>
<td>
<select name="pos">
<option>사원</option>
<option>대리</option>
<option>과장</option>
<option>차장</option>
<option>부장</option>
</select>
</td>
</tr>
<tr>
<td>부서</td>
<td>
<select name="dep">
<option value="101">영업 1부</option>
<option value="102">영업 2부</option>
<option value="103">영업 3부</option>
<option value="104">인사부</option>
<option value="105">영업자원부</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="rignt">
<input type="submit" value="등록"/>
</td>
</tr>
</table>
</form>
</body>
</html>
(5) member 폴더 - registerProc.jsp 생성
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%
// 전송 데이터 수신
request.setCharacterEncoding("utf-8");
String uid = request.getParameter("uid");
String name = request.getParameter("name");
String hp = request.getParameter("hp");
String pos = request.getParameter("pos");
String dep = request.getParameter("dep");
// 데이터베이스 작업
String host = "jdbc:mysql://127.0.0.1:3306/데이터베이스 명";
String user = "root";
String pass = "서버 접속 시 입력 비밀번호";
try{
// 1단계: 드라이버 로드
Class.forName("com.mysql.cj.jdbc.Driver");
// 2단계: 데이터베이스 연결
Connection conn = DriverManager.getConnection(host,user,pass);
// 3단계: '등록' SQL문 작성
String sql = "INSERT INTO `member` VALUES (?,?,?,?,?,now());";
PreparedStatement psmt = conn.preparedStatement(sql);
psmt.setString(1, uid);
psmt.setString(2, name);
psmt.setString(3, hp);
psmt.setString(4, pos);
psmt.setString(5, dep);
// 4단계: SQL문 전송
psmt.executeUpdate();
// (5단계)
// 6단계: 연결 해제
psmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
// 리다이렉트
response.sendRedirect("./list.jsp");
%>
(6) member 폴더 - modify.jsp 생성
<%@page import="bean.MemberBean"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%
// list.jsp에서 등록(전송)한 데이터 수신
request.setCharacterEncoding("utf-8");
String uid = request.getParameter("uid");
// 데이터베이스 작업
String host = "jdbc:mysql://127.0.0.1:3306/데이터베이스명";
String user = "root";
String pass = "서버 접속 비밀번호";
Memberbean mb = null;
try{
// (1단계)
// 2단계: 데이터베이스 연결
Connection conn = DriverManager.getConnection(host,user,pass);
// 3단계: Statement 생성
Statement stmt = conn.createStatement();
// 4단계: SQL 작성, 전송 + 5-1단계: 결과 받기
ResultSet rs = stmt.executeQuery("SELECT * FROM `member` WHERE `uid`='"+uid+"'");
// 5-2단계: 다음 결과 받기
if(rs.next()){
mb = new MemberBean();
mb.setUid(rs.getString(1));
mb.setName(rs.getString(2));
mb.setHp(rs.getString(3));
mb.setPos(rs.getString(4));
mb.setDep(rs.getInt(5));
mb.setRdate(rs.getString(6));
}
// 6단계: 연결 해제
rs.close();
stmt.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>member::modify</title>
</head>
<body>
<h3>member 수정</h3>
<a href="../1_JDBCTest.jsp">처음으로</a>
<a href="./list.jsp">member 목록</a>
<form action="./modifyProc.jsp" method="post">
<table border="1">
<tr>
<td>아이디</td>
<td><input type="text" name="uid" readonly value="<%= mb.getUid() %>"/></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="name" value="<%= mb.getName() %>"/></td>
</tr>
<tr>
<td>휴대폰</td>
<td><input type="text" name="hp" value="<%= mb.getHp() %>"/></td>
</tr>
<tr>
<td>직급</td>
<td>
<select name="pos">
<option>사원</option>
<option>대리</option>
<option>과장</option>
<option>차장</option>
<option>부장</option>
</select>
</td>
</tr>
<tr>
<td>부서</td>
<td>
<select name="dep">
<option value="101">영업1부</option>
<option value="102">영업2부</option>
<option value="103">영업3부</option>
<option value="104">영업4부</option>
<option value="105">인사부</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="수정"/>
</td>
</tr>
</table>
</form>
</body>
</html>
%>
(7) member 폴더 - modifyProc.jsp 생성
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%
// 데이터 수신
request.setCharacterEncoding("utf-8");
String uid = request.getParameter("uid");
String name = request.getParameter("name");
String hp = request.getParameter("hp");
String pos = request.getParameter("pos");
String dep = request.getParameter("dep");
// 데이터베이스 작업
try{
String host = "jdbc:mysql://127.0.0.1:3306/데이터베이스명";
String user = "root";
String pass = "서버 접속 비밀번호";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(host, user, pass);
Statement stmt = conn.createStatement();
String sql = "UPDATE `member` SET ";
sql += "`name`='"+name+"',";
sql += "`hp`='"+hp+"',";
sql += "`pos`='"+pos+"',";
sql += "`dep`="+dep+" ";
sql += "WHERE `uid`='"+uid+"'";
stmt.executeUpdate(sql);
conn.close();
}catch(Exception e){
e.printStackTrace();
}
// 목록이동
response.sendRedirect("./list.jsp");
%>
(8) member 폴더 - delete.jsp 생성
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%
//전송 데이터 수신
request.setCharacterEncoding("utf-8");
String uid = request.getParameter("uid");
// 데이터베이스 작업
try{
String host = "jdbc:mysql://127.0.0.1:3306/데이터베이스명";
String user = "root";
String pass = "서버 접속 비밀번호";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(host, user, pass);
Statement stmt = conn.createStatement();
stmt.executeUpdate("DELETE FROM `member` WHERE `uid`='"+uid+"'");
conn.close();
}catch(Exception e){
e.printStackTrace();
}
// 리스트 이동
response.sendRedirect("./list.jsp");
%>
'JSP' 카테고리의 다른 글
[JSP] 프로젝트 실습 - 게시판 만들기 2 (로그인 화면 설계) (0) | 2022.10.19 |
---|---|
[JSP] 프로젝트 실습 - 게시판 만들기 1 ( 유스케이스 작성) (0) | 2022.10.19 |
[JSP] JDBC 프로그래밍 + 실습: User 관리 (0) | 2022.10.14 |
[JSP] <jsp: useBean> (0) | 2022.10.12 |
[JSP] 액션 태그 - <jsp: forward> (0) | 2022.10.11 |