1. DB연결: JDBC 방식
2. 빈 객체 사용하지 않음
3. jstl 사용하지 않음
4. DB: oracle
5. 쿼리문
create table member2(
idx number not null primary key,
id varchar2(20),
pw varchar2(16),
name varchar2(20),
age varchar2(3),
gender varchar2(2));
create sequence seq;
6. 폴더 구조
src
com.utill
-DBConn.java
WebContent
-loginForm.jsp
-loginProcess.jsp
-registForm.jsp
-regist.jsp
-main.jsp
DBConn.java
데이터 베이스 연결
package com.utill;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConn {
public static Connection conn = null;//커넥션 객체 생성
//데이터베이스 연결 메서드
public static Connection getConnection(){
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String id="scott";
String pw="tiger";
String driver="oracle.jdbc.driver.OracleDriver";
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,id,pw);
} catch (Exception e) {
System.out.println("접속 실패: "+e.toString());
}
return conn;
}//연결 메서드 종료
//데이터베이스 연결 종료 메서드
public static void dbClose(){
if(conn==null){
return;
}
try {
if(conn!=null){
conn.close();
}
} catch (Exception e) {
System.out.println("데이터 비정상 종료"+e.toString());
}
conn=null;
}
}//ClassEND
loginForm.jsp
로그인 페이지
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="loginProcess.jsp" method="post">
<center>
<table border="1">
<tr>
<td>아이디</td>
<td><input name="id" type="text"/></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input name="pw" type="password"/></td>
</tr>
<tr>
<td><input type="submit" value="login"/></td>
<td><a href="registForm.jsp"><input name="regist" type="button" value="회원가입"/></a></td>
</tr>
</table>
</center>
</form>
</body>
</html>
loginProcess.jsp
로그인 과정을 담당
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="com.utill.DBConn"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("EUC-KR"); //한글깨짐
//로그인 페이지로 부터 입력값을 받아 온다.
String id = request.getParameter("id");
String pw = request.getParameter("pw");
Connection conn = DBConn.getConnection();//데이터베이스 연결
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "";
try{
sql="select * from member2 where id=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, id);
rs=pstmt.executeQuery();
if(rs.next()){
if(pw.equals(rs.getString("pw")) && id.equals(rs.getString("id"))){
session.setAttribute("id",id);//세션에 아이디 값을 저장
out.println("<script>");
out.println("location.href='main.jsp'");
out.println("</script>");
}
}
out.println("<script>");
out.println("location.href='loginForm.jsp'");
out.println("</script>");
DBConn.dbClose();//데이터 베이스 닫기
}catch(Exception e){
e.printStackTrace();
}
%>
registForm.jsp
회원가입 폼
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="regist.jsp" method="post">
<center>
<table border="1">
<tr>
<td>아이디:</td>
<td><input name="id" type="text"/></td>
</tr>
<tr>
<td>비밀번호:</td>
<td><input name="pw" type="text"/></td>
</tr>
<tr>
<td>이름:</td>
<td><input name="name" type="text"/></td>
</tr>
<tr>
<td>나이:</td>
<td><input name="age" type="text"/></td>
</tr>
<tr>
<td>성별:</td>
<td>
<input name="gender" type="radio" value="남" checked/>남자
<input name="gender" type="radio" value="여"/>여자
</td>
</tr>
<tr>
<td><input name="regist" type="submit" value="등록"/></td>
<td><input type="reset" value="다시입력"/></td>
</tr>
</table>
</center>
</form>
</body>
</html>
regist.jsp
회원 가입 과정을 담당
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="com.utill.DBConn"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("EUC-KR"); //한글깨짐
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String name = request.getParameter("name");
String age = request.getParameter("age");
String gender = request.getParameter("gender");
Connection conn = DBConn.getConnection();//데이터베이스 연결
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "";
try{
sql="insert into member2(idx,id,pw,name,age,gender) values(seq.nextval,?,?,?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pw);
pstmt.setString(3, name);
pstmt.setString(4, age);
pstmt.setString(5, gender);
int result = pstmt.executeUpdate();
DBConn.dbClose();
if(result!=0){
response.sendRedirect("loginForm.jsp");
}else{
response.sendRedirect("registForm.jsp");
}
}catch(Exception e){
e.printStackTrace();
}
%>
main.jsp
로그인 후 보여줄 페이지
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
//세션에 아이디 값이 있으면(로그인 성공) 해당값을(형 변환 후) 전달 해 준다.
String id = null;
if(session.getAttribute("id")!=null){//if문으로써 로그인 상태와 비로그인 상태를 구분 할 수 있다.
id=(String)session.getAttribute("id");
}else{
out.println("<script>");
out.println("location.href='loginForm.jsp'");
out.println("</script>");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>로그인이 된 메인 페이지</h3>
안녕하세요<%=id %>님, 여기는 일반 회원 메뉴 입니다.<br/>
<%
if(id.equals("admin")){
%>
<%} %>
</body>
</html>