본문 바로가기

JSP, Servlet/JSP, Oracle 기초

[JSP] 상품정보 입력 후 출력(fmt태그 사용)

상품 정보를 입력 받아 출력하는 프로그램을 작성해보자.

 

먼저 테이블을 생성한다.

create table item(
name varchar2(20), -- 상품 제목
price number(10), -- 가격
description varchar2(1000) -- 상품설명
);

 

그리고 상품 정보 입력할 폼을 생성한다.

<05_itemWriteForm.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 method="POST" action="05_itemWrite.jsp">
		<table>
			<tr>
				<td>이름</td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td>가격</td>
				<td><input type="text" name="price"></td>
			</tr>
			<tr>
				<td>설명</td>
				<td><textarea rows="8" cols="40" name="description"></textarea></td>
			</tr>
			<tr>
				<td><input type="submit" value="전송"><input type="reset" value="취소"></td>
			</tr>
		</table>
	</form>
</body>
</html>

 

상품정보입력폼에서 전달받은 데이터를 위에서 생성한 DB테이블에 저장한다.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.PreparedStatement"%>
<%!Connection conn = null;
	PreparedStatement pstmt = null;

	String url = "jdbc:oracle:thin:@localhost:1521:XE";
	String uid = "ora_user";
	String pass = "ora123";

	String sql = "insert into item values(?, ?, ?)";%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("UTF-8");
	String name = request.getParameter("name");
	int price = Integer.parseInt(request.getParameter("price"));
	String description = request.getParameter("description");
	

	try {
		Class.forName("oracle.jdbc.driver.OracleDriver");
		conn = DriverManager.getConnection(url, uid, pass);
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, name);
		pstmt.setInt(2, price);
		pstmt.setString(3, description);

		pstmt.executeUpdate();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (pstmt != null) {
		pstmt.close();
			}
			if (conn != null) {
		conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	%>
	<a href="05_itemResult.jsp">결과 보기</a>
</body>
</html>

 

입력받은 DB데이터로 테이블을 생성하여 화면에 나타낸다

<05_itemResult.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.sql.ResultSet"%>
<%!// 변수 선언
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	String uid = "ora_user";
	String pwd = "ora123";
	String url = "jdbc:oracle:thin:@localhost:1521:XE";
	String sql = "select * from item";%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>입력 완료된 정보</h3>
	<%
		try {
		// 데이터베이스를 접속하기 위한 드라이버 SW 로드
		Class.forName("oracle.jdbc.driver.OracleDriver");
		// 데이터베이스에 연결하는 작업 수행
		conn = DriverManager.getConnection(url, uid, pwd);
		// 쿼리를 생성할 객체 생성
		stmt = conn.createStatement();
		// 쿼리 생성
		rs = stmt.executeQuery(sql);
	%>
	<table border="1">
		<tr>
			<td>상품명</td>
			<td>가격</td>
			<td>설명</td>
		</tr>
		<%
			while (rs.next()) {
			String name = rs.getString("name");
			int price = rs.getInt("price");
			// request.setAttribute("price", price)
			String description = rs.getString("description");
		%>
		<tr>
			<td><%=name%></td>
			<td><fmt:formatNumber type="currency" value="<%=price%>" /></td> <!-- value = "${price} -->
			<td><%=description%></td>
		</tr>


		<%
			}
		} catch (Exception e) {
		e.printStackTrace();
		} finally {
		try {
		if (rs != null) {
			rs.close();
		}
		if (stmt != null) {
			stmt.close();
		}
		if (conn != null) {
			conn.close();
		}
		} catch (Exception e) {
		e.printStackTrace();
		}
		}
		%>
	</table>
</body>
</html>

 

 

728x90
반응형