<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page session="true" %>

<% request.setCharacterEncoding("EUC-KR"); %>
<%
	String id = request.getParameter("ID");
	String pw1 = request.getParameter("PW1");
	String pw2 = request.getParameter("PW2");
	String name = request.getParameter("Name");
	String zipcode1 = request.getParameter("ZipCode1");
	String zipcode2 = request.getParameter("ZipCode2");
	String zip1 = request.getParameter("Zip1");
	String zip2 = request.getParameter("Zip2");
	String email1 = request.getParameter("Email1");
	String email2 = request.getParameter("Email2");
	String job = request.getParameter("Job");
	
	String birth = request.getParameter("Birth");
	String zipcode = zipcode1 +"-"+zipcode2;
	String zip = zip1 + " " + zip2;
	String email = email1 + "@" + email2;
	
	try{
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@168.126.146.45:1521:orcl","20162515","970623");
		PreparedStatement pstmt = conn.prepareStatement("select id from member_1 where id=?");
		pstmt.setString(1,id);
		ResultSet rs = pstmt.executeQuery();
		
		if(rs.next()){
			String abc = (String)rs.getString("id");
			if(id.equals(abc)){
				%> <script>alert('이미 존재하는 아이디입니다.'); location.href="http://localhost:8080/Park20162515/Join.jsp"</script><%
			}
		}else if(pw1.equals("")){
			%> <script>alert('비밀번호를 입력해주세요.'); location.href="http://localhost:8080/Park20162515/Join.jsp"</script><%
		}else if(!pw1.equals(pw2)){
			%> <script>alert('비밀번호 두개가 일치하지 않습니다.'); location.href="http://localhost:8080/Park20162515/Join.jsp"</script><%
		}else{
		pstmt = conn.prepareStatement("insert into member_1(id,pw,name,birthday,zipcode,zip,email,job) values(?,?,?,?,?,?,?,?)");
		
		pstmt.setString(1,id);
		pstmt.setString(2,pw1);
		pstmt.setString(3,name);
		pstmt.setString(4,birth);
		pstmt.setString(5,zipcode);
		pstmt.setString(6,zip);
		pstmt.setString(7,email);
		pstmt.setString(8,job);
		
		
		pstmt.executeUpdate();
		pstmt.close();
		conn.close();
		
		session.setAttribute("login","1");
		session.setAttribute("id",id);
		session.setAttribute("pw",pw1);
		pstmt.close();
		conn.close();
		%> <script>alert('회원가입이 완료 되었습니다.'); location.href="http://localhost:8080/Park20162515/Main.jsp"</script><%
		}
	}catch(Exception e){
		session.setAttribute("login","0");
		session.setAttribute("id",null);
		session.setAttribute("pw",null);
		%> <script>alert('회원가입에 실패 하였습니다.\n다시 입력해 주시기 바랍니다.'); location.href="http://localhost:8080/Park20162515/Join.jsp"</script><%
	}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>

</body>
</html>