๐ŸŒฑ[๊ตฌ๋””์•„์นด๋ฐ๋ฏธ IT๊ตญ๋น„์ง€์›]

๊น€ํฌ์ง„ยท2023๋…„ 4์›” 27์ผ
0

[๊ตฌ๋””์•„์นด๋ฐ๋ฏธ IT๊ตญ๋น„์ง€์›] WHERE์ ˆ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์ž๋“ค์„ ๋ฐฐ์šฐ๊ณ , ๊ทธ์ค‘ LIKE ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๋ฆ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค. ๋˜ํ•œ ์ฝ˜์†”์ฐฝ์˜ ์ถœ๋ ฅ ์ƒ‰์ƒ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ๋ฐฐ์› ๋‹ค.


๐Ÿ’ก WHERE์ ˆ์— ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์—ฐ์‚ฐ์ž


SELECT * FROM employees
WHERE gender = 'M'
LIMIT 0, 10;

SELECT * FROM employees
WHERE emp_no = 10005;

SELECT * FROM employees
WHERE emp_no < 10005;

SELECT * FROM employees
WHERE first_name < 'C';

SELECT * FROM employees
WHERE emp_no = 10005 OR emp_no = 10008;

SELECT * FROM employees
WHERE emp_no IN (10005, 10008);
// ์ด๋ ‡๊ฒŒ ์“ฐ๋Š” ๊ฒƒ์ด ๋” ์ข‹์Œ

SELECT * FROM employees
WHERE emp_no >= 10005 AND emp_no <= 10008;

SELECT * FROM employees
WHERE emp_no BETWEEN 10005 AND 10008;
// ์ด๋ ‡๊ฒŒ ์“ฐ๋Š” ๊ฒƒ์ด ๋” ์ข‹์Œ

SELECT * FROM employees
WHERE first_name LIKE 'Anneke'
// ๋ฌธ์ž์—ด์— ํŠนํ™”๋œ ์—ฐ์‚ฐ์ž
SELECT * FROM employees
WHERE first_name LIKE 'A%'
// ์ด๋ ‡๊ฒŒ ์กฐ๊ฑด์„ ๋ถ™์ผ ์ˆ˜ ์žˆ์Œ (A๋กœ ์‹œ์ž‘ํ•˜๋Š”)
SELECT * FROM employees
WHERE first_name LIKE '%a'
// a๋กœ ๋๋‚˜๋Š”
SELECT * FROM employees
WHERE first_name LIKE '%ab%'
// ์ˆœ์„œ์ƒ๊ด€์—†์ด ab๊ฐ€ ๋“ค์–ด๊ฐ€๋Š”
SELECT * FROM employees
WHERE first_name LIKE '_ab_'
// ์ค‘๊ฐ„์— ab๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” _๋Œ€์‹  ?๊ฐ€๋Šฅ

SELECT * FROM employees
WHERE first_name LIKE '%be%' OR last_name LIKE '%be%';

SELECT concat(first_name,' ',last_name) fullname
FROM employees
WHERE concat(first_name,' ',last_name) LIKE '%be%';

SELECT * FROM employees
WHERE YEAR(hire_date) BETWEEN 1988 AND 1990;
  • sql์—์„œ ๋Œ€์ž… ์—ฐ์‚ฐ์ž := ๋“ฑํ˜ธ ์—ฐ์‚ฐ์ž =
    ์ž๋ฐ”์—์„œ ๋Œ€์ž… ์—ฐ์‚ฐ์ž = ๋“ฑํ˜ธ ์—ฐ์‚ฐ์ž ==

  • sql๋ฌธ์—์„œ๋Š” <,> ์—ฐ์‚ฐ์ž๋ฅผ ๋ฌธ์ž์—ด์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด a๋ณด๋‹ค z๊ฐ€ ๋” ํฌ๋‹ค.

  • ์ž๋ฐ”์—์„œ ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ๋Š” ๋“ฑํ˜ธ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— equals๋ฅผ ์ด์šฉํ•ด์•ผํ•œ๋‹ค. sql๋ฌธ์—์„œ ๋“ฑํ˜ธ์—ฐ์‚ฐ์ž๋Š” ๋ฌธ์ž์™€ ์ˆซ์ž์— ๋ชจ๋‘ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๋Œ€์‹ ์— ๋ฌธ์ž์—ด์— ํŠนํ™”๋œ LIKE ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค. LIKE ์—ฐ์‚ฐ์ž๋Š” ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ ์–ด๋–ค ๊ธ€์ž๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€, ์–ด๋–ค ๊ธ€์ž๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ๋“ฑ์˜ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค!

  • concat ์—ฐ์‚ฐ์ž๋Š” ๋ฌธ์ž์—ด์„ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค. ๋‘๊ฐœ์˜ ๋ฌธ์ž์—ด ์นผ๋Ÿผ ๋ชจ๋‘์—์„œ ๊ฐ™์€ ๊ฒ€์ƒ‰์„ ํ•˜๊ณ  ์‹ถ์„ ๋•Œ, concat ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ํ•œ๋ฒˆ์— ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๋‹จ, ์ด๋•Œ ์ค‘๊ฐ„์— ๊ณต๋ฐฑ์„ ๋„ฃ์–ด์„œ ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ํ•ฉ์ณ์ ธ์„œ ๊ฒ€์ƒ‰๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€๋ง์ž!

  • IN ์—ฐ์‚ฐ์ž๋Š” ๊ด„ํ˜ธ ๋‚ด์˜ ๊ฐ’ ์ค‘ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์žˆ์œผ๋ฉด ture๊ฐ€ ๋˜๋Š”๋ฐ, OR ์—ฐ์‚ฐ์ž์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜์ง€๋งŒ IN ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ์ฟผ๋ฆฌ๋ฅผ ํ›จ์”ฌ ์งง๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

  • BETWEEN ์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์ž‡๊ฐ’์„ ํ‘œํ˜„ํ•˜๋Š”๋ฐ, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ AND ์—ฐ์‚ฐ์ž๋กœ๋„ ๊ฐ™์€ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์ง€๋งŒ BETWEEN ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ๋” ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ’ก empListBySearch.jsp

์œ„์—์„œ ๋ฐฐ์šด ์—ฐ์‚ฐ์ž๋“ค์„ ์ด์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ์ด๋ฆ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

