JSP / JSTL - 게시판 로그인 / 로그아웃

Jiyeong·2021년 5월 13일
0

JSP

목록 보기
9/13

MyUtils : openJSP, getParamInt, parseStringToInt, getLoginUserPk, getLoginUser

public class MyUtils {
	
	public static UserVO getLoginUser(HttpServletRequest req) {
		if (req == null) {return null;}
		HttpSession hs = req.getSession();
		return (UserVO) hs.getAttribute("loginUser");
	} //

	public static int getLoginUserPK(HttpServletRequest req) {
		return getLoginUser(req).getIuser();
	} // 삭제, 글 쓰기, 

	public static int parseStringToInt(String val) {
		try {
			int result = Integer.parseInt(val);
			return result;
		} catch (Exception e) {
			return 0;
		} 
	} //String을 Int로 형변화 

	public static int getParamInt(String key, HttpServletRequest req) {
		// String key는 매개변수
		String val = req.getParameter(key);
		int intVal = MyUtils.parseStringToInt(val);
		return intVal;

	}

	public static void openJSP(String fileNM, HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		String jsp = "/WEB-INF/view/" + fileNM + ".jsp";
		// 단계로 들어갈 땐 앞의 / 빼면 에러남!
		req.getRequestDispatcher(jsp).forward(req, res);
	}
}

DBUtils:


public class DBUtils {
	public static Connection getCon() throws Exception {
		final String DB_NAME = "boardver4";
		final String DRIVER = "com.mysql.cj.jdbc.Driver";
		final String URL = "jdbc:mysql://localhost:3308/" + DB_NAME;
		final String USER_NAME = "root";
		final String PASSWORD = "koreait";
		
		Class.forName(DRIVER);
		Connection con = DriverManager.getConnection(URL,USER_NAME,PASSWORD);
		System.out.println("연결 성공");
		
		return con;
	}
	
	public static void close(Connection con, PreparedStatement ps) {
		close(con, ps, null);
	}
	
//								다리 역할				
//if문 순서 중요
	public static void close(Connection con, PreparedStatement ps, ResultSet rs) {
		if(rs != null) try {rs.close();} catch (SQLException e) {e.printStackTrace();}
		if(ps != null) try {ps.close();} catch (SQLException e) {e.printStackTrace();}
		if(con != null) try {con.close();} catch (SQLException e) {e.printStackTrace();}
	}
}

board : BoardDAO, BoardVO, Del, Detail, List, Mod, Write

BoardDAO:

public class BoardDAO {
public static int insBoard(BoardVO param) {

	Connection con = null;
	PreparedStatement ps = null;

	String sql = " INSERT INTO t_board(title,ctnt,iuser) VALUES(?,?,?) ";

	try {
		con = DBUtils.getCon();
		ps = con.prepareStatement(sql);

		ps.setString(1, param.getTitle());
		ps.setString(2, param.getCtnt());
		ps.setInt(3, param.getIuser());

		return ps.executeUpdate();

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		DBUtils.close(con, ps);
	}

	return 0;
}

public static List<BoardVO> selBoardList() {

	List<BoardVO> list = new ArrayList();

	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	String sql = " SELECT A.iboard, A.title, A.iuser, A.regdt, B.unm " + " FROM t_board A " + " LEFT JOIN t_user B "
			+ " ON A.iuser = B.iuser " + " ORDER BY A.iboard DESC ";
	try {
		con = DBUtils.getCon();
		ps = con.prepareStatement(sql);
		rs = ps.executeQuery(); // return type이 result set임

		while (rs.next()) {
			BoardVO vo = new BoardVO();
			vo.setTitle(rs.getString("title"));
			vo.setIboard(rs.getInt("iboard"));
			vo.setRegdt(rs.getString("regdt"));
			vo.setUnm(rs.getString("unm"));

			list.add(vo);
		}
		return list;
	} catch (Exception e) {
		e.printStackTrace();

	} finally {
		DBUtils.close(con, ps, rs);
	}

	return null;
}

public static BoardVO selBoard(int iboard) {

	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	BoardVO vo = null;

	String sql = " SELECT A.title, A.iuser, A.regdt, A.ctnt, B.unm " + " FROM t_board A " + " LEFT JOIN t_user B "
			+ " ON A.iuser = B.iuser " + " WHERE iboard = ? ";

	try {
		con = DBUtils.getCon();
		ps = con.prepareStatement(sql);
		ps.setInt(1, iboard);
		rs = ps.executeQuery();

		if (rs.next()) {

			String title = rs.getString("title");
			String ctnt = rs.getString("ctnt");
			String regdt = rs.getString("regdt");
			int iuser = rs.getInt("iuser");
			String unm = rs.getString("unm");
			// () 안에 컬럼명 집어넣기

			vo = new BoardVO();
			vo.setIboard(iboard);
			vo.setTitle(title);
			vo.setCtnt(ctnt);
			vo.setRegdt(regdt);
			vo.setUnm(unm);
			vo.setIuser(iuser);

			return vo;
		}

	} catch (Exception e) {
		e.printStackTrace();
		return vo;
	} finally {
		DBUtils.close(con, ps, rs);
	}

	return null;
}

public static int upBoard(BoardVO vo) {

	Connection con = null;
	PreparedStatement ps = null;

	String sql = " UPDATE t_board SET title =?, ctnt = ? WHERE iboard=? ";

	try {
		con = DBUtils.getCon();
		ps = con.prepareStatement(sql);

		ps.setString(1, vo.getTitle());
		ps.setString(2, vo.getCtnt());
		ps.setInt(3, vo.getIboard());

		return ps.executeUpdate();

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		DBUtils.close(con, ps, null);
	}
	return 0;
}

public static int delBoard(BoardVO vo) {

	Connection con = null;
	PreparedStatement ps = null;

	String sql = " DELETE FROM t_board WHERE iboard = ? AND iuser = ? ";

	try {
		con = DBUtils.getCon();
		ps = con.prepareStatement(sql);

		ps.setInt(1, vo.getIboard());
		ps.setInt(2, vo.getIuser());

		return ps.executeUpdate();

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		DBUtils.close(con, ps);
	}

	return 0;
}

}

