JDBC를 이용한 회원가입 예제

이기현·2020년 1월 12일
0

2020Camp

목록 보기
7/16

먼저 user.java 파일을 만들어서 객체를 만들 준비를 한다.

package team3_jsp;

public class User {
        private String userID;
        private String userPassword;
        private String userName;
        private String userGender;
        
        public String getUserID() {
            return userID;
        }
        public void setUserID(String userID) {
            this.userID = userID;
        }
        public String getUserPassword() {
            return userPassword;
        }
        public void setUserPassword(String userPassword) {
            this.userPassword = userPassword;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getUserGender() {
            return userGender;
        }
        public void setUserGender(String userGender) {
            this.userGender = userGender; 
        }   
}

JDBC_join.jsp : 회원가입 페이지

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8 ">
<meta name="viewport" content="width=device-width", initial-scale="1">
<link rel="stylesheet" href="css/bootstrap.css">
<title>JSP 게시판</title>
</head>
<body>
<div class="container">
        <div class="jumbotron" style="padding-top: 20px;">
        <form method="post" action="JDBC_account.jsp">
        <h3 style="text-align:center;">회원가입 화면</h3>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="아이디" name="userID" maclength="20">
        </div>
        <div class="form-group">
            <input type="password" class="form-control" placeholder="비밀번호" name="userPassword" maclength="20">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="이름" name="userName" maclength="20">
        </div>
        <div class="form-group" style="text-align:center;">
            <div class="btn-group" data-toggle="buttons">
                    <label class="btn btn-primary active">
                        <input type="radio" name="userGender" autocomplete="off" value="남자" checked>남자
                    </label>
                    <label class="btn btn-primary">
                        <input type="radio" name="userGender" autocomplete="off" value="여자" checked>여자
                    </label>
            </div>    
        </div>
        <input type="submit" class="btn btn-primary form-control" value="회원가입">
        </form>
    </div>
  </div>


        
</body>
</html>

여기서 form을 통해서 action되는 jsp파일을 선택한다. 여기서는 JDBC_account.jsp파일!!

JDBC_account.jsp에서는 아까 만든 user.java 파일로부터 javabean객체를 만들어서 값을 저장하고, userDAO.java에서 나의 Database로 연결 후, 입력받은 값을 통해 table에 값을 저장하는 작업을 한다.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="team3_jsp.UserDAO" %> <!-- JAVA클래스 import-->
<%@ page import="java.io.PrintWriter"%>
<% request.setCharacterEncoding("UTF-8"); %> <!-- javabean 객체 만들기-->
<jsp:useBean id="user" class="team3_jsp.User" scope="page"/>
<jsp:setProperty name="user" property="userID"/>
<jsp:setProperty name="user" property="userPassword"/>
<jsp:setProperty name="user" property="userName"/>
<jsp:setProperty name="user" property="userGender"/>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8 ">
<title>JSP 게시판</title>
</head>
<body>
    <%
      UserDAO userDAO = new UserDAO();
      int result = userDAO.join(user);
      if(result == -1){
          PrintWriter script = response.getWriter();
          script.println("<script>");
          script.println("alert('이미 존재하는 아이디입니다.')");
          script.println("history.back()");
          script.println("</script>");
      }
      else{
          session.setAttribute("userID",user.getUserID());
          PrintWriter script = response.getWriter();
		  out.println("회원가입 성공 !!");
      }
        
 
 
    %>
</body>
</html>

userDAO.java

package team3_jsp;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 
public class UserDAO {
 
    private Connection conn;   //데이터베이스에 접근하기 위한 객체
    private PreparedStatement pstmt;  
    private ResultSet rs;   //정보를 담을 수 있는 변수를 생성
    
    public UserDAO() {
        try {
            //생성자
            String dbURL="jdbc:mysql://db4free.net:3306/mydata_21300506?useSSL=false&serverTimezone=UTC&characterEncoding=utf8";                             
            String dbID="emawlrdl";
            String dbPassword="toddlf930";
            Class.forName("com.mysql.cj.jdbc.Driver"); 
            conn=DriverManager.getConnection(dbURL,dbID,dbPassword);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    public int join(User user) {
        String SQL= "INSERT INTO user VALUES(?, ?, ?, ?) ";
        try {
            pstmt = conn.prepareStatement(SQL);
            pstmt.setString(1,user.getUserID());
            pstmt.setString(2,user.getUserPassword());
            pstmt.setString(3,user.getUserName());
            pstmt.setString(4,user.getUserGender());
            return pstmt.executeUpdate();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return -1;
    }
    
}
profile
실력을 쌓아가는 하루하루

0개의 댓글