본문 바로가기
Spring/JSP

[JSP] JDBC 데이터 입력

by JJH0100 2022. 11. 22.
728x90
반응형

2022.11.22 - [JSP] - [JSP] JDBC 데이터노출

 

[JSP] JDBC 연결

필요한 사전 작업 테이블 생성 2022.11.22 - [Database] - [MySQL] 테이블생성, 사용해보기 [MySQL] 테이블생성, 사용해보기 2022.11.22 - [JSP] - [JSP] MySQL 설치 [JSP] MySQL 설치 https://www.mysql.com/ MySQL Over 2000 ISVs, OEM

jjh93.com

 

 

addForm.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>
<h2>회원의 정보 입력 폼</h2>
<form method="post" action="addMemberAll.jsp">
	<table>
		<tr>
			<td>이름</td>
			<td><input type="text" name="name" size="20"></td>
		</tr>
		<tr>
			<td>아이디</td>
			<td><input type="text" name="userid" size="20"></td>
		</tr>
		<tr>
			<td>비밀번호</td>
			<td><input type="password" name="pwd" size="20"></td>
		</tr>
		<tr>
			<td>이메일</td>
			<td><input type="text" name="email" size="20"></td>
		</tr>
		<tr>
			<td>전화번호</td>
			<td><input type="text" name="phone" size="13"></td>
		</tr>
		<tr>
			<td>등급</td>
			<td>
				<input type="radio" name="admin" value="1"> 관리자
				<input type="radio" name="admin" value="2"> 일반회원
			</td>
		</tr>
		<tr>
			<td><input type="submit" value="전송"></td>
			<td><input type="reset" value="취소"></td>
		</tr>
	</table>
</form>
</body>
</html>

 

addMemberAll.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<%! //선언부는 첫 반문자에 의해서 단 한번 수행됩니다.
	Connection conn = null;
	//컴파일
	Statement stmt = null;
	//런타임
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	//선언
	String url = "jdbc:mysql://localhost:3306/mydb";
	String id = "root";
	String pw = "1234";
	String sql =  "insert into member values(?,?,?,?,?,?)";
%>
<body>
<%
	request.setCharacterEncoding("UTF-8");
	
	String name = request.getParameter("name");
	String userid = request.getParameter("userid");
	String pwd = request.getParameter("pwd");
	String email = request.getParameter("email");
	String phone = request.getParameter("phone");
	String admin = request.getParameter("admin");
	
	try{
		//1. JDBC드라이버 로드
		Class.forName("com.mysql.jdbc.Driver");
		
		//2.데이터 베이스 연결 객체인 Connection 생성
		conn = DriverManager.getConnection(url, id, pw);
		
		//3. PreparedStatment 객체 생성
		pstmt = conn.prepareStatement(sql);
		
		//4. 바인딩 변수 대입
		pstmt.setString(1, name);
		pstmt.setString(2, userid);
		pstmt.setString(3, pwd);
		pstmt.setString(4, email);
		pstmt.setString(5, phone);
		pstmt.setString(6, admin);
		
		pstmt.executeUpdate();
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		try{
			if(pstmt != null){
				pstmt.close();
			}
			if(conn != null){
				conn.close();
			}			
		}catch(Exception e){
			
		}
	}

%>
<h3>회원 가입 성공</h3>
<a href="MemberTest.jsp">회원 전체 목록 보기</a>

</body>
</html>

 

a링크를 클릭해서 데이터노출 테이블로 이동하면 홍길동이 추가 된 것이 보인다.

728x90
반응형

'Spring > JSP' 카테고리의 다른 글

[JSP] 표현 언어(EL : Expression Language)  (0) 2022.11.23
[JSP] Apache Commons Library  (2) 2022.11.23
[JSP] JDBC 데이터노출  (0) 2022.11.22
[JSP] mysql-connector-j-8.0.31 설치  (0) 2022.11.22
[JSP] 자바빈즈 (Java Beans)  (0) 2022.11.22

댓글