๐Ÿ“ ์ž‘์„ฑ

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "vo.*" %>
<%@ page import = "java.util.*" %>
<%
	// ์ด๋ฆ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ์ถ”๊ฐ€

	// 1) Controller Layer : ์š”์ฒญ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„
	// ์š”์ฒญ ์ฒ˜๋ฆฌํ•ด์•ผํ•  ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ : ํ˜„์žฌํŽ˜์ด์ง€, ํ•œํŽ˜์ด์ง€๋‹น ์ถœ๋ ฅํ•  ํ–‰์˜ ์ˆ˜, ๊ฒ€์ƒ‰๋‹จ์–ด
	// ํ˜„์žฌํŽ˜์ด์ง€, ํ•œํŽ˜์ด์ง€๋‹น ์ถœ๋ ฅํ•  ํ–‰์˜ ์ˆ˜(String) : Integer -> int ๋กœ ๋ฐ”๊ฟ”์•ผํ•จ
	
	// ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์ด null์ธ์ง€ ๋””๋ฒ„๊น…
	System.out.println(request.getParameter("currentPage") + " <-- empListBySearch param currentPage");
	System.out.println(request.getParameter("rowperPage") + " <-- empListBySearch param rowperPage");
	System.out.println(request.getParameter("searchWord") + " <-- empListBySearch param searchWord");
	
	// ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ null ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ(null์ด์–ด๋„ ๋˜๋Š”์ง€, null์ด๋ฉด ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€)
	// ํ˜„์žฌํŽ˜์ด์ง€ : null์ด ์•„๋‹ ๊ฒฝ์šฐ Integer๋กœ int ๋ณ€์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ ์ €์žฅ, null์ผ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’(1ํŽ˜์ด์ง€) ์ €์žฅ
	// ํ•œํŽ˜์ด์ง€๋‹น ์ถœ๋ ฅํ•  ํ–‰์˜ ์ˆ˜ : null์ด ์•„๋‹ ๊ฒฝ์šฐ Integer๋กœ int ๋ณ€์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ ์ €์žฅ, null์ผ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’(10๊ฐœ) ์ €์žฅ
	// ๊ฒ€์ƒ‰๋‹จ์–ด : null์ด ์•„๋‹ ๊ฒฝ์šฐ String ๋ณ€์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ ์ €์žฅ, null์ผ ๊ฒฝ์šฐ ""(๊ณต๋ฐฑ) ์ €์žฅ
	// ๊ฒ€์ƒ‰๋‹จ์–ด๊ฐ€ null์ด๊ฑฐ๋‚˜ ๊ณต๋ฐฑ์ผ ๋•Œ ๋‘˜๋‹ค ์‹คํ–‰ํ•  ์ฟผ๋ฆฌ๋Š” ๊ฐ™์€๋ฐ, ""(๊ณต๋ฐฑ)์œผ๋กœ ํ†ต์ผ ์‹œํ‚ค์ง€ ์•Š์œผ๋ฉด ๋ถ„๊ธฐ๋ฅผ ๋‘๋ฒˆ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—
	
	/* 
		int currentPage; // ๋ณ€์ˆ˜์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋•Œ๋ฌธ์— if๋ฌธ ๋ธ”๋ก ๋ฐ–์—์„œ ์„ ์–ธ
		if(request.getParameter("currentPage") != null) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		} else { // null์ผ ๊ฒฝ์šฐ
			currentPage = 1; // ๋งŒ์•ฝ์— ์„ ์–ธ ํ•  ๋•Œ ์ดˆ๊ธฐ๊ฐ’์„ 1๋กœ ์ฃผ์—ˆ๋‹ค๋ฉด ์ฝ”๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ!
		}
	*/
	int currentPage = 1;
	if(request.getParameter("currentPage") != null) {
		currentPage = Integer.parseInt(request.getParameter("currentPage"));
	}
	
	int rowPerPage = 10;
	if(request.getParameter("rowPerPage") != null) {
		rowPerPage = Integer.parseInt(request.getParameter("rowPerPage"));
	}
	
	String searchWord = "";
	if(request.getParameter("searchWord") != null) {
		searchWord = request.getParameter("searchWord");
	}
	
	// ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํ›„(๊ฒฐ๊ณผ ๋ณ€์ˆ˜) ๋””๋ฒ„๊น…
	System.out.println(currentPage + " <-- empListBySearch currentPage");
	System.out.println(searchWord + " <-- empListBySearch searchWord");
	System.out.println(rowPerPage + " <-- empListBySearch rowperPage");
	
	
	// 2) Model Layer : ๋ชจ๋ธ๊ฐ’(view์— ์ถœ๋ ฅํ•  ๋ชจ๋“  ๋‚ด์šฉ)์„ ์ƒ์„ฑํ•˜๊ธฐ ๊นŒ์ง€์˜ ๋ชจ๋“  ๋ถ€๋ถ„
	// Controller Layer์—์„œ ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ ๋ณ€์ˆ˜(currentPage, rowPerPage, searchWord)๋ฅผ ๊ฐ€๊ณตํ•ด์„œ, ๋ชจ๋ธ์„ ์ƒ์„ฑํ•œ๋‹ค
	
	// DB ํ˜ธ์ถœ
	// ํ˜ธ์ถœ์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜ ์ƒ์„ฑ
	String driver = "org.mariadb.jdbc.Driver";
	String dbUrl = "jdbc:mariadb://127.0.0.1:3306/employees";
	String dbUser = "****";
	String dbPw = "****";
	Class.forName(driver);
	Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPw);
	
	// ์ฒซ๋ฒˆ์งธ ๋ชจ๋ธ๊ฐ’(searchWord์˜ ๊ฐ’์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ํŽ˜์ด์ง€) ๊ตฌํ•˜๊ธฐ
	// ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€
	int startRow = (currentPage - 1) * rowPerPage;
	System.out.println(startRow + " <-- empListBySearch startRow"); // ๋””๋ฒ„๊น…
	
	// ๋™์ ์ฟผ๋ฆฌ(๋ถ„๊ธฐ๋˜๋Š” ์ฟผ๋ฆฌ) ์ƒ์„ฑ
	String sql = null; // ๋™์ ์ฟผ๋ฆฌ๋Š” if๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— if๋ฌธ ๋ฐ–์—์„œ ๋จผ์ € ์„ ์–ธ
	PreparedStatement stmt = null; 
	if(searchWord.equals("")) { // if(searchWord.equals("") == true))
		sql = "SELECT emp_no empNo, birth_date birthDate, first_name firstName, last_name lastName, gender gender, hire_date hireDate FROM employees ORDER BY emp_no ASC LIMIT ?, ?";
		stmt = conn.prepareStatement(sql);
		stmt.setInt(1, startRow);
		stmt.setInt(2, rowPerPage);
	} else { // WHERE CONCAT(first_name,' ',last_name) LIKE ? ์ถ”๊ฐ€๋จ // CONCAT๋กœ ๋‘ ์ปฌ๋Ÿผ๊ฐ’์„ ํ•ฉ์ณ์ฃผ์–ด์•ผ... ์ด๋ถ€๋ถ„ ๋‹ค์‹œ ๋ณด๊ธฐ
		sql = "SELECT emp_no empNo, birth_date birthDate, first_name firstName, last_name lastName, gender gender, hire_date hireDate FROM employees WHERE CONCAT(first_name,' ',last_name) LIKE ? ORDER BY emp_no ASC LIMIT ?, ?";
		stmt = conn.prepareStatement(sql);
		stmt.setString(1, "%"+searchWord+"%");
		stmt.setInt(2, startRow);
		stmt.setInt(3, rowPerPage);
	}
	System.out.println(stmt + "<-- empListBySearch ์™„์„ฑ๋œ ๋™์ ์ฟผ๋ฆฌ");
	
	ResultSet rs = stmt.executeQuery();
	// ์ผ๋ฐ˜์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ(๋ชจ๋ธ)๋กœ ๋ณ€๊ฒฝ // ์—ฌ๋Ÿฌํ–‰(list)์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋ฏ€๋กœ ArrayList๋กœ ๋ณ€๊ฒฝ
	ArrayList<Emp> empList = new ArrayList<Emp>();
	while(rs.next()) {
		Emp e = new Emp();
		e.empNo = rs.getInt("empNo");
		e.birthDate = rs.getString("birthDate");
		e.firstName = rs.getString("firstName");
		e.lastName = rs.getString("lastName");
		e.gender = rs.getString("gender");
		e.hireDate = rs.getString("hireDate");
		empList.add(e);
	}
	// ์ฒซ๋ฒˆ์งธ ๋ชจ๋ธ๊ฐ’ ๋””๋ฒ„๊น…
	System.out.println(empList.size() + "<-- empListBySearch empList.size()"); // ArrayList๋Š” size
	for(Emp e : empList) {
		System.out.println(e.firstName + " " + e.lastName);
	}
	
	// ๋‘๋ฒˆ์งธ ๋ชจ๋ธ๊ฐ’(์ด ํŽ˜์ด์ง€ ์ˆ˜, ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€์˜ ๋„˜๋ฒ„) ๊ตฌํ•˜๊ธฐ
	// ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€
	int totalCount = 0;
	int lastPage = 0;
	
	// totalCount๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ์ฟผ๋ฆฌ ์ƒ์„ฑ
	String sql2 = "SELECT count(*) FROM employees"; // count(*) ๋ฐ์ดํ„ฐ์˜ ๋ชจ๋“  ํ–‰์˜ ์ˆ˜๋ฅผ ์„ผ๋‹ค
	PreparedStatement stmt2 = conn.prepareStatement(sql2);
	ResultSet rs2 = stmt2.executeQuery();
	// ๋ณ€์ˆ˜์— ์ €์žฅ
	if(rs2.next()) {
		totalCount = rs2.getInt("count(*)");
	}
	System.out.println(totalCount + "<-- empListBySearch ๋ชจ๋“  ๋ฐ์ดํ„ฐํ–‰์˜ ์ˆ˜");
	
	// lastPage ๊ตฌํ•˜๊ธฐ
	lastPage = totalCount / rowPerPage;
	if(totalCount % rowPerPage != 0) {
		lastPage = lastPage + 1; // ํŽ˜์ด์ง€๊ฐ€ ๋”ฑ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด ํ•œํŽ˜์ด์ง€๊ฐ€ ๋” ํ•„์š”ํ•˜๋‹ค
	}
	System.out.println(lastPage + "<-- empListBySearch ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€ ๋„˜๋ฒ„");
	
	// ๋‚˜์ด ๊ณ„์‚ฐํ•˜๊ธฐ
	Calendar today = Calendar.getInstance(); // ์˜ค๋Š˜ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ
	// ์˜ค๋Š˜ ๋‚ ์งœ์˜ ๋…„๋„,์›”,์ผ ๋ณ€์ˆ˜์— ๋ฐ›๊ธฐ
	int todayYear = today.get(Calendar.YEAR);
	int todayMonth = today.get(Calendar.MONTH) + 1; // ์ž๋ฐ”api์™€ ๋งˆ๋ฆฌ์•„db์˜ ์›” ์‹œ์ž‘์ผ์ด ๋‹ค๋ฅด๋ฏ€๋กœ +1 ํ•ด์ค€๋‹ค (์ž๋ฐ”๋Š” 0์›”๋ถ€ํ„ฐ ์‹œ์ž‘)
	int todayDate = today.get(Calendar.DATE);
	System.out.println("์˜ค๋Š˜๋‚ ์งœ: " + todayYear + "๋…„" + todayMonth + "์›”" + todayDate + "์ผ");
	
	// 3) View Layer : ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ง€๋Š” ๋ถ€๋ถ„์€ body๋ถ€ํ„ฐ...
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>empListBySearch</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
 	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<style>
	.small {width: 30px; height: 30px;}
