[목차]
01. 회원가입 화면
💡
1. 회원가입 폼 만들기 (id, pw, level, name, email)
2. 회원가입 처리화면으로 경로 설정
[코드예제] user_insert_form.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html> <form action="<%= request.getContextPath() %>/uinsert/user_insert_action.jsp" method="post"> <table border="1"> <tr> <td>아이디</tb> <td><input type="text" name="u_id"></td> </tr> <tr> <td>비밀번호</tb> <td><input type="text" name="u_pw"></td> </tr> <tr> <td>권한</tb> <td><input type="text" name="u_level"></td> </tr> <tr> <td>이름</tb> <td><input type="text" name="u_name"></td> </tr> <tr> <td>이메일</tb> <td><input type="text" name="u_email"></td> </tr> <tr> <td colspan="2"><input type="submit" value="회원가입"></td> </tr> </table> </form>
02. 회원가입 처리
💡
1. JDBC 프로그램 실행 7단계
2. DB연결 시 필요한 정보
DB 종류(mysql), ip(localhost), 포트번호(3306), DB명(dbhooni), ID(idhooni), PW(pwhooni)
3. mysql jar파일 가져오기
[코드예제] user_insert_action.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.SQLException" %> <% // post 방식으로 데이터 받아오기 request.setCharacterEncoding("euc-kr"); String id = request.getParameter("u_id"); String pw = request.getParameter("u_pw"); String level = request.getParameter("u_level"); String name = request.getParameter("u_name"); String email = request.getParameter("u_email"); // 1단계 : 드라이버 실행 Class.forName("com.mysql.jdbc.Driver"); Connection conn = null; PreparedStatement pstmt = null; try { // 2단계 : DB 연결 String jdbcDriver = "jdbc:mysql://localhost:3306/dbhooni?" + "useUnicode=true&characterEncoding=euckr"; String dbUser = "idhooni"; String dbPass = "pwhooni"; conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass); System.out.println(conn + "<- conn"); // 3단계 : 쿼리 실행을 위한 객체 생성 pstmt = conn.prepareStatement("INSERT INTO tb_user VALUES(?, ?, ?, ?, ?)"); System.out.println(pstmt + "<- pstmt 1"); pstmt.setString(1, id); pstmt.setString(2, pw); pstmt.setString(3, level); pstmt.setString(4, name); pstmt.setString(5, email); System.out.println(pstmt + "<- pstmt 2"); // 4단계 : 쿼리 실행 int result = pstmt.executeUpdate(); System.out.println(result + "<-result"); // 5단계 : 쿼리 실행결과 사용(insert 생략 가능) }finally{ // 6단계 :statement 또는 prepareStatement객체 종료(close()) if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {} // 7단계 :Connection 객체 종료(close()) if (conn != null) try { conn.close(); } catch(SQLException ex) {} } %>
03. 최종 화면
tag : #DB #insert #쿼리 #회원가입 #JDBC
Uploaded by N2T