728x90
반응형
■ JAVA (src/main/java)
□ TelInfoDBConn
* TelInfoDBConn.java
package TelInfoDBConn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TelInfoDBConn {
// jsp 파일에서 접속객체호출시 여기있는 getConnection()을 호출해서 사용
// 이것 없으면 jsp마다 접속객체 만들어야 함
private Connection con; // 접속객체 선언
public Connection getConnection() {
return con;
}
// 어디선가 jsp에서 TelInfoDBConn 클래스의 객체 만드는 new하면
// default 생성자인 여기를 제일 먼저 찾아오므로
// 여기에 드라이브 로딩, 접속 url, id, pw 등을 기록
public TelInfoDBConn() // 생성자(default)
throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
// 오라클 드라이버 메모리로딩 선언
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr");
}
}
□ TelInfoVO
* TelInfoVO.java
package TelInfoVO;
import java.util.Date;
public class TelInfoVO {
private int id;
private String name;
private String tel;
private Date d; // 자바이므로 util로 import
public TelInfoVO() {
}
public TelInfoVO(int id, String name, String tel, Date d) {
this.id = id;
this.name = name;
this.tel = tel;
this.d = d;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Date getD() {
return d;
}
public void setD(Date d) {
this.d = d;
}
}
□ TelInfoDAO
* TelInfoDAO.java
package TelInfoDAO;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import TelInfoDBConn.TelInfoDBConn;
import TelInfoVO.TelInfoVO;
public class TelInfoDAO {
private Connection con;
PreparedStatement pstmt = null;
ResultSet rs = null;
// 접속을 위해서
// 어떤 jsp에서 new TelInfoDAO()하면 여기로 옴(인자없는 생성자를 찾아옴)
public TelInfoDAO() throws ClassNotFoundException, SQLException {
con = new TelInfoDBConn().getConnection();
}
// 즉 접속객체 con을 얻는 순서는
// 어느 jsp에서 (1) new TelInfoDBConn()를 함 => 초기치가 없는 디폴트 생성자를 찾아감
// => 오라클드라이브 로딩, 접속, con에 값이 들어감(접속객체생성)
// ==> 그다음 단계를 (2) getConnection()을 호출한다면 생성된 접속객체가 전달
// 그러므로 con = new TelInfoDBConn().getConnection(); 문장은 (1) (2)를 의미하므로
// con에는 넘어온 접속객체가 들어감
// 이런 방법으로 con을 다른 jsp에서도 가져다 씀
// jsp에서는 접속객체를 얻는 방법은 new TelInfoDAO()를 사용하기만 하면 됨
// jsp에서는 브라우저에 출력하는 작업
// DAO에서는 DB에서 전체 꺼내는 작업
// 사원명단 천제 검색
public ArrayList<TelInfoVO> getAllInfo() throws SQLException{
ArrayList<TelInfoVO> tiarray= new ArrayList<TelInfoVO>();
String sql = "select * from TelTable5 order by id";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String tel = rs.getString("tel");
Date d = rs.getDate("d");
TelInfoVO tv = new TelInfoVO(id, name, tel, d); // VO 객체 생성
tiarray.add(tv); // ArrayList 컬렉션 넣기 작업
}
return tiarray;
} // getAllInfo()-end
public boolean insert_nametel(int id, String name, String tel, String sDate) {
String sql = "insert into TelTable5 values(?, ?, ?, to_date(?))";
try {
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, tel);
pstmt.setString(4, sDate);
pstmt.executeUpdate();
} catch(SQLException e) {
System.out.println("insert Exception");
return false;
}
return true;
} // insert_nametel-end
public TelInfoVO search_nametel(String oriName) throws SQLException {
TelInfoVO tv = null;
String sql = "select * from TelTable5 where name = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, oriName);
rs = pstmt.executeQuery();
if(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String tel = rs.getString("tel");
Date d = rs.getDate("d");
tv = new TelInfoVO(id, name, tel, d);
} else {
tv = null; // tv 객체 참조변수에 null
}
return tv;
} // search_nametel
// 해당 이름 정보 DB에서 삭제
public boolean delete_nametel(String name2) {
String sql = "delete from TelTable5 where name = ?";
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, name2);
pstmt.executeUpdate();
} catch(SQLException e) {
System.out.println("delete Exception");
return false;
}
return true;
}
// 해당 이름의 전화번호 정보 수정
public boolean update_nametel(String tel2, String name2) {
String sql = "update TelTable5 set tel = ? where name = ?";
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, tel2);
pstmt.setString(2, name2);
pstmt.executeUpdate();
} catch(SQLException e) {
System.out.println("update Exception");
return false;
}
return true;
}
}
■ jsp 영역
□ 인덱스
바로 view 페이지로 넘어가도록 설정.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MVC 1 Model</title>
</head>
<body>
<!--
사원관리 프로그램 CRUD(게시판)
사원관리의 시작은 index.jsp(실제로 사원전체보기 화면을 출력하면 됨)
-->
<!-- (1) 클릭해서 사원전체보기 하고자 할 때 -->
<!-- <a href="SawonAllView.jsp">사원전체보기</a> -->
<script>
// (2) 바로 사원전체보기로 가고자 할 때.
window.location="525_005_SawonAllView.jsp";
</script>
</body>
</html>
□ 525_005_SawonAllView.jsp
<%@page import="TelInfoVO.TelInfoVO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="TelInfoDAO.TelInfoDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- 525_005_SawonAllView.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>사원 전체 명단</title>
</head>
<body>
<h1>사원 전체 명단</h1>
<%
request.setCharacterEncoding("UTF-8"); // post(한글처리시 웹브라우저 ==> 서버)
response.setCharacterEncoding("UTF-8"); // post(한글처리시 서버 ==> 웹브라우저)
response.setContentType("text/html; charset=UTF-8");
// get, post에서(한글처리시 나 이런 방식으로 처리한다는 뜻)
// 이쪽 jsp에서 저쪽에 있는 DAO 클래스를 사용하려면 new로 객체를 만들면 된다.
TelInfoDAO tidao = new TelInfoDAO(); // tidao 객체 생성, 여기 jsp에서 자바 안에 있는 메소드 호출을 위해
// connect를 찾아가서 con을 가져와서 db 접속객체 준비 끝
ArrayList<TelInfoVO> tiArray = tidao.getAllInfo();
%>
<!--
목적을 생각하면
모든 사원의 정보를 출력하려고
그런데 모든 사원의 정보가 ArrayList<TelInfoVO> tiArray에 있으므로
이것을 브라우저에 출력하면 된다.
그래서 (1) 테이블 형태를 구성하면서 내용을 출력해보자는
짐작컨데 출력은 컬렉션에서 get(0) 등으로 꺼내고 다시 id, name 등을 꺼내려면 getter를 사용.
-->
<table border="2">
<tr>
<th>사번</th>
<th>이름</th>
<th>전화번호</th>
<th>입사일</th>
</tr>
<% for(TelInfoVO imsi : tiArray){%>
<tr>
<td><%=imsi.getId() %></td>
<td><%=imsi.getName() %></td>
<td><%=imsi.getTel() %></td>
<td><%=imsi.getD() %></td>
</tr>
<%} %>
</table>
<table border="0">
<tr>
<td><a href="525_005_SawonInsertForm.jsp">[입력]</a></td>
<td><a href="525_005_SawonAllForUpdate.jsp">[수정]</a></td>
<td><a href="525_005_SawonAllForDelete.jsp">[삭제]</a></td>
</tr>
</table>
</body>
</html>
□ insert
* 525_005_SawonInsertProcess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 525_005_SawonInsertForm.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>사원 정보 입력</h1>
<form action="525_005_SawonInsertProcess.jsp" method="post">
<table border="2">
<tr>
<td>사번</td>
<td><input type="text" name="id"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>전화번호</td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td>입사일</td> <!-- 20220712 yyyymmdd 형식인데 문자열로 들어옴. 기본 달력 범위에 맞게 -->
<td><input type="text" name="sDate"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="추가하자"></td>
</tr>
</table>
<table border="0">
<tr>
<td><a href="525_005_SawonInsertProcess.jsp">[입력]</a></td>
<td><a href="525_005_SawonAllForUpdate.jsp">[수정]</a></td>
<td><a href="525_005_SawonAllForDelete.jsp">[삭제]</a></td>
<td><a href="525_005_SawonAllView.jsp">[모두보기]</a></td>
</tr>
</table>
</form>
</body>
</html>
* 525_005_SawonInsertProcess.jsp
<%@page import="TelInfoDAO.TelInfoDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- 525_005_SawonInsertProcess.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>사원 추가</title>
</head>
<body>
<h1>사원 추가</h1>
<%
// 사원추가는 무조건 입력하여 dao를 거쳐 db로 들어가면 된다.
// 단, 날짜 입력 형식에 맞게 // 예) 20160219 o, 20151567 x
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
TelInfoDAO tidao = new TelInfoDAO();
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String tel = request.getParameter("tel");
String sDate = request.getParameter("sDate"); // 20160908은 문자형태로 입력
boolean b1 = tidao.insert_nametel(id, name, tel, sDate);
if(b1){
response.sendRedirect("525_005_SawonAllView.jsp");
} else{
%>
<a href="525_005_SawonInsertForm.jsp">사원입력 에러 - 입력화면으로</a>
<%
}
%>
</body>
</html>
□ update
* 525_005_SawonAllForUpdate.jsp
<%@page import="TelInfoVO.TelInfoVO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="TelInfoDAO.TelInfoDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- 525_005_SawonAllForUpdate.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>수정할 이름 선택</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
TelInfoDAO tidao = new TelInfoDAO();
ArrayList<TelInfoVO> tiArray = tidao.getAllInfo();
%>
<table border="2">
<tr>
<th>사번</th>
<th>이름</th>
<th>전화번호</th>
<th>입사일</th>
</tr>
<% for(TelInfoVO imsi : tiArray){%>
<tr>
<td><%=imsi.getId() %></td>
<td><a href="525_005_SawonUpdateForm.jsp?name=<%=imsi.getName() %>"><%=imsi.getName() %></a></td>
<td><%=imsi.getTel() %></td>
<td><%=imsi.getD() %></td>
<%} %>
</tr>
</table>
<table border="0">
<tr>
<td><a href="525_005_SawonInsertForm.jsp">[입력]</a></td>
<td><a href="525_005_SawonAllForDelete.jsp">[삭제]</a></td>
<td><a href="525_005_SawonAllView.jsp">[모두보기]</a></td>
</tr>
</table>
</body>
</html>
* 525_005_SawonUpdateForm.jsp
<%@page import="TelInfoVO.TelInfoVO"%>
<%@page import="TelInfoDAO.TelInfoDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>사원 수정 폼</h1>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
TelInfoDAO tidao = new TelInfoDAO();
String oriName = request.getParameter("name");
TelInfoVO tv2 = tidao.search_nametel(oriName);
%>
<form action="525_005_SawonUpdateProcess.jsp" method="post">
<table border="2">
<tr>
<th>사번</th>
<th>이름</th>
<th>전화번호</th>
<th>입사일</th>
</tr>
<tr>
<td><%=tv2.getId() %></td>
<td><%=tv2.getName() %></td>
<td><input type="text" name="tel" value="<%=tv2.getTel() %>"></td>
<td><%=tv2.getD() %></td>
<input type="hidden" name="name" value="<%=tv2.getName() %>">
</tr>
<tr>
<td colspan="4"><input type="submit" value="수정"></td>
</tr>
</table>
<table border="0">
<tr>
<td><a href="525_005_SawonInsertForm.jsp">[입력]</a></td>
<td><a href="525_005_SawonDeleteForm.jsp">[삭제]</a></td>
<td><a href="525_005_SawonAllView.jsp">[모두보기]</a></td>
</tr>
</table>
</form>
</body>
</html>
* 525_005_SawonUpdateProcess.jsp
<%@page import="TelInfoDAO.TelInfoDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
TelInfoDAO tidao = new TelInfoDAO();
String name = request.getParameter("name");
String tel = request.getParameter("tel");
boolean b1 = tidao.update_nametel(tel, name);
if(b1){
response.sendRedirect("525_005_SawonAllView.jsp");
} else{
%>
<a href="525_005_SawonUpdateForm.jsp">사원수정 에러 - 수정화면으로</a>
<%
}
%>
</body>
</html>
□ delete
* 525_005_SawonAllForDelete.jsp
<%@page import="TelInfoVO.TelInfoVO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="TelInfoDAO.TelInfoDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 525_005_SawonAllForDelete.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>삭제할 이름 선택</h1>
<!--
DAO의 메소드 사용하여
(1) 전체사원명단 출력
(2) 삭제 처리하면 된다.(수정도 같은 패턴으로)
-->
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
TelInfoDAO tidao = new TelInfoDAO();
ArrayList<TelInfoVO> tiArray = tidao.getAllInfo(); // (1)번 작업
// 지금까지는 전체명단 출력에서 이미 사용한 방식
// 이제 일단 전체출력을 하고 삭제시 다시 전체출력 후 삭제
// 또는 일단 전체출력을 하고 수정시 다시 전체출력 후 수정할 예정
// 이제 (2)번 작업을 시작.
%>
<table border="2">
<tr>
<th>사번</th>
<th>이름</th>
<th>전화번호</th>
<th>입사일</th>
</tr>
<% for(TelInfoVO imsi : tiArray){%>
<tr>
<td><%=imsi.getId() %></td>
<td><a href="525_005_SawonDeleteForm.jsp?name=<%=imsi.getName() %>"><%=imsi.getName() %></a></td>
<td><%=imsi.getTel() %></td>
<td><%=imsi.getD() %></td>
<%} %>
</tr>
</table>
<!-- 여기까지 하면 이름에 파란줄이 나오면서 화면에 전체 사원 명단이 출력 -->
<!-- 이제 다음 단계는 내가 삭제를 선택한 사원 1명에 대한 정보가 출력 되어야 한다. -->
<!-- DAO에서 사원 1명 꺼내는 메소드 3형식 정의문이 있어야 한다. -->
<table border="0">
<tr>
<td><a href="525_005_SawonInsertForm.jsp">[입력]</a></td>
<td><a href="525_005_SawonAllForUpdate.jsp">[수정]</a></td>
<td><a href="525_005_SawonAllView.jsp">[모두보기]</a></td>
</tr>
</table>
</body>
</html>
* 525_005_SawonDeleteForm.jsp
<%@page import="TelInfoVO.TelInfoVO"%>
<%@page import="TelInfoDAO.TelInfoDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 525_005_SawonDeleteForm.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>사원 삭제 폼</h1>
<!--
삭제할 사원을 선택한 경우이므로 그 사원의 정보를 일단 화면에 출력
그 다음에 삭제할지 말지를 결정하면 됨
"삭제" 누르면 삭제, 이것 안 누르면 취소.
-->
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
TelInfoDAO tidao = new TelInfoDAO();
String oriName = request.getParameter("name");
TelInfoVO tv2 = tidao.search_nametel(oriName);
%>
<form action="525_005_SawonDeleteProcess.jsp" method="post">
<table border="2">
<tr>
<th>사번</th>
<th>이름</th>
<th>전화번호</th>
<th>입사일</th>
</tr>
<tr>
<td><%=tv2.getId() %></td>
<td><%=tv2.getName() %></td>
<td><%=tv2.getTel() %></td>
<td><%=tv2.getD() %></td>
<input type="hidden" name="name" value="<%=tv2.getName() %>">
</tr>
<tr>
<td colspan="4"><input type="submit" value="삭제"></td>
</tr>
</table>
</form>
</body>
</html>
* 525_005_SawonDeleteProcess.jsp
<%@page import="TelInfoDAO.TelInfoDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 525_005_SawonDeleteProcess.jsp -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
TelInfoDAO tidao = new TelInfoDAO();
String name = request.getParameter("name");
boolean b1 = tidao.delete_nametel(name);
if(b1){
response.sendRedirect("525_005_SawonAllView.jsp");
} else{
%>
<a href="525_005_SawonAllForDelete.jsp">사원삭제 에러 - 삭제화면으로</a>
<%
}
%>
</body>
</html>
728x90
반응형
'IT&코딩 > 국비지원' 카테고리의 다른 글
Servlet - 2 (frontcontroller) (0) | 2023.06.02 |
---|---|
Servlet - 1 (설명, no_frontcontroller) (0) | 2023.05.29 |
JSP, EL, JSTL - 2 (0) | 2023.05.26 |
JSP, EL, JSTL - 1 (0) | 2023.05.25 |
Ajax (0) | 2023.05.24 |