</style>
</head>
<body>
<div class="p-5 bg-primary text-white text-center">
	<h1>์‚ฌ์› ๋ชฉ๋ก</h1>
</div>
<div class="container mt-3">
	<table class="table text-center">
		<thead class="table-primary">
			<tr><!-- ์ปฌ๋Ÿผ๋ช… -->
				<th>no</th>
				<th>age</th>
				<th>first name</th>
				<th>last name</th>
				<th>gender</th>
				<th>hire date</th>
			</tr>
		</thead>
		<tbody>
			<%
				for(Emp e : empList) {
			%>
				<tr>
					<td><%=e.empNo%>๋ฒˆ</td>
					<%
						// e.birthDate์—์„œ ๋…„๋„,์›”,์ผ intํƒ€์ž…์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
						int birthYear = Integer.parseInt(e.birthDate.substring(0,4));
						int birthMonth = Integer.parseInt(e.birthDate.substring(5,7));
						int birthDate = Integer.parseInt(e.birthDate.substring(8));
						int age = todayYear - birthYear; // ๋‚˜์ด = ์˜ค๋Š˜๋…„๋„ - ํƒœ์–ด๋‚œ๋…„๋„
							
						if(birthMonth <= todayMonth && birthDate <= todayDate) {
								// ํ˜„์žฌ ์›”์ด ํƒœ์–ด๋‚œ ์›”๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ (์ด๋ฏธ ์ง€๋‚ฌ๊ฑฐ๋‚˜ ์ด๋ฒˆ๋‹ฌ์ด๊ณ )
								// ํ˜„์žฌ ์ผ์ด ํƒœ์–ด๋‚œ ์ผ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด(์ด๋ฏธ ์ง€๋‚ฌ๊ฑฐ๋‚˜ ์˜ค๋Š˜์ด๋ฉด)
								// ์ƒ์ผ์ด ์ง€๋‚ฌ๊ฑฐ๋‚˜ ์˜ค๋Š˜์ด ์ƒ์ผ์ด๋ฏ€๋กœ +1์‚ด ํ•ด์ค€๋‹ค
								age = age + 1;
						}
					%>
					<td><%=age%>์„ธ</td>
					<td><%=e.firstName%></td>
					<td><%=e.lastName%></td>
					<td>
						<%
							if(e.gender.equals("M")) { // Stringํƒ€์ž…์ด๋ฏ€๋กœ equals ์‚ฌ์šฉ
						%>
								<img src="./img/m.JPG" class="small">
						<%
							} else {
						%>
								<img src="./img/f.JPG" class="small">
						<%
							}				
						%>
					</td>
					<td><%=e.hireDate%></td>
				</tr>
			<%
				}
			%>
		</tbody>
	</table>
	<div class="text-center">
		<!-- ์ด๋ฆ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ -->
		<form action="./empListBySearch.jsp" method="get">
			<label>์ด๋ฆ„๊ฒ€์ƒ‰ : </label>
			<input type="text" name="searchWord" value="<%=searchWord%>">
			<button type="submit">๊ฒ€์ƒ‰</button>
		</form>
	</div>
