YJ의 새벽
JDBC 연습2 ( SELECT (사용자입력) + JSP ) 본문
---- index.html ( student/selectDeptName 전달 )
<form action="student/selectDeptName" method="get">
학과명 입력 : <input type="text" name="DeptName">
<button>검색</button>
</form>
-- SelectDeptName.class ( /student/selectDeptName 받은 서블릿 . )
@WebServlet("/student/selectDeptName")
public class SelectDeptName extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StudentService service = new StudentService();
try {
String deptName = req.getParameter("deptName"); // input값을 가져옴
List<Student> stdList2 = service.selectDeptName(deptName);
// List 만들어서 service클래스로 deptName 매개변수값 전달
RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/views/student/selectDept.jsp");
req.setAttribute("stdList2", stdList2);
dispatcher.forward(req, resp);
}catch(Exception e) {
e.printStackTrace();
}
}
}
--- StudentsService.class ( connection 해줄 service 클래스 )
public List<Student> selectDeptName(String deptName) throws Exception {
Connection conn = getConnection(); // 연결 후
List<Student> stdList2 = dao.selectDeptName(conn,deptName);
// dao 클래스로 Connection , deptName 매개변수 전달.
close(conn);
return stdList2;
}
--- StudentDAO . class ( DB와 접근할 DAO 클래스 .)
public List<Student> selectDeptName(Connection conn, String deptName) throws Exception{
List<Student> stdList2 = new ArrayList<>();
try {
String sql = prop.getProperty("selectDeptName");
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, deptName);
rs = pstmt.executeQuery();
while(rs.next()) {
String studentNo = rs.getString("STUDENT_NO");
String studentName = rs.getString("STUDENT_NAME");
String departmentName=rs.getString("DEPARTMENT_NAME");
String studentAddress = rs.getString("STUDENT_ADDRESS");
stdList2.add(
new Student(studentNo,studentName,departmentName,studentAddress)
);
}
}finally {
close(rs);
close(pstmt);
}
return stdList2;
}
--- student-sql.xml ( where 조건 SELECT 문 저장 )
<entry key="selectDeptName">
SELECT STUDENT_NO,STUDENT_NAME,DEPARTMENT_NAME,STUDENT_ADDRESS
FROM TB_STUDENT
JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
WHERE DEPARTMENT_NAME = ?
</entry>
---- selectDept.jsp ( 받은 List 출력 )
<%@ page language="java" contentType="text/html; cherset=UTF-8"
pageEncoding ="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1></h1>
<table border="1">
<tr>
<td>순서</td>
<td>학번</td>
<td>이름</td>
<td>학과</td>
<td>주소</td>
</tr>
<c:forEach var="student" items="${stdList2}" varStatus="vs" >
<tr>
<td>${vs.count }</td>
<td>${student.studentNo }</td>
<td>${student.studentName }</td>
<td>${student.departmentName }</td>
<td>${student.studentAddress }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
'SelfStudy > JDBC' 카테고리의 다른 글
JDBC 활용 연습용3. ( 암호화 (로그인)) (0) | 2023.03.27 |
---|---|
JDBC 활용 연습용2. ( ajax 이용 화면에 정보 바로뿌리기 ) (0) | 2023.03.24 |
JDBC 활용 연습용. ( 이메일 받아서 정보출력 ) (0) | 2023.03.24 |
JDBC 연습 ( SELECT + JSP ) (0) | 2023.03.22 |
JDBC ( insert , update , select ) (0) | 2023.03.17 |
Comments