본문 바로가기

MVC

MVC1 // 회원정보 // 리스트

첫 화면 고객정보 추가링크 클릭 ->
고객 추가버튼 누르면 목록으로 돌아가서 ID와 NAME이 보임

 

 

 

index

<%@ 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>
<a href="custuserlist.jsp">고객목록</a>
</body>
</html>

 

CustUserDao.java

package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dto.CustUserDto;

//model만들기
public class CustUserDao {

	//dao를 singleton으로 만들기
	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;				//db관련
		PreparedStatement psmt = null;		//query관련
		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;
	}
	
    
	//jsp에서 DB에 넣는 작업 (add)
	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) {
			e.printStackTrace();
		} finally {
			close(conn, psmt, null);
		}
		return count>0?true:false;
	}
	
	
	public Connection getConnection()throws SQLException {
		String url = "jdbc:oracle:thin:@192.168.7.40: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();
		}
	}
}

 

 

custuserlist.jsp

<%@page import="dto.CustUserDto"%>
<%@page import="java.util.List"%>
<%@page import="dao.CustUserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
//Singleton 호출
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">
<col width="100"><col width="300"><col width="300">
<tr>
	<td height="2" bgcolor="#0000ff" colspan="3"></td>
</tr>
<tr>
	<th bgcolor="#ffff00">
		<input type="button" name="alldel" onclick="deletechecks(this.checked)">
	</th>
	<th>ID</th>
	<th>NAME</th>
</tr>
<tr>
	<td height="2" bgcolor="#0000ff" colspan="3"></td>
</tr>
<%
if(list.size() == 0){//데이터가 하나도 없다
	%>
	<tr bgcolor="#f6f6f6">
		<td colspan="3" align="center">고객 리스트가 존재하지 않습니다 </td>
	</tr>
	<tr>
	<td height="2" bgcolor="#0000ff" colspan="3"></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.getName() %>">
					<%=cust.getName() %>
				</a>
			</td>
		</tr>
		<tr>
			<td height="2" bgcolor="#0000ff" colspan="3"></td>
		</tr>
		
		<%
	}
}
%>
<tr>
	<td align="center" height="1" bgcolor="#c0c0c0" colspan="3">
		<!-- 다중삭제 : 체크버튼 선택 후 원하는 값만 삭제-->
		<input type="submit" value="고객정보 삭제">
	</td>
</tr>
<tr>
	<td height="2" bgcolor="#0000ff" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
	<td colspan="3">
		<a href="custuseradd.jsp">고객정보 추가</a>
	</td>
</tr>
</table>
</form>

</body>
</html>

 

custuseradd.jsp

<%@ 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>
<!-- custuseraddAf.jsp는 DB에 넣는 작업 -->
<form action="custuseraddAf.jsp">

<table>
<col width="100px"><col width="300px">
<tr>
	<td height="2" bgcolor="#ff0000" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
	<th>아이디</th>
	<td>
		<input type="text" name="id" size="20">
	</td>
</tr>
<tr>
	<td height="2" bgcolor="#ff0000" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
	<th>이름</th>
	<td>
		<input type="text" name="name" size="20">
	</td>
</tr>
<tr>
	<td height="2" bgcolor="#ff0000" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
	<th>주소</th>
	<td>
		<input type="text" name="address" size="20">
	</td>
</tr>
<tr>
	<td height="2" bgcolor="#ff0000" colspan="3"></td>
</tr>
<tr bgcolor="#f6f6f6">
	<td align="center" colspan="2">
		<input type="submit" value="고객추가">
		<input type="reset" value="취소">
	</td>
</tr>
</table>

</form>
</body>
</html>

 

 

custuseraddAf.jsp

<%@page import="dto.CustUserDto"%>
<%@page import="dao.CustUserDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
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>

 

 

CustUserDto

package dto;
import java.io.Serializable;
/*CREATE TABLE CUSTUSER(
		ID VARCHAR2(50) PRIMARY KEY,
		NAME VARCHAR2(50) NOT NULL,
		ADDRESS VARCHAR2(50)
);*/
public class CustUserDto implements Serializable {
	private String id;
	private String name;
	private String address;
	
	public CustUserDto() {
		// TODO Auto-generated constructor stub
	}
	public CustUserDto(String id, String name, String address) {
		super();
		this.id = id;
		this.name = name;
		this.address = address;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "CustUserDto [id=" + id + ", name=" + name + ", address=" + address + "]";
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'MVC' 카테고리의 다른 글

회원관리list//  (0) 2020.07.23
방문회수//  (0) 2020.07.22
기본  (0) 2020.07.22