</div>
	<!-- ํŽ˜์ด์ง• -->
	<div class="mt-5 p-4 bg-secondary text-white text-center">
		<%
			if(currentPage > 1) { // ์ฒซ๋ฒˆ์งธ ํŽ˜์ด์ง€(1)๋ณด๋‹ค ํฐ ํŽ˜์ด์ง€๋ถ€ํ„ฐ "์ด์ „"์„ ์ถœ๋ ฅ
		%>
				<a href="./empListBySearch.jsp?currentPage=<%=currentPage - 1%>&searchWord=<%=searchWord%>&rowPerPage=<%=rowPerPage%>" class="btn btn-primary btn-sm"> ์ด์ „ </a>
		<%
			}
		%>
			<%=currentPage%>ํŽ˜์ด์ง€ <!-- ํ˜„์žฌํŽ˜์ด์ง€ ์ถœ๋ ฅ -->
		<%
			if(currentPage < lastPage) { // ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€ ๋„˜๋ฒ„๋ณด๋‹ค ์ž‘์€ ํŽ˜์ด์ง€๊นŒ์ง€๋งŒ "๋‹ค์Œ"์„ ์ถœ๋ ฅ
		%>
				<a href="./empListBySearch.jsp?currentPage=<%=currentPage + 1%>&searchWord=<%=searchWord%>&rowPerPage=<%=rowPerPage%>" class="btn btn-primary btn-sm"> ๋‹ค์Œ </a>
		<%
			}
		%>
	</div>
</body>
</html>
  • ์ด๋ฆ„ ๊ฒ€์ƒ‰์กฐ๊ฑด์— ๋”ฐ๋ผ ์ถœ๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์ˆ˜ ๋˜ํ•œ ๋งค๋ฒˆ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ, ํŽ˜์ด์ง•์„ ์œ„ํ•ด์„œ ์ž‘์„ฑํ•˜๋Š” ์ฟผ๋ฆฌ(count) ๋˜ํ•œ ๊ฒ€์ƒ‰ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜์—ฌ(where์ ˆ) ๋ถ„๊ธฐํ•ด์•ผ ํ•œ๋‹ค!

๐Ÿ“ ๊ฒฐ๊ณผ


๐Ÿ“Œ ์ฝ˜์†”์ฐฝ ์ถœ๋ ฅ ์ƒ‰์ƒ ๋ณ€๊ฒฝํ•˜๊ธฐ

ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋‹ค ๋ณด๋ฉด ๋””๋ฒ„๊น…์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ˜์†”์ฐฝ์ด ์–ด์ง€๋Ÿฌ์›Œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ๋˜ํ•œ ์ถ”ํ›„ ํ˜‘์—… ์ง„ํ–‰์‹œ์—๋„ ๋ˆ„๊ฐ€ ์ž‘์„ฑํ•œ ๋””๋ฒ„๊น… ์ฝ”๋“œ์ธ์ง€ ์‰ฝ๊ฒŒ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ์ฝ˜์†”์ฐฝ์˜ ์ถœ๋ ฅ ์ƒ‰์ƒ์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์ข‹๋‹ค.

๐Ÿ“ ์ž‘์„ฑ

// ์ฝ˜์†”์ฐฝ ์ถœ๋ ฅ๋‚ด์šฉ์— ์ƒ‰๊น” ์ž…ํžˆ๊ธฐ
// RESET ANSI CODE ์‚ฌ์šฉ

/*
	RED -> \u001B[31m
	GREEN -> \u001B[32m
	
	RED_background -> \u001B[41m
	GREEN_background -> \u001B[42m
	                           
	RESET -> \u001B[0m
	๋ฆฌ์…‹ ์ฝ”๋“œ๋ฅผ ๋’ค์— ๋ถ™์—ฌ์ฃผ์ง€ ์•Š์œผ๋ฉด ๋ชจ๋“  ์ฝ˜์†” ์ถœ๋ ฅ๋‚ด์šฉ์ด ํ•ด๋‹น ์ƒ‰๊น”๋กœ ๋ฐ”๋€œ
*/

// font color
System.out.println("\u001B[31m hello \u001B[0m");
System.out.println("\u001B[32m hello \u001B[0m");

// background color
System.out.println("\u001B[41m hello \u001B[0m");
System.out.println("\u001B[42m hello \u001B[0m");

