본문 바로가기

Java

#067 게시글 Listing / Select / Board 까지 전체 코드 정리 2차

Dao

더보기

ArticleDao

package com.sbs.example.easytextboard.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.sbs.example.easytextboard.dto.Article;
import com.sbs.example.easytextboard.dto.Board;

public class ArticleDao {

	private List<Article> articles;
	private List<Board> boards;
	private int lastArticleId;
	private int lastBoardId;

	public ArticleDao() {
		articles = new ArrayList<>();
		boards = new ArrayList<>();
		lastArticleId = 0;
		lastBoardId = 0;

	}

	public List<Article> getArticles() {
		articles.clear();
		test();
		return articles;
	}

	public Article getArticleByIndex(int i) {
		return articles.get(i);
	}

	public Article getArticle(int inputedId) {
		int index = getIndexById(inputedId);
		if (index == -1) {
			return null;
		}
		return articles.get(index);
	}

	private int getIndexById(int inputedId) {
		for (int i = 0; i < articles.size(); i++) {
			if (articles.get(i).id == inputedId) {
				return i;
			}
		}
		return -1;
	}

	public void remove(int inputedId) {
		int index = getIndexById(inputedId);
		articles.remove(index);
	}

	public Article modify(int inputedId, String title, String body) {
		Article article = getArticle(inputedId);
		article.title = title;
		article.body = body;
		return article;

	}

	public int makeBoard(String boardName) {
		Board board = new Board();
		board.id = lastBoardId + 1;
		board.name = boardName;

		boards.add(board);
		lastBoardId = board.id;

		return board.id;
	}

	public Board getBoardById(int inputedId) {
		for (Board board : boards) {
			if (board.id == inputedId) {
				return board;
			}
		}
		return null;
	}

	public List<Board> getBoards() {
		return boards;
	}

	public void test() {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;

		String url = "jdbc:mysql://localhost:3306/a3";
		String user = "sbsst";
		String pw = "sbs123414";

		String sql = "SELECT * FROM article";

		try {
			Class.forName("com.mysql.cj.jdbc.Driver");

			con = DriverManager.getConnection(url, user, pw);

			stmt = con.createStatement();

			rs = stmt.executeQuery(sql);

			while (rs.next()) {

				int id = rs.getInt("id");
				String title = rs.getString("title");
				String body = rs.getString("body");
				int memberId = rs.getInt("memberId");
				int boardId = rs.getInt("boardId");

				Article article = new Article();
				article.id = id;
				article.title = title;
				article.body = body;
				article.memberId = memberId;
				article.boardId = boardId;

				articles.add(article);

			}
		}

		catch (SQLException e) {
			System.out.println("에러났어요." + e);
		} catch (ClassNotFoundException e1) {
			System.out.println("에러났어여!");
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public int add(String title, String body, int memberId, int boardId) {
		Connection con = null;
		PreparedStatement pstmt = null;

		String url = "jdbc:mysql://localhost:3306/a3";
		String user = "sbsst";
		String pw = "sbs123414";

		String sql = "insert into article (id, title, body, memberId, boardId) values(?,?,?,?,?)";

		try {
			Class.forName("com.mysql.cj.jdbc.Driver");

			con = DriverManager.getConnection(url, user, pw);

			pstmt = con.prepareStatement(sql);

			pstmt.setInt(1, lastArticleId + 1);
			pstmt.setString(2, title);
			pstmt.setString(3, body);
			pstmt.setInt(4, memberId);
			pstmt.setInt(5, boardId);

			int r = pstmt.executeUpdate();

			System.out.println("변경되면 1 안되면 0 : " + r);

		} catch (SQLException e) {
			System.out.println("에러났어요." + e);
		} catch (ClassNotFoundException e1) {
			System.out.println("에러났어여!");
		} finally {
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		lastArticleId++;
		return lastArticleId + 1;
	}

}

MemberDao

package com.sbs.example.easytextboard.dao;

import java.util.ArrayList;
import java.util.List;

import com.sbs.example.easytextboard.dto.Member;

public class MemberDao {
	private List<Member> members;
	private int lastMemberId;

	public MemberDao() {
		members = new ArrayList<>();
		lastMemberId = 0;

	}

	public int join(String loginId, String loginPw, String name) {
		Member member = new Member();
		member.memberId = lastMemberId + 1;
		member.loginId = loginId;
		member.loginPw = loginPw;
		member.name = name;

		members.add(member);
		lastMemberId = member.memberId;

		return member.memberId;
	}

	public Member getMemberByLoginId(String loginId) {
		for (Member member : members) {
			if (member.loginId.equals(loginId)) {
				return member;
			}
		}
		return null;
	}

	public Member getMemberById(int loginedMemberId) {
		for (Member member : members) {
			if (member.memberId == loginedMemberId) {
				return member;
			}
		}
		return null;
	}

}

 

 

Dto

더보기

ArticleDto

package com.sbs.example.easytextboard.dto;

public class Article {
	public int id;
	public int memberId;
	public String title;
	public String body;
	public int boardId;
}

MemberDto

package com.sbs.example.easytextboard.dto;

public class Member {
	public int memberId;
	public String loginId;
	public String loginPw;
	public String name;
}

Board

package com.sbs.example.easytextboard.dto;

public class Board {
	public int id;
	public String name;
}

 

 

 

*전체적인 일상은 노션을 통해 작성하고 있습니다.

링크 : www.notion.so/026-e329c0e1fc3c4a9fb978f8b040d44ee1