[23.01.27] 66일차 [프론트엔드]

W·2023년 1월 27일
0

국비

목록 보기
97/119

DB에 회원(members)테이블 email address phone mobile 추가

mysql -uroot -p1234 jspdb1

alter table members
add email varchar(30);

alter table members
add address varchar(50);

alter table members
add phone varchar(30);

alter table members
add mobile varchar(30);

  • joinPro.jsp에 추가

  • MemberDTO.java에 추가

  • MemberDAO.java insert메서드에 추가

  • 가입하기

DB 확인 - 데이터 입력되어 있음

게시판 만들기

  • DB에 게시판(board) 테이블 만들기
    글번호, 글쓴이, 제목, 내용, 날짜, 조회수
create table board
(num int primary key,
 name varchar(20),
 subject varchar(50),
 content varchar(300),
 readcount int,
 date timestamp);
  • BoardDTO.java
package board;

import java.sql.Timestamp;

public class BoardDTO {
	
	private int num;
	private String name;
	private String subject;
	private String content;
	private int readcount;
	private Timestamp date;
	
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public int getReadcount() {
		return readcount;
	}
	public void setReadcount(int readcount) {
		this.readcount = readcount;
	}
	public Timestamp getDate() {
		return date;
	}
	public void setDate(Timestamp date) {
		this.date = date;
	}
}
  • BoardDAO.java
package board;

import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class BoardDAO {

	public Connection getConnection() throws Exception {
		Context init = new InitialContext();
		DataSource ds = (DataSource)init.lookup("java:comp/env/jdbc/MysqlDB");
		Connection con = ds.getConnection();
		return con;
	}
	
	public void insertBoard(BoardDTO dto) {
		System.out.println("BoardDAO insertMember");
		System.out.println("BoardDTO 바구니 전달받은 주소 : " + dto);
		System.out.println("바구니주소 전달받은 name : "+dto.getName() );
		System.out.println("바구니주소 전달받은 subject : "+dto.getSubject() );
		System.out.println("바구니주소 전달받은 content : "+dto.getContent() );
		System.out.println("바구니주소 전달받은 readcount : "+dto.getReadcount());
		System.out.println("바구니주소 전달받은 date : "+dto.getDate());
		Connection con = null;
		PreparedStatement pstmt = null;
		try {
			con = getConnection();
			int num=1;
			String sql = "insert into board(num, name, subject, content, readcount, date) values(?,?,?,?,?,?)";
			
			pstmt=con.prepareStatement(sql);
			
			pstmt.setInt(1, num);
			pstmt.setString(2, dto.getName());
			pstmt.setString(3, dto.getSubject());
			pstmt.setString(4, dto.getContent());
			pstmt.setInt(5, dto.getReadcount());
			pstmt.setTimestamp(6, dto.getDate());
			
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(con!=null)try {con.close();} catch (Exception e2) {};
			if(pstmt!=null)try {pstmt.close();} catch (Exception e2) {};
		}
	}
}
  • writefrom.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>board/writeForm.jsp</title>
</head>
<body>
<%

String id=(String)session.getAttribute("id");

if(id==null){
	response.sendRedirect("../member/login.jsp");
}


%>

<h1>글쓰기</h1>

<form action="writePro.jsp" method="post">
<table border="1">
<tr><td>글쓴이</td>
	<td><input type="text" name="name" value="<%=id%>" readonly></td></tr>
<tr><td>글제목</td>
	<td><input type="text" name="subject"></td></tr>
<tr><td>글내용</td>
	<td><textarea name="content" rows="10" cols="20"></textarea></td></tr>
</table>
<input type="submit" value="작성완료" class="submit">
</form>
</body>
</html>
  • writePro.jsp
<%@page import="board.BoardDAO"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="board.BoardDTO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
// board/writePro.jsp

// request 한글처리
// request name, subject, content 가져와서 변수에 저장
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String subject = request.getParameter("subject");
String content = request.getParameter("content");

int readcount = 0;
Timestamp date = new Timestamp(System.currentTimeMillis());

// 패키지 board 파일이름 BoardDTO

// 멤버변수 num, name, subject, content, readcount, date
// set get 만들기

// BoardDTO 객체 생성
// set 메서드 호출해서 값 저장
BoardDTO dto = new BoardDTO();
dto.setName(name);
dto.setSubject(subject);
dto.setContent(content);
dto.setReadcount(readcount);
dto.setDate(date);


// 패키지 board 파일이름 BoardDAO
// 리턴할형없음 insertBoard(boardDTO dto) 메서드 정의
// BoardDAO 객체생성
// inserBoard(dto) 메서드 호출

BoardDAO dao = new BoardDAO();
dao.insertBoard(dto);

// 글목록 list.jsp
response.sendRedirect("list.jsp");

%>

num 값 순차적으로 자동 입력되게 하기

  • insert 메서드에 쿼리 추가하기

0개의 댓글