Web Programming/Project

[게시판 만들기] 1-4. JSP - Oracle 을 이용한 Model1 입니다.

유가엘 2020. 3. 26. 11:23

lesson13_JSP_Board_MVC.zip
0.01MB

데이터베이스의 테이블 생성당시 BOARD_IDX 는 Primary Key ( 중복 허용 안함 ) 은 고유한 값을 가지므로, 하나씩 넣어주는 것은 매우 비 효율적인 작업입니다.

 

이를 위해 오라클에서는 시퀀스를 제공합니다. ( 생성 시 옵션은 생략하였습니다. )

생성된 시퀀스는 아래와 같은 방법으로 사용할 수 있습니다.

 

insert.jsp 에서 수기로 기재 하였던 BOARD_IDX 에 시퀀스를 적용합니다.

또한 finally 구문에 성공 실패 여부와 상관없이 index.jsp로 이동하는 구문을 작성해주면 insert에 관련된 작업은 모두 끝났습니다.


Select 진행 순서

index.jsp -> list.jsp (페이지의 시작은 index에서 시작하며 redirect를 통해 list로 이동합니다.)

 

다음으로 데이터베이스에 등록된 정보를 브라우저에서 보여주기 위하여 list.jsp를 수정해야 합니다.

Select 구문은 삽입,변경,삭제와 다르게 ResultSet을 반환합니다.

이를 위해 상단에 초기화 한 후, ResultSet result = stmt.executeQuery(sql); 을 사용 합니다.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
		String driver="oracle.jdbc.driver.OracleDriver";
		String url="jdbc:oracle:thin:@localhost:1521:orcl";
		String id = "tutorial";
		String pw = "12341234";
		
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		
		try {
			Class.forName(driver);
			con=DriverManager.getConnection(url,id,pw);
			stmt=con.createStatement();
			String sql = "SELECT * FROM BOARD";
			result = stmt.executeQuery(sql);
%>

<body>
	<h4>MVC1 게시판</h4>
	
	<table border="1">
		<thead>
			<tr>
				<th>번호</th>
				<th>제목</th>
				<th>작성자</th>
				<th>날짜</th>
				<th>조회수</th>
			</tr>
		</thead>
		<tbody>
<%
			while (result.next()) {
				out.print("<tr>");
				out.print("<td>"+result.getString("BOARD_IDX")+"</td>");
				out.print("<td>"+result.getString("BOARD_TITLE")+"</td>");
				out.print("<td>"+result.getString("BOARD_WRITER")+"</td>");
				out.print("<td>"+result.getString("BOARD_WRITEDATE")+"</td>");
				out.print("<td>"+result.getString("BOARD_COUNT")+"</td>");
				out.print("</tr>");
			}
%>
		</tbody>
	</table>
	
	<!-- WebContent/Board/write.jsp 로 이동합니다. -->
	<a href="write.jsp">글쓰기로 이동</a>
	
<%
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

%>
</body>
</html>

작업이 완료 되었다면 데이터베이스 목록을 웹브라우저에서 출력되는 것을 확인 할 수 있습니다.