MariaDB, DTO, DAO 생성

Wooney98·2022년 11월 17일
1

PlayData_BackEnd

목록 보기
7/33

CMD로 DB생성


BoardDTO

package org.comstudy.myapp.model;

public class BoardDTO {
	private int bno;
	private String title;
	private String content;
	private String author;
	private String write_day;
	
	public BoardDTO() {
		this(0,"","","","");
	}
	
	public BoardDTO(int bno, String title, String content, String author, String write_day) {
		super();
		this.bno = bno;
		this.title = title;
		this.content = content;
		this.author = author;
		this.write_day = write_day;
	}


	public int getBno() {
		return bno;
	}


	public void setBno(int bno) {
		this.bno = bno;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getContent() {
		return content;
	}


	public void setContent(String content) {
		this.content = content;
	}


	public String getAuthor() {
		return author;
	}


	public void setAuthor(String author) {
		this.author = author;
	}


	public String getWrite_day() {
		return write_day;
	}


	public void setWrite_day(String write_day) {
		this.write_day = write_day;
	}

	@Override
	public String toString() {
		return "BoardDTO [bno=" + bno + ", title=" + title + ", content=" + content + ", author=" + author
				+ ", write_day=" + write_day + "]";
	}
}

BoardDTO

package org.comstudy.myapp.model;

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

import org.comstudy.dbcp.JdbcUtil;

public class BoardDAO {
	private static String SELECT_ALL = "SELECT * FROM BOARDDB";
	private static String SELECT_ONE = "SELECT * FROM USERS WHERE no=?";
	private static String INSERT = "INSERT INTO USERS(bno,title,content,author,write_day) values(?,?,?,?,?)";
	private static String UPDATE = "UPDATE USERS SET bno=?,title=?,content=?,author=?,write_day=? WHERE BNO=?";
	private static String DELETE = "DELETE FROM USERS WHERE BNO=?";
	
	private Connection conn;
	private PreparedStatement stmt;
	private ResultSet rs;
	
	private void commitCheck(int cnt) {
		try {
			if(cnt > 0) {
				conn.close();
			}else {
				conn.rollback();
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public List<BoardDTO> selectAll() {
		List<BoardDTO> list = new ArrayList<BoardDTO>();
		try {
	        conn = JdbcUtil.getConnection();
	        stmt = conn.prepareStatement(SELECT_ALL);
	        rs = stmt.executeQuery();
	        while (rs.next()) {
	            int bno = rs.getInt("bno");
	            String title = rs.getString("title");
	            String content = rs.getString("content");
	            String author = rs.getString("author");
	            String write_day = rs.getString("write_day");
	
	            //System.out.printf("%d, %s, %s, %s, %s\n", bno, title, content, author, write_day);
	            list.add(new BoardDTO(bno, title, content, author, write_day));
	         }
	      } catch (SQLException e) {
	         e.printStackTrace();
	      } finally {
	         JdbcUtil.close(conn, stmt, rs);
	      }
	      return list;
	}
	
	public void insert(BoardDTO bdto) {
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement(INSERT);
			stmt.setInt(1, bdto.getBno());
			stmt.setString(2, bdto.getTitle());
			stmt.setString(3, bdto.getContent());
			stmt.setString(4, bdto.getAuthor());
			stmt.setString(5, bdto.getWrite_day());
			commitCheck(stmt.executeUpdate() );
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.close(conn,stmt,rs);
		}
		
	}
	public void update(BoardDTO bdto) {
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement(UPDATE);
			stmt.setInt(1, bdto.getBno());
			stmt.setString(2, bdto.getTitle());
			stmt.setString(3, bdto.getContent());
			stmt.setString(4, bdto.getAuthor());
			stmt.setString(5, bdto.getWrite_day());
			commitCheck(stmt.executeUpdate() );
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.close(conn,stmt,rs);
		}
		
	}
	public void delet(BoardDTO bdto) {
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement(DELETE);
			stmt.setInt(1, bdto.getBno());
			commitCheck(stmt.executeUpdate() );
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.close(conn,stmt,rs);
		}
		
	}
	public BoardDTO selectOne(BoardDTO bdto) {
		BoardDTO board = null;
		
		try {
			conn = JdbcUtil.getConnection();
			stmt = conn.prepareStatement(SELECT_ONE);
			stmt.setInt(1, bdto.getBno());
			rs = stmt.executeQuery();
			if(rs.next()) {
				int bno = rs.getInt(1);
	            String title = rs.getString(2);
	            String content = rs.getString(3);
	            String author = rs.getString(4);
	            String write_day = rs.getString(5);
	            board = new BoardDTO(bno,title,content,author,write_day);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JdbcUtil.close(conn,stmt,rs);
		}
		return board;
		
	}
	
	public static void main(String[] args) {
	      BoardDAO Bdao = new BoardDAO();
	      List<BoardDTO> list = Bdao.selectAll();

	      for (BoardDTO board : list) {
	         System.out.println(board);
	      }
	   }
}

profile
👨Education Computer Engineering 🎓Expected Graduation: February 2023 📞Contact info thstjddn77@gmail.com

0개의 댓글