본문 바로가기

JSP, Servlet/JSP, Oracle 기초

[JSP] 사원정보 입력 후 테이블 형태로 출력

먼저 DB에 테이블을 생성한다

 

create table employee(name varchar2(20), address varchar2(100), jumin_no char(14));

 

회원정보를 입력한다.

<04_addEmployee.jsp>

<%@ 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.Statement"%>
<%@ 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 employee values(?, ?, ?)";
	String[] name = { "duke", "pororo", "candy" };
	String[] address = { "seoul", "busan", "daejeon" };
	String[] jumin_no = { "970224-1039234", "001222-1038782", "981221-1829192"};%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		try {
		Class.forName("oracle.jdbc.driver.OracleDriver");
		conn = DriverManager.getConnection(url, uid, pass);
		pstmt = conn.prepareStatement(sql);
		
		for (int i = 0; i < 3; i++) {
			pstmt.setString(1, name[i]);
			pstmt.setString(2, address[i]);
			pstmt.setString(3, jumin_no[i]);
			pstmt.executeUpdate();
		}

		
		pstmt.close();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (conn != null) {
		conn.close();
			}
			if (pstmt != null) {
		pstmt.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	%>
	<h3>회원 정보 입력 완료</h3>
	<a href="04_employeeResult.jsp">회원 정보 확인</a>
</body>
</html>

 

 

사원정보를 출력한다.

<04_employeeResult.jsp>

<%@ 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.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 employee";%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
		try {
		// 데이터베이스를 접속하기 위한 드라이버 SW 로드
		Class.forName("oracle.jdbc.driver.OracleDriver");
		// 데이터베이스에 연결하는 작업 수행
		conn = DriverManager.getConnection(url, uid, pwd);
		// 쿼리를 생성gkf 객체 생성
		stmt = conn.createStatement();
		// 쿼리 생성
		rs = stmt.executeQuery(sql);
	%>
	<table border="1">
		<tr>
			<td>이름</td>
			<td>주소</td>
			<td>주민등록변호</td>
		</tr>
		<%
			while (rs.next()) {
		%>
		<tr>
			<td><%=rs.getString("name")%></td>
			<td><%=rs.getString("address")%></td>
			<td><%=rs.getString("jumin_no")%></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();
		}
		}
		%>
</body>
</html>

 

2번 실행해서 2번 중복된걸 볼 수 있다.

728x90
반응형