// ์“ฐ๊ธฐ ๊ท€์ฐฎ๋‹ค๋ฉด..? ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์ž!
// final์€ ๋”์ด์ƒ ๋ฐ”๊ฟ€ ์ผ์ด ์—†๋Š” ๋ณ€์ˆ˜์— ๋ถ™์ธ๋‹ค // final ๋ณ€์ˆ˜๋Š” ํ†ต์ƒ์ ์œผ๋กœ ๋Œ€๋ฌธ์ž
final String RED = "\u001B[31m";
final String BG_RED = "\u001B[41m";
final String GREEN = "\u001B[32m";
final String BG_GREEN = "\u001B[42m";
final String RESET = "\u001B[0m";

System.out.println(RED+"hello"+RESET);
System.out.println(BG_RED+"hello"+RESET);
System.out.println(GREEN+"hello"+RESET);
System.out.println(BG_GREEN+"hello"+RESET);

// ๋” ๋‹ค์–‘ํ•œ ์ƒ‰์ƒ์€ ๊ตฌ๊ธ€๋ง
  • ์ƒ‰์ƒ ์ฝ”๋“œ๋ฅผ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ํŽธํ•˜๋‹ค!

๐Ÿ“ ๊ฒฐ๊ณผ



๐Ÿ’ก ๊ณผ์ œ

์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๊ธฐ๋Šฅ์œผ๋กœ ์„ฑ๋ณ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ๊ณผ ์ด๋ฆ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ํ•œํŽ˜์ด์ง€์— ํ•ฉ์ณ๋ณด์•˜๋‹ค. (+์ฝ˜์†” ์ถœ๋ ฅ ์ƒ‰์ƒ ๋ณ€๊ฒฝ!)

