카테고리 없음

JSP 로그인(Oracle Database 연동)

나는흰머리오목눈이 2020. 6. 23. 09:27

 

 

안녕하세요, 저번 글에 입사지원서 화면을 올렸는데요.

모든 프로그램에서나 웹에서나 데이터를 관리할 때 빠지면 안되는 것이 바로

로그인입니다.

 

로그인을 해야 처음 

그리고 글을 작성했을 경우, 수정 또는 삭제를 할 수 있게끔 할 수 있게 해줘야 하구요.

 

그래서 오늘은 로그인 화면과 오라클 데이터베이스 연동을 어떻게 하는지 보여드리겠습니다.

 


로그인 화면

원래의 로그인이라면 아이디와 비밀번호가 있는데

입사지원이므로 굳이 아이디를 만들 필요가 없다고 생각했습니다.

 

본인인지 확인만 한다면 값을 불러드릴 수 있으므로

이름을 가지고 왔고, 동명이인이 있을 수 있으므로 휴대전화까지 넣어줬습니다.

비밀번호로 일치하는지 확인을 해주고 맞으면 수정, 삭제를 할 수 있는 페이지로 넘어가며

이름과 전화번호가 일치하지 않는다면 데이터가 없으므로

새로 작성할 수 있는 페이지로 넘어갑니다.

 

이제 소스를 한 번 보실까요?

 


소스

일단 로그인 화면에서는 소스가 딱히 없고 화면 구현만 있으며 <form> 태그를 이용해

로그인해주는 페이지로 넘겨줍니다.

 

그나마 기능은 휴대전화 입력칸에 숫자만 가능하게 하는 것입니다.

 

$("input:text[numberOnly]").on("keyup", function() {
     $(this).val($(this).val().replace(/[^0-9]/g,""));
}); /* 숫자만 입력 */

 

 

이제 데이터베이스 연동을 해주는 소스를 확인해보실까요?

 

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

        String name=request.getParameter("name");  //name 값 받아오기
        String hdpno1=request.getParameter("hdpno1");  //hdpno1값 받아오기
        String hdpno2=request.getParameter("hdpno2");  //hdpno2값 받아오기
        String hdpno3=request.getParameter("hdpno3");  //hdpno3값 받아오기
        String hdpno=hdpno1+hdpno2+hdpno3;
        String password=request.getParameter("password");  //password값 받아오기

 

        //세션 저장하기
        session.setAttribute("username", name);
        session.setAttribute("userhdpno", hdpno);
        session.setAttribute("userpwd", password);
%>

 

<%
        Connection conn = null;
        PreparedStatement pstmt = null;
        PreparedStatement pstmt1 = null;
        ResultSet rs = null;
        ResultSet rs1 = null;

        String pwd_sql = "select * from 테이블이름 where name=? and hdpno=?";

        String user_sql = "select * from 테이블이름 where name=? and hdpno=? and password=?";

        try {
                String jdbcUrl="jdbc:oracle:thin:@서버주소:테이블이름포트:SID";
                String dbid="사용자아이디";
                String dbpass="사용자비밀번호"; // db연동

                conn=DriverManager.getConnection(jdbcUrl,dbid,dbpass);

                pstmt=conn.prepareStatement(user_sql);
                pstmt.setString(1, name); //값 대입
                pstmt.setString(2, hdpno);
                pstmt.setString(3, password);

                pstmt1=conn.prepareStatement(pwd_sql);
                pstmt1.setString(1, name);
                pstmt1.setString(2, hdpno);

                rs = pstmt.executeQuery();
                rs1 = pstmt1.executeQuery();

 

                if (rs.next()) { //이름, 휴대전화, 비밀번호가 일치했을 경우

                        response.sendRedirect("페이지 주소"); 

                }
                else {
                        if (rs1.next()) { //이름, 휴대전화는 일치했지만 비밀번호가 일치하지 않았을 경우
                               out.println("<script>alert('비밀번호를 잘못 입력하였습니다.');</script>");

                               out.println("<script>location.href='로그인 페이지 주소';</script>");

                       }
                        else { //데이터 정보가 일치하는 게 없을 경우
                                response.sendRedirect("페이지 주소");
                        }
                }
        }
        catch (Exception ex) {  //에러 검출
                ex.printStackTrace();
        }
        finally {
                if (pstmt != null)
                        try {pstmt.close();} catch (SQLException ex) {}
                if (pstmt1 != null)
                        try {pstmt1.close();} catch (SQLException ex) {}
                if (conn != null)
                        try {conn.close();} catch (SQLException ex) {}
        }
%>

 

이해가 되셨나요?

데이터베이스 연동을 한 번이라도 성공하셨으면 이제 어떤 데이터베이스 연동이든

참고를 할 수 있으니 한 번이라도 해보고 이해해보세요!

 

저는 그럼 다음 글에서 만나겠습니다.