EX) age_all.jsp
<%@page import="com.codingbox.web.dto.MemberBean"%>
<%@page import="java.util.ArrayList"%>
<%@ 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="mdao" class="com.codingbox.web.dao.MemberDAO"/>
<%
ArrayList<MemberBean> result = mdao.selectAll();
%>
<table border="1">
<tr>
<th>이름</th>
<th>나이</th>
</tr>
<% for( MemberBean member : result ){ %>
<tr>
<td><%=member.getName() %></td>
<td><%=member.getAge() %></td>
</tr>
<%} %>
</table>
<input type="button" value="다시 검색하기"
onclick="location.href='age_search.jsp'" />
</body>
</html>
EX) age_db.jsp
<%@page import="com.codingbox.web.dao.MemberDAO"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="com.codingbox.web.dao.DBConnection"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<%
String name = request.getParameter("name");
MemberDAO mdao = new MemberDAO();
int age = mdao.getAge(name);
%>
<% if( age != 0 ){ %>
이름 : <%=name %><br/>
나이 : <%=age %><br/>
<% } else {%>
찾는 이름이 없습니다.
<% } %>
<input type="button" value="다시 검색하기"
onclick="location.href='age_search.jsp'" />
</body>
</html>
EX) age_search.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>
<form action="age_db.jsp">
<label>
이름 <input type="text" name="name"/>
</label>
<br/>
<input type="submit" />
</form>
<input type="button" value="전체 확인하기" onclick="selectAll()" />
<script>
function selectAll(){
location.href = "age_all.jsp";
}
</script>
</body>
</html>
```
---
EX) MemberBean.java
package com.codingbox.web.dto;
public class MemberBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
---
EX) MemberDAO.java
package com.codingbox.web.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.codingbox.web.dto.MemberBean;
public class MemberDAO {
Connection conn;
PreparedStatement pstm;
ResultSet rs;
// 이름으로 나이를 조회
public int getAge(String name) {
String sql = "SELECT age FROM MEMBER WHERE name=?";
int age = 0;
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();
}
return age;
}
public ArrayList<MemberBean> selectAll(){
ArrayList<MemberBean> result = new ArrayList<>();
String sql = "SELECT * FROM MEMBER";
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));
esult.add(member);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}