본문 바로가기
웹 프로그래밍(풀스택-->java)/웹프로그래밍(백엔드-->java)

17. JSP와 데이터베이스 연동 2

by 백엔드개발자0107 2021. 11. 28.

오늘은  데이터베이스 연동 두번쨰 시간을 배워볼것이다.

 

멤버테이블 생성, jsp를 이용해서 usebean을 이용하는것등을 배워볼것이다.

 

그리고 jsp에서 오라클 접속하면 시간이 맞을 것이다.

 

지난 시간에는 javaBean까지 공부했다.

 

우리는 mysqlBench가 있다.

 

자 테이블부터 만들어야하는데, 테이블을 create해야한다.

 

자, 이런 mysqlworkbench같은거를 이용하면, 클릭으로 테이블을 만들수가 있다.

 

테이블 이름은 Member로 짓자,

 

대충 이런식으로 만든다고 생각하면 될듯하다.

 

MemberJoin.jsp

 

<!-- 
자바빈을 사용하여 , 데이터베이스 데이터를 저장하고 이용할수 있게끔한다.
고로 db패키지를 만든다.
MemberBean 클래스를만든다.  
 -->


<%@ 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>
	<center>
		<h2>회원 가입</h2>
		<form action="MemberJoinProc.jsp">
			<table width="500" border="1">
				<tr height="50">
					<td width="150" align="center">아이디</td>
					<td width="350" align="center"><input type="text" name="id"
						size="40" placeholder="id를 넣으세요"></td>
				</tr>

				<tr height="50">
					<td width="150" align="center">패스워드</td>
					<td width="350" align="center"><input type="password"
						name="pass1" size="40" placeholder="비밀번호는 숫자와 영어만 넣어주세요">
					</td>
				</tr>

				<tr height="50">
					<td width="150" align="center">이메일</td>
					<td width="350" align="center"><input type="email"
						name="email" size="40"></td>
				</tr>

				<tr height="50">
					<td width="150" align="center">전화 번호</td>
					<td width="350" align="center"><input type="tel" name="tel"
						size="40"></td>
				</tr>

				<tr height="50">
					<td width="150" align="center">당신의 관심 분야</td>
					<td width="350" align="center"><input type="checkbox"
						name="hobby" value="캠핑">캠핑&nbsp; <input type="checkbox"
						name="hobby" value="등산">등산&nbsp; <input type="checkbox"
						name="hobby" value="독서">독서&nbsp; <input type="checkbox"
						name="hobby" value="음악">음악&nbsp;</td>
				</tr>

				<tr height="50">
					<td width="150" align="center">당신의 직업은</td>
					<td width="350" align="center"><select name="job">
							<option value="교사">교사</option>
							<option value="의사">의사</option>
							<option value="변호사">변호사</option>
							<option value="기술사">기술사</option>
					</select></td>
				</tr>

				<tr height="50">
					<td width="150" align="center">당신의 연령은</td>
					<td width="350" align="center"><input type="radio" name="age"
						value="10">10대&nbsp; <input type="radio" name="age"
						value="20">20대&nbsp; <input type="radio" name="age"
						value="30">30대&nbsp; <input type="radio" name="age"
						value="40">40대&nbsp;</td>
				</tr>

				<tr height="50">
					<td width="150" align="center">하고 싶은말</td>
					<td width="350" align="center"><textarea rows="5" cols="40"
							name="info"></textarea></td>
				</tr>

				<tr height="50">
					<td width="150" colspan="2"><input type="submit" value="회원 가입">
						<input type="reset" value="취소"></td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>

 

MemberJoinProc.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>

<%
request.setCharacterEncoding("UTF-8");

//취미부분은 별도로 읽어드려 다시 빈클래스에 저장 
String [] hobby = request.getParameterValues("hobby");
//배열에 있는 내용을 하나의 스트링으로 저장 
String texthobby="";

for(int i =0;i<hobby.length;i++){
	
	texthobby += hobby[i]+" ";
	
}
%>

<!-- useBean을 이용하여 한꺼번에 데이터를 받아오도록 한다. -->

<jsp:useBean id="mbean" class="model.MemberBean">
<jsp:setProperty name="mbean" property="*"/>
<!-- 맵핑시키시오  -->
</jsp:useBean>

<%

mbean.setHobby(texthobby);//기존 취미는 주소번지가 저장되기에 위에 배열의 내용을 하나의 스트링으로 저장한 변수를 다시입력 

%>

<h2>당신의 아이디=<%=mbean.getId() %></h2>
<h2>당신의 취미는 =<%=mbean.getHobby()%></h2>


</body>
</html>