๐Ÿ“ ์ž‘์„ฑ

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "vo.*" %>
<%@ page import = "java.util.*" %>
<%
	// ์ฝ˜์†”์ฐฝ ์ถœ๋ ฅ ์ƒ‰์ƒ ์ง€์ •
	final String RED = "\u001B[31m";
	final String BG_RED = "\u001B[41m";
	final String GREEN = "\u001B[32m";
	final String BG_GREEN = "\u001B[42m";
	final String RESET = "\u001B[0m";
		
	// ์„ฑ๋ณ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ, ์ด๋ฆ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ ์ถ”๊ฐ€
	
	// Controller Layer : ์š”์ฒญ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„
	// ์š”์ฒญ์ฒ˜๋ฆฌํ•ด์•ผํ•  ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’: currentPage, rowPerPage, searchWord, gender
	// currentPage(null, ๊ฐ’), rowPerPage(null, ๊ฐ’), gender(null, "", "M/F") , searchWord(null, "", "๊ฐ’")
	
	// ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์ด null์ธ์ง€ ๋””๋ฒ„๊น…
	System.out.println(RED+request.getParameter("currentPage") + " <-- empListBySearch param currentPage");
	System.out.println(request.getParameter("rowperPage") + " <-- empListBySearch param rowperPage");
	System.out.println(request.getParameter("gender") + " <-- empListBySearch param gender");
	System.out.println(request.getParameter("searchWord") + " <-- empListBySearch param searchWord"+RESET);
	
	// ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ null ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ (null์ด์–ด๋„ ๋˜๋Š”์ง€, null์ด๋ฉด ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€ ๊ฒฐ์ •)
	/* 
		int currentPage; // ๋ณ€์ˆ˜์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋•Œ๋ฌธ์— if๋ฌธ ๋ธ”๋ก ๋ฐ–์—์„œ ์„ ์–ธ
		if(request.getParameter("currentPage") != null) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		} else { // null์ผ ๊ฒฝ์šฐ
			currentPage = 1; // currentPage์˜ ๊ฐ’์ด 1์ด์—ˆ๋‹ค๋ฉด ์ด ๋ถ€๋ถ„์€ ์ƒ๋žต ๊ฐ€๋Šฅ 
		}
		์ฆ‰, null์ผ ๊ฒฝ์šฐ์— ์ค˜์•ผํ•  ๊ฐ’์„ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ์ฃผ๊ณ  ์‹œ์ž‘ํ–ˆ๋‹ค๋ฉด if๋ฌธ ์ž‘์„ฑ์‹œ์— else์„ ์ž‘์„ฑํ•  ์ˆ˜๊ณ ๋ฅผ ๋œ ์ˆ˜ ์žˆ๋‹ค
	*/
	
	// currentPage : null์ด ์•„๋‹ ๊ฒฝ์šฐ Integer๋กœ int ๋ณ€์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ ์ €์žฅ, null์ผ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’(1ํŽ˜์ด์ง€) ์ €์žฅ
	int currentPage = 1;
	if(request.getParameter("currentPage") != null) {
		currentPage = Integer.parseInt(request.getParameter("currentPage"));
	}
	// rowPerPage : null์ด ์•„๋‹ ๊ฒฝ์šฐ Integer๋กœ int ๋ณ€์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ ์ €์žฅ, null์ผ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’(10๊ฐœ) ์ €์žฅ
	int rowPerPage = 10;
	if(request.getParameter("rowPerPage") != null) {
		rowPerPage = Integer.parseInt(request.getParameter("rowPerPage"));
	}
	// gender : null์ด ์•„๋‹ ๊ฒฝ์šฐ String ๋ณ€์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ ์ €์žฅ, null์ผ ๊ฒฝ์šฐ ""(๊ณต๋ฐฑ) ์ €์žฅ
	String gender = "";
	if(request.getParameter("gender") != null) {
		gender = request.getParameter("gender");
	}
	// searchWord : null์ด ์•„๋‹ ๊ฒฝ์šฐ String ๋ณ€์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’ ์ €์žฅ, null์ผ ๊ฒฝ์šฐ ""(๊ณต๋ฐฑ) ์ €์žฅ
	String searchWord = "";
	if(request.getParameter("searchWord") != null) {
		searchWord = request.getParameter("searchWord");
	}
	// null์ผ ๊ฒฝ์šฐ์— ""(๊ณต๋ฐฑ)์œผ๋กœ ์ฃผ๋Š” ์ด์œ  -> null์ด๊ฑฐ๋‚˜ ๊ณต๋ฐฑ์ผ ๋•Œ ๋‘˜๋‹ค ์‹คํ–‰ํ•  ์ฟผ๋ฆฌ๋Š” ๊ฐ™์œผ๋ฏ€๋กœ, ๊ฐ™์€ ์ฟผ๋ฆฌ๋กœ ๋‘๋ฒˆ ๋ถ„๊ธฐํ•˜์ง€ ์•Š์•„๋„ ๋˜๋„๋ก ""(๊ณต๋ฐฑ)์œผ๋กœ ํ†ต์ผ
	
	// ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ํ›„(๊ฒฐ๊ณผ ๋ณ€์ˆ˜) ๋””๋ฒ„๊น…
	System.out.println(GREEN+currentPage + " <-- empListBySearch currentPage");
	System.out.println(rowPerPage + " <-- empListBySearch rowperPage");
	System.out.println(gender + " <-- empListBySearch gender");
	System.out.println(searchWord + " <-- empListBySearch searchWord");
    
	// Model Layer : ๋ชจ๋ธ๊ฐ’(view์— ์ถœ๋ ฅํ•  ๋ชจ๋“  ๋‚ด์šฉ)์„ ์ƒ์„ฑํ•˜๊ธฐ ๊นŒ์ง€์˜ ๋ชจ๋“  ๋ถ€๋ถ„
	// Controller Layer์—์„œ ์ฒ˜๋ฆฌํ•œ ๊ฒฐ๊ณผ ๋ณ€์ˆ˜(currentPage, rowPerPage, gender, searchWord)๋ฅผ ๊ฐ€๊ณตํ•ด์„œ, ๋ชจ๋ธ๊ฐ’์„ ์ƒ์„ฑํ•œ๋‹ค
	
	// DB ํ˜ธ์ถœ // ํ˜ธ์ถœ์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜ ์ƒ์„ฑ
	String driver = "org.mariadb.jdbc.Driver";
	String dbUrl = "jdbc:mariadb://127.0.0.1:3306/employees";
	String dbUser = "****";
	String dbPw = "****";
	Class.forName(driver);
	Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPw);
	
	// ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€
	int startRow = (currentPage - 1) * rowPerPage;
	System.out.println(startRow + " <-- empListBySearch startRow"+RESET); // ๋””๋ฒ„๊น…
	
	// ์ฒซ๋ฒˆ์งธ ๋™์  ์ฟผ๋ฆฌ (๋ถ„๊ธฐ๋˜๋Š” ์ฟผ๋ฆฌ)
	// ์ž‘์„ฑ ์ˆœ์„œ: ๊ฐ€์žฅ ์ฟผ๋ฆฌ๊ฐ€ ๊ฐ„๋‹จํ•œ ์กฐ๊ฑด ๋จผ์ € ์ž‘์„ฑ (๋ฌผ์Œํ‘œ๊ฐ€ ์ ๊ฑฐ๋‚˜ ์ฟผ๋ฆฌ๊ฐ€ ๋œ ๋ณต์žกํ•œ ๊ฒƒ๋ถ€ํ„ฐ)
	// 1) ๋‘˜๋‹ค ๊ณต๋ฐฑ 2) searchWord๋งŒ ๊ณต๋ฐฑ(gender์˜ ๊ฐ’์ด ์žˆ๋Š”๊ฒฝ์šฐ) 3) gender๋งŒ ๊ณต๋ฐฑ(searchWord์˜ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ) 4) ๋‘˜๋‹ค ๊ฐ’์ด ์žˆ์Œ
	// 1) SELECT * FROM employees ORDER BY emp_no ASC LIMIT ?, ?
	// 2) SELECT * FROM employees WHERE gender = ? ORDER BY emp_no ASC LIMIT ?, ?
	// 3) SELECT * FROM employees WHERE CONCAT(first_name,' ',last_name) LIKE ? ORDER BY emp_no ASC LIMIT ?, ?
	// 4) SELECT * FROM employees WHERE gender = ? AND CONCAT(first_name,' ',last_name) LIKE ? ORDER BY emp_no ASC LIMIT ?, ?
	// ODERT BY์˜ ๊ธฐ๋ณธ๊ฐ’์ด ๊ธฐ๋ณธํ‚ค(emp_no) ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ(ASC)์ด๋ฏ€๋กœ ์ƒ๋žต๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์ „๋ถ€ ์ž‘์„ฑํ–ˆ๋‹ค
			
	// ๋™์ ์ฟผ๋ฆฌ๋Š” if๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— if๋ฌธ ๋ฐ–์—์„œ ๋จผ์ € ์„ ์–ธ
	String sql = null;
	PreparedStatement stmt = null;
	if(gender.equals("") && searchWord.equals("")) { // 1) ๋‘˜๋‹ค ๊ณต๋ฐฑ
		sql = "SELECT emp_no empNo, birth_date birthDate, first_name firstName, last_name lastName, gender gender, hire_date hireDate FROM employees ORDER BY emp_no ASC LIMIT ?, ?";
		stmt = conn.prepareStatement(sql);
		stmt.setInt(1, startRow);
		stmt.setInt(2, rowPerPage);
	} else if(!gender.equals("") && searchWord.equals("")) { // 2) searchWord๋งŒ ๊ณต๋ฐฑ(gender์˜ ๊ฐ’์ด ์žˆ๋Š”๊ฒฝ์šฐ)
 		sql = "SELECT emp_no empNo, birth_date birthDate, first_name firstName, last_name lastName, gender gender, hire_date hireDate FROM employees WHERE gender = ? ORDER BY emp_no ASC LIMIT ?, ?";
		stmt = conn.prepareStatement(sql);
		stmt.setString(1, gender);
		stmt.setInt(2, startRow);
		stmt.setInt(3, rowPerPage);
	} else if(gender.equals("") && !searchWord.equals("")) { //  3) gender๋งŒ ๊ณต๋ฐฑ(searchWord์˜ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ)
		sql = "SELECT emp_no empNo, birth_date birthDate, first_name firstName, last_name lastName, gender gender, hire_date hireDate FROM employees WHERE CONCAT(first_name,' ',last_name) LIKE ? ORDER BY emp_no ASC LIMIT ?, ?";
		stmt = conn.prepareStatement(sql);
		stmt.setString(1, "%"+searchWord+"%");
		stmt.setInt(2, startRow);
		stmt.setInt(3, rowPerPage);
	} else { // 4) ๋‘˜๋‹ค ๊ฐ’์ด ์žˆ์Œ
		sql = "SELECT emp_no empNo, birth_date birthDate, first_name firstName, last_name lastName, gender gender, hire_date hireDate FROM employees WHERE gender = ? AND CONCAT(first_name,' ',last_name) LIKE ? ORDER BY emp_no ASC LIMIT ?, ?";
		stmt = conn.prepareStatement(sql);
		stmt.setString(1, gender);
		stmt.setString(2, "%"+searchWord+"%");
		stmt.setInt(3, startRow);
		stmt.setInt(4, rowPerPage);
	}
	// ์™„์„ฑ๋œ ๋™์  ์ฟผ๋ฆฌ ๋””๋ฒ„๊น… ํ•ด๋ณด๊ธฐ
	System.out.println(BG_GREEN+stmt + " <-- empList ์™„์„ฑ๋œ ์ฒซ๋ฒˆ์งธ ๋™์  ์ฟผ๋ฆฌ"+RESET);
	
	ResultSet rs = stmt.executeQuery();
	// ์ผ๋ฐ˜์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ(๋ชจ๋ธ)๋กœ ๋ณ€๊ฒฝ // ์—ฌ๋Ÿฌํ–‰(list)์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋ฏ€๋กœ ArrayList๋กœ ๋ณ€๊ฒฝ
	ArrayList<Emp> empList = new ArrayList<Emp>();
	while(rs.next()) {
		Emp e = new Emp();
		e.empNo = rs.getInt("empNo");
		e.birthDate = rs.getString("birthDate");
		e.firstName = rs.getString("firstName");
		e.lastName = rs.getString("lastName");
		e.gender = rs.getString("gender");
		e.hireDate = rs.getString("hireDate");
		empList.add(e);
	}
	
	// ๋ชจ๋ธ์„ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ถ”๊ฐ€
	int totalCount = 0; // count(*)๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค
	int lastPage = 0; // totalCount / rowPerPage๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค
		
	// ๋‘๋ฒˆ์งธ ๋™์  ์ฟผ๋ฆฌ
	// totalCount์™€ lastPage์˜ ๊ฐ’์ด ๊ฒ€์ƒ‰ ์กฐ๊ฑด(์ฒซ๋ฒˆ์งธ ๋™์ ์ฟผ๋ฆฌ์˜ ์กฐ๊ฑด)์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ if๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผํ•œ๋‹ค
	// SELECT count(*) FROM employees ์— ์กฐ๊ฑด์— ๋”ฐ๋ผ WHERE์„ ์ถ”๊ฐ€
	String sql2 = null;
	PreparedStatement stmt2 = null;
	if(gender.equals("") && searchWord.equals("")) { // 1) ๋‘˜๋‹ค ๊ณต๋ฐฑ
		sql2 = "SELECT count(*) FROM employees";
		stmt2 = conn.prepareStatement(sql2);
	} else if(!gender.equals("") && searchWord.equals("")) { // 2) searchWord๋งŒ ๊ณต๋ฐฑ(gender์˜ ๊ฐ’์ด ์žˆ๋Š”๊ฒฝ์šฐ)
		sql2 = "SELECT count(*) FROM employees WHERE gender = ?";
		stmt2 = conn.prepareStatement(sql2);
		stmt2.setString(1, gender);
	} else if(gender.equals("") && !searchWord.equals("")) { // 3) gender๋งŒ ๊ณต๋ฐฑ(searchWord์˜ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ)
		sql2 = "SELECT count(*) FROM employees WHERE CONCAT(first_name,' ',last_name) LIKE ?";
		stmt2 = conn.prepareStatement(sql2);
		stmt2.setString(1, "%"+searchWord+"%");
	} else { // 4) ๋‘˜๋‹ค ๊ฐ’์ด ์žˆ์Œ
		sql2 = "SELECT count(*) FROM employees WHERE gender = ? AND CONCAT(first_name,' ',last_name) LIKE ?";
		stmt2 = conn.prepareStatement(sql2);
		stmt2.setString(1, gender);
		stmt2.setString(2, "%"+searchWord+"%");
	}
	// ์™„์„ฑ๋œ ๋™์  ์ฟผ๋ฆฌ ๋””๋ฒ„๊น… ํ•ด๋ณด๊ธฐ
	System.out.println(BG_RED+stmt2 + " <-- empList ์™„์„ฑ๋œ ๋‘๋ฒˆ์งธ ๋™์  ์ฟผ๋ฆฌ"+RESET);
	
	ResultSet rs2 = stmt2.executeQuery();
	// ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅ
	if(rs2.next()) {
		totalCount = rs2.getInt("count(*)");
	}
	System.out.println(GREEN+totalCount + " <-- empListBySearch ๋ชจ๋“  ๋ฐ์ดํ„ฐํ–‰์˜ ์ˆ˜");
	// lastPage ๊ตฌํ•˜๊ธฐ
	lastPage = totalCount / rowPerPage;
	if(totalCount % rowPerPage != 0) { // ํŽ˜์ด์ง€๊ฐ€ ๋”ฑ ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด ๋‚˜๋จธ์ง€๊ฐ€ ์ถœ๋ ฅ๋  ํ•œํŽ˜์ด์ง€๊ฐ€ ๋” ํ•„์š”ํ•˜๋ฏ€๋กœ
		lastPage = lastPage + 1; 
	}
	System.out.println(lastPage + " <-- empListBySearch ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€ ๋„˜๋ฒ„");
		
	// ๋‚˜์ด ๊ณ„์‚ฐํ•˜๊ธฐ
	Calendar today = Calendar.getInstance(); // ์˜ค๋Š˜ ๋‚ ์งœ ๊ตฌํ•˜๊ธฐ
	// ์˜ค๋Š˜ ๋‚ ์งœ์˜ ๋…„๋„,์›”,์ผ ๋ณ€์ˆ˜์— ๋ฐ›๊ธฐ
	int todayYear = today.get(Calendar.YEAR);
	int todayMonth = today.get(Calendar.MONTH) + 1; // ์ž๋ฐ”api์™€ ๋งˆ๋ฆฌ์•„db์˜ ์›” ์‹œ์ž‘์ผ์ด ๋‹ค๋ฅด๋ฏ€๋กœ +1 ํ•ด์ค€๋‹ค (์ž๋ฐ”๋Š” 0์›”๋ถ€ํ„ฐ ์‹œ์ž‘)
	int todayDate = today.get(Calendar.DATE);
	System.out.println("์˜ค๋Š˜๋‚ ์งœ: " + todayYear + "๋…„" + todayMonth + "์›”" + todayDate + "์ผ"+RESET);
		
	// View Layer : ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ง€๋Š” ๋ถ€๋ถ„์€ body๋ถ€ํ„ฐ...
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>empList3.jsp</title>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
 	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<style>
	.small {width: 30px; height: 30px;}