User : UserDAO, UserVO, Join, Login, Logout
UserDAO :

public class UserDAO {
public static int joinUser(UserVO param) {
Connection con = null;
PreparedStatement ps = null;

	String sql = " INSERT INTO t_user (uid , upw, unm, gender) VALUES(?,?,?,?) ";

	try {
		con = DBUtils.getCon();
		ps = con.prepareStatement(sql);

		ps.setString(1, param.getUid());
		ps.setString(2, param.getUpw());
		ps.setString(3, param.getUnm());
		ps.setInt(4, param.getGender());

		return ps.executeUpdate();

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		DBUtils.close(con, ps);
	}

	return 0;
}

// 로그인 성공 : 1, 아이디없음: 2, 비밀번호 틀림 : 3, 에러 : 0
public static int loginUser(UserVO param) {

	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	String sql = " SELECT * FROM t_user WHERE uid=? ";

	try {
		con = DBUtils.getCon();
		ps = con.prepareStatement(sql);
		ps.setString(1, param.getUid());
		rs = ps.executeQuery();

		// reference 변수에 대한 이해 필요

		
		if (rs.next()) {
			// 아이디가 있는 경우 & 비밀번호 체크

			String dbPw = rs.getString("upw");
			if (BCrypt.checkpw(param.getUpw(), dbPw)) {
				//dbPw.equals(param.getUpw())
				int iuser = rs.getInt("iuser");
				String unm = rs.getString("unm");

				param.setUnm(unm);
				param.setIuser(iuser);
				
				return 1;
			} else {
				return 3;
			}

		} else {
			// 아이디가 없는 경우
			return 2;
		}

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		DBUtils.close(con, ps);
	}
	return 0;
}

}

// 1-로그인성공, 2-아이디불일치, 3-비밀번호불일치, 0-에러
public static int loginUser(UserVO vo) {

	Connection con = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	String jsp = " SELECT*FROM t_user WHERE uid=? ";

	try {
		con = DBUtils.getCon();
		ps = con.prepareStatement(jsp);

		ps.setString(1, vo.getUid());
		rs = ps.executeQuery();

		if (rs.next()) {
			String dbPW = rs.getString("upw");
			if (BCrypt.checkpw(vo.getUpw(), dbPW)) {
				int iuser = rs.getInt("iuser");
				String unm = rs.getString("unm");

				vo.setUnm(unm);
				vo.setIuser(iuser);

				return 1;
			
			}else {
				return 3;
			}
				
		} else	{return 2;}
	} catch (Exception e) {
		e.printStackTrace();
		return 0;
	} finally {
		DBUtils.close(con, ps, rs);
	}
}

}

profile
깃스타가 되고 싶은 벨플루언서

0개의 댓글