<%@ 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>
<form action="age_db.jsp">
<label>이름 <input type="text" name="name"> </label><br>
<input type="submit">
</form>
<input type="button" value="전체 확인하기" onclick="selectAll();">
</body>
<script>
function selectAll() {
location.href = "age_all.jsp";
}
</script>
</html>
package com.koit.web.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.koit.web.beans.MemberBean; public class MemberDAO { //DB 접근 Connection conn; PreparedStatement pstm; ResultSet rs ; public int getAge(String name) { int age = 0; String sql = "SELECT AGE FROM MEMBER WHERE NAME = ?"; try { conn = DBConnection.getConnection(); pstm = conn.prepareStatement(sql); pstm.setString(1, name); rs = pstm.executeQuery(); if(rs.next()) { age = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return age; } public ArrayList<MemberBean> member (){ String sql = "SELECT * FROM MEMBER"; ArrayList<MemberBean> arrMember = new ArrayList<>(); try { conn = DBConnection.getConnection(); pstm = conn.prepareStatement(sql); rs = pstm.executeQuery(); while(rs.next()) { MemberBean member = new MemberBean(); member.setName(rs.getString(1)); member.setAge(rs.getInt(2)); arrMember.add(member); } } catch (SQLException e) { e.printStackTrace(); } return arrMember; } }
<%@page import="com.koit.web.beans.MemberBean"%> <%@page import="java.util.ArrayList"%> <%@page import="com.koit.web.dao.MemberDAO"%> <%@ 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> <jsp:useBean id="udao" class="com.koit.web.dao.MemberDAO"/> <table border="1"> <tr> <td>이름</td> <td>나이</td> </tr> <% for(int i = 0; i < udao.member().size(); i++){ %> <tr> <td><%=udao.member().get(i).getName() %></td> <td><%=udao.member().get(i).getAge() %></td> </tr> <% } %> </table> </body> </html>
- DB 와 커넥션, 쿼리문 처리를 할 MemberDAO 객체 생성
- DAO 객체인 udao의 ArrayList로 리턴받을 member() 메서드의 사이즈 만큼 for문을 돌려 각각의 빈즈객체에 접근
- ArrayList의 인덱스마다 값을 getter로 가져와서 출력!
<%@page import="com.koit.web.beans.MemberBean"%> <%@page import="java.util.ArrayList"%> <%@page import="com.koit.web.dao.MemberDAO"%> <%@ 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> <jsp:useBean id="udao" class="com.koit.web.dao.MemberDAO"/> <table border="1"> <tr> <td>이름</td> <td>나이</td> </tr> <% ArrayList<MemberBean> result = udao.member(); for(MemberBean member : result){ %> <tr> <td><%=member.getName() %></td> <td><%=member.getAge() %></td> </tr> <% } %> </table></br> <input type="button" value="다시 검색하기" onclick="location.href='age_search.jsp'"/> </body> </html>
- 리턴값을 받는 ArrayList 선언하여 변수명으로 사용
- for each문 사용
- 처음 화면으로 돌아가는 버튼 생성