</style>
</head>
<body>
<div class="container mt-3">
	<div class="p-5 bg-primary text-white text-center">
		<h1>์‚ฌ์› ๋ชฉ๋ก</h1>
	</div>
	<table class="table text-center">
		<thead class="table-primary">
			<tr>
				<th>no</th>
				<th>age</th>
				<th>first name</th>
				<th>last name</th>
				<th>gender</th>
				<th>hire date</th>
			</tr>
		</thead>
		<tbody>
			<%
				for(Emp e : empList) {
			%>
				<tr>
					<td><%=e.empNo%>๋ฒˆ</td>
					<%
						// e.birthDate์—์„œ ๋…„๋„,์›”,์ผ intํƒ€์ž…์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
						int birthYear = Integer.parseInt(e.birthDate.substring(0,4));
						int birthMonth = Integer.parseInt(e.birthDate.substring(5,7));
						int birthDate = Integer.parseInt(e.birthDate.substring(8));
						int age = todayYear - birthYear; // ์˜ค๋Š˜๋…„๋„ - ํƒœ์–ด๋‚œ๋…„๋„ = ๋งŒ๋‚˜์ด
							
						if(birthMonth <= todayMonth && birthDate <= todayDate) {
							// ํ˜„์žฌ ์›”์ด ํƒœ์–ด๋‚œ ์›”๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ (์ด๋ฏธ ์ง€๋‚ฌ๊ฑฐ๋‚˜ ์ด๋ฒˆ๋‹ฌ์ด๊ณ )
							// ํ˜„์žฌ ์ผ์ด ํƒœ์–ด๋‚œ ์ผ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด(์ด๋ฏธ ์ง€๋‚ฌ๊ฑฐ๋‚˜ ์˜ค๋Š˜์ด๋ฉด)
							// ์ƒ์ผ์ด ์ง€๋‚ฌ๊ฑฐ๋‚˜ ์˜ค๋Š˜์ด ์ƒ์ผ์ด๋ฏ€๋กœ +1์‚ด ํ•ด์ค€๋‹ค
							age = age + 1;
						}
					%>
					<td><%=age%>์„ธ</td>
					<td><%=e.firstName%></td>
					<td><%=e.lastName%></td>
					<td>
						<%
							if(e.gender.equals("M")) {
						%>
								<img src="./img/m.JPG" class="small">
						<%
							} else {
						%>
								<img src="./img/f.JPG" class="small">
						<%
							}				
						%>
					</td>
					<td><%=e.hireDate%></td>
				</tr>
			<%
				}
			%>
		</tbody>
	</table>
	<!-- ์š”์ฒญ ํผ -->
	<div class="text-center">
		<form method="get" action="./empList3.jsp">
			<label>์„ฑ๋ณ„ : </label> 
			<select name="gender">
				<%
					if(gender.equals("")) {
				%>
						<option value="" selected="selected">์„ ํƒ</option>
						<option value="M">๋‚จ</option>
						<option value="F">์—ฌ</option>
				<%		
					} else if(gender.equals("M")) {
				%>
						<option value="">์„ ํƒ</option>
						<option value="M" selected="selected">๋‚จ</option>
						<option value="F">์—ฌ</option>
				<%
					} else {
				%>
						<option value="">์„ ํƒ</option>
						<option value="M">๋‚จ</option>
						<option value="F" selected="selected">์—ฌ</option>
				<%		
					}
				%>
			</select>
			<label>์ด๋ฆ„๊ฒ€์ƒ‰ : </label>
			<input type="text" name="searchWord" value="<%=searchWord%>">
			
			<!-- 
			<label>์ž…์‚ฌ๋…„๋„ : </label>
			<input type="number" name="beginYear"> 
			~ 
			<input type="number" name="endYear">
			-->
			
			<button type="submit">์กฐํšŒ</button>
		</form>
	</div>
	<!-- ํŽ˜์ด์ง€ ๋„ค๋น„๊ฒŒ์ด์…˜ -->
	<div class="mt-5 p-4 bg-secondary text-white text-center">
		<%
			if(currentPage > 1) { // ์ฒซ๋ฒˆ์งธ ํŽ˜์ด์ง€(1)๋ณด๋‹ค ํฐ ํŽ˜์ด์ง€๋ถ€ํ„ฐ "์ด์ „"์„ ์ถœ๋ ฅ
		%>
				<a href="./empList3.jsp?currentPage=<%=currentPage - 1%>&rowPerPage=<%=rowPerPage%>&gender=<%=gender%>&searchWord=<%=searchWord%>" class="btn btn-primary btn-sm"> ์ด์ „ </a>
		<%
			}
		%>
			<%=currentPage%>ํŽ˜์ด์ง€ <!-- ํ˜„์žฌํŽ˜์ด์ง€ ์ถœ๋ ฅ -->
		<%
			if(currentPage < lastPage) { // ๋งˆ์ง€๋ง‰ ํŽ˜์ด์ง€ ๋„˜๋ฒ„๋ณด๋‹ค ์ž‘์€ ํŽ˜์ด์ง€๊นŒ์ง€๋งŒ "๋‹ค์Œ"์„ ์ถœ๋ ฅ
		%>
				<a href="./empList3.jsp?currentPage=<%=currentPage + 1%>&rowPerPage=<%=rowPerPage%>&gender=<%=gender%>&searchWord=<%=searchWord%>" class="btn btn-primary btn-sm"> ๋‹ค์Œ </a>
		<%
			}
		%>
	</div>
</div>
</body>
</html>
  • ์„ฑ๋ณ„๊ณผ ์ด๋ฆ„ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์ด ํ•ฉ์ณ์ง„ ๋งŒํผ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๋ฏ€๋กœ if๋ฌธ์˜ ๋ถ„๊ธฐ๊ฐ€ ๋งŽ์•„์ง„๋‹ค!

๐Ÿ“ ๊ฒฐ๊ณผ




profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ๐Ÿ’ช ์ดˆ๋ณด ๊ฐœ๋ฐœ์ž ๋„์ „๊ธฐ ๐ŸŒฑ๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€