index
<body>
<!-- <a href="custuserlist.jsp">고객목록</a> -->
<%
response.sendRedirect("custuserlist.jsp");
%>
</body>
custuserlist.jsp
리스트 목록
<%
CustUserDao dao = CustUserDao.getInstance();
List<CustUserDto> list = dao.getCustUserList();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>custuserlist</title>
</head>
<body>
<h1>고객 목록</h1>
<form action="muldel.jsp" method="post">
<table style="width: 700" border="2">
<col width="100"><col width="300"><col width="300">
<tr>
<th bgcolor="#ffff00">
<input type="checkbox" name="alldel" onclick="deletechecks(this.checked)">
</th>
<th style="background-color: gray;">ID</th>
<th style="background-color: gray;">Name</th>
</tr>
<%
if(list.size() == 0){ // 하나도 없다
%>
<tr bgcolor="#f6f6f6">
<td colspan="3" align="center">고객 리스트가 존재하지 않습니다</td>
</tr>
<%
}else{ // 있다
for(int i = 0;i < list.size(); i++){
CustUserDto cust = list.get(i);
%>
<tr bgcolor="#f6f6f6">
<td align="center" bgcolor="yellow">
<input type="checkbox" name="delck" value="<%=cust.getId() %>">
</td>
<td>
<%=cust.getId() %>
</td>
<td>
<a href="custuserdetail.jsp?id=<%=cust.getId() %>">
<%=cust.getName() %>
</a>
</td>
</tr>
<%
}
}
%>
<tr>
<td align="center" height="1" bgcolor="#c0c0c0" colspan="3">
<!-- 다중 삭제 -->
<input type="submit" value="고객정보 삭제">
</td>
</tr>
<tr bgcolor="#f6f6f6">
<td colspan="3">
<a href="custuseradd.jsp">고객정보 추가</a>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
function deletechecks( ch ) {
let arrCheck = document.getElementsByName("delck");
for(i = 0;i < arrCheck.length; i++){
arrCheck[i].checked = ch;
}
}
</script>
</body>
</html>
custuseradd.jsp
고객추가 버튼 누름
<body>
<h1>고객 추가</h1>
<form action="custuseraddAf.jsp">
<table border="2">
<col width="100px"><col width="300px">
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">아이디</th>
<td>
<input type="text" name="id" size="20">
</td>
</tr>
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">이름</th>
<td>
<input type="text" name="name" size="20">
</td>
</tr>
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">주소</th>
<td>
<input type="text" name="address" size="20">
</td>
</tr>
<tr bgcolor="#f6f6f6">
<td align="center" colspan="2">
<input type="submit" value="고객추가">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</body>
custuseraddAf.jsp
추가된 경우 다시 list로, 아니면 add로 돌아감////
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
System.out.println(id + " " + name + " " + address);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
CustUserDao dao = CustUserDao.getInstance();
boolean isS = dao.addCustUser(new CustUserDto(id, name, address));
if(isS){
%>
<script type="text/javascript">
alert('성공적으로 추가되었습니다');
location.href = "custuserlist.jsp";
</script>
<%
}else{
%>
<script type="text/javascript">
alert('추가되지 않았습니다');
location.href = "custuseradd.jsp";
</script>
<%
}
%>
</body>
</html>
cuscustuserdetail.jsp ?id=<%=cust.getId()
회원정보 보기
<%
String id = request.getParameter("id");
System.out.println("id:" + id);
CustUserDao dao = CustUserDao.getInstance();
CustUserDto dto = dao.getCustuser(id);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>custuserdetail</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>고객정보</h1>
<table style="width: 600" border="2">
<col width="200"><col width="400">
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">ID</th>
<td id="id"><%=dto.getId() %></td>
</tr>
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">이름</th>
<td><%=dto.getName() %></td>
</tr>
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">주소</th>
<td><%=dto.getAddress() %></td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="button" id="updateBtn">수정</button>
<%--
<form action="custuserupdate.jsp">
<input type="hidden" name="id" value="<%=dto.getId() %>">
<input type="submit" value="수정">
</form>
--%>
<button type="button" id="deleteBtn">삭제</button>
</td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function () {
$("#updateBtn").click(function () {
alert('updateBtn');
location.href = "custuserupdate.jsp?id=" + $("#id").text();
// let id = $("#id").text();
// alert(id);
// let id = "<%=dto.getId() %>";
// alert(id);
});
$("#deleteBtn").click(function () {
location.href = "custuserdelete.jsp?id=" + $("#id").text();
});
});
</script>
</body>
</html>
custuserdelete.jsp (삭제버튼)
<%
String id = request.getParameter("id");
System.out.println("id:" + id);
CustUserDao dao = CustUserDao.getInstance();
CustUserDto dto = dao.getCustuser(id);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>custuserdetail</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>고객정보</h1>
<table style="width: 600" border="2">
<col width="200"><col width="400">
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">ID</th>
<td id="id"><%=dto.getId() %></td>
</tr>
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">이름</th>
<td><%=dto.getName() %></td>
</tr>
<tr bgcolor="#f6f6f6">
<th style="background-color: gray;">주소</th>
<td><%=dto.getAddress() %></td>
</tr>
<tr>
<td colspan="2" align="center">
<button type="button" id="updateBtn">수정</button>
<%--
<form action="custuserupdate.jsp">
<input type="hidden" name="id" value="<%=dto.getId() %>">
<input type="submit" value="수정">
</form>
--%>
<button type="button" id="deleteBtn">삭제</button>
</td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function () {
$("#updateBtn").click(function () {
alert('updateBtn');
location.href = "custuserupdate.jsp?id=" + $("#id").text();
// let id = $("#id").text();
// alert(id);
// let id = "<%=dto.getId() %>";
// alert(id);
});
$("#deleteBtn").click(function () {
location.href = "custuserdelete.jsp?id=" + $("#id").text();
});
});
</script>
</body>
</html>
custuserupdate.jsp
(수정버튼)
<%
String id = request.getParameter("id");
System.out.println("update id:" + id);
CustUserDao dao = CustUserDao.getInstance();
CustUserDto cust = dao.getCustuser(id);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>custuserupdate</title>
</head>
<body>
<h1>고객정보 수정</h1>
<form action="custuserupdateAf.jsp">
<table style="width: 600">
<col width="200"><col width="400">
<tr bgcolor="#f6f6f6">
<td style="background-color: gray;">아이디</td>
<td><input type="text" size="30" name='id' value="<%=cust.getId() %>" readonly="readonly"></td>
</tr>
<tr bgcolor="#f6f6f6">
<td style="background-color: gray;">이름</td>
<td><input type="text" size="30" name="name" value="<%=cust.getName() %>"> </td>
</tr>
<tr bgcolor="#f6f6f6">
<td style="background-color: gray;">주소</td>
<td><input type="text" size="30" name="address" value="<%=cust.getAddress() %>"> </td>
</tr>
<tr bgcolor="#f6f6f6">
<td colspan="2" align="center">
<input type="submit" value="고객정보변경">
</td>
</tr>
</table>
</form>
</body>
</html>
custuserupdateAf.jsp
수정요청
완료되면 list로 돌아감
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>custuserupdateAf.jsp</title>
</head>
<body>
<%
CustUserDao dao = CustUserDao.getInstance();
int count = dao.updateCustUser(id, name, address);
if(count > 0){
%>
<script type="text/javascript">
alert("정상적으로 수정되었습니다");
location.href = "custuserlist.jsp";
</script>
<%
}else{
%>
<script type="text/javascript">
alert("수정되지 않았습니다");
location.href = "custuserlist.jsp";
</script>
<%
}
%>
</body>
</html>
muldel.jsp
list에서 checkbox 선택하여 삭제(다중클릭 가능)
<%
String delck[] = request.getParameterValues("delck");
CustUserDao dao = CustUserDao.getInstance();
boolean isS = true;
if(delck != null){
/* for(int i = 0; i < delck.length; i++){ //삭제를 세번(개수만큼)
System.out.println(delck[i]);
//db에서 삭제
dao.deleteCustUsers(delck[i]);
} */
isS = dao.deleteCustUsers(delck);
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if(isS == true){
%>
<script type="text/javascript">
alert("삭제되었습니다");
location.href = "custuserlist.jsp";
</script>
<%
}else{
%>
<script type="text/javascript">
alert("삭제되지 않았습니다");
location.href = ""
</script>
<%
}
%>
</body>
</html>
DAO
// model
public class CustUserDao {
private static CustUserDao dao = new CustUserDao();
private CustUserDao() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("OracleDriver load Success");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static CustUserDao getInstance() {
return dao;
}
public List<CustUserDto> getCustUserList() {
String sql = " SELECT ID, NAME, ADDRESS "
+ " FROM CUSTUSER "
+ " ORDER BY ID DESC ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
List<CustUserDto> list = new ArrayList<CustUserDto>();
try {
conn = this.getConnection();
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while(rs.next()) {
String id = rs.getString("ID");
String name = rs.getString("NAME");
String address = rs.getString("ADDRESS");
list.add( new CustUserDto(id, name, address) );
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(conn, psmt, rs);
}
return list;
}
public boolean addCustUser(CustUserDto dto) {
String sql = " INSERT INTO CUSTUSER(ID, NAME, ADDRESS) "
+ " VALUES(?, ?, ?) ";
Connection conn = null;
PreparedStatement psmt = null;
int count = 0;
try {
conn = this.getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getId());
psmt.setString(2, dto.getName());
psmt.setString(3, dto.getAddress());
count = psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close(conn, psmt, null);
}
return count>0?true:false;
}
public CustUserDto getCustuser(String id) {
String sql = " SELECT ID, NAME, ADDRESS "
+ " FROM CUSTUSER"
+ " WHERE ID=? ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
CustUserDto dto = null;
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, id.trim());
rs = psmt.executeQuery();
if(rs.next()) {
dto = new CustUserDto();
dto.setId( rs.getString("ID") );
dto.setName( rs.getString("NAME") );
dto.setAddress( rs.getString("ADDRESS") );
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, psmt, rs);
}
return dto;
}
public boolean deleteCustUser(String id) {
String sql = " DELETE "
+ " FROM CUSTUSER "
+ " WHERE ID=? ";
Connection conn = null;
PreparedStatement psmt = null;
int count = 0;
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, id.trim());
count = psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, psmt, null);
}
return count>0?true:false;
}
public boolean deleteCustUsers(String ids[]) {
String sql = " DELETE FROM CUSTUSER "
+ " WHERE ID=? ";
Connection conn = null;
PreparedStatement psmt = null;
int count[] = new int[ids.length];
try {
conn = getConnection();
conn.setAutoCommit(false);
psmt = conn.prepareStatement(sql);
for(int i = 0;i < ids.length; i++) {
psmt.setString(1, ids[i]);
psmt.addBatch();
}
count = psmt.executeBatch();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {}
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
close(conn, psmt, null);
}
boolean isS = true;
for (int i = 0; i < count.length; i++) {
if(count[i] != -2) { // -2 : 정상종료
isS = false;
break;
}
}
return isS;
}
public int updateCustUser(String id, String name, String address) {
String sql = " UPDATE CUSTUSER "
+ " SET NAME=?, ADDRESS=?"
+ " WHERE ID=? ";
Connection conn = null;
PreparedStatement psmt = null;
int count = 0;
try {
conn = getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, name.trim());
psmt.setString(2, address.trim());
psmt.setString(3, id.trim());
count = psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
close(conn, psmt, null);
}
return count;
}
public Connection getConnection()throws SQLException {
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "hr";
String password = "hr";
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public void close(Connection conn, PreparedStatement psmt, ResultSet rs) {
try {
if(conn != null) {
conn.close();
}
if(psmt != null) {
psmt.close();
}
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
'MVC' 카테고리의 다른 글
방문회수// (0) | 2020.07.22 |
---|---|
기본 (0) | 2020.07.22 |
MVC1 // 회원정보 // 리스트 (0) | 2020.07.22 |