wasup

게시판 만들기#ver1_write 본문

IT/Java

게시판 만들기#ver1_write

wasupup 2021. 5. 27. 11:56
반응형

대략의 설계 표.

데이터베이스명 board1
테이블명 게시물
필드명 번호 제목 내용 작성일 조회수
타입 int varchar(50) varchar(200) timestamp int
비고 PRIMARY KEY AUTO_INCREMENT NOT NULL NULL DEFAULT CURRENT_TIMESTAMP DEFAULT 0
의미 게시물번호.
자동증가.
게시물 제목.
NULL불가
게시물 내용.
NULL이가능함.
작성날짜.
기본값은 현재날짜
게시물 조회수.
기본값은 0

실행할 SQL

DROP DATABASE IF EXISTS boardtest;

CREATE DATABASE boardtest;

USE boardtest;

CREATE TABLE board(
	boNum int PRIMARY KEY AUTO_INCREMENT,
	boTit varchar(50) NOT NULL,
	boCon varchar(200) NULL,
	boDate timestamp DEFAULT CURRENT_TIMESTAMP,
	boHit int DEFAULT 0
);

BoardManage.java 

package boardpackage;

import java.sql.Date;
//값만 가진 객체 : VO(Value Object)
public class boardtable {

	private int boNum;
	private String boTit;
	private String boCon;
	private Date boDate;
	private int boHit;
	
	public int getBoNum() {
		return boNum;
	}
	public void setBoNum(int boNum) {
		this.boNum = boNum;
	}
	public String getBoTit() {
		return boTit;
	}
	public void setBoTit(String boTit) {
		this.boTit = boTit;
	}
	public String getBoCon() {
		return boCon;
	}
	public void setBoCon(String boCon) {
		this.boCon = boCon;
	}
	public Date getBoDate() {
		return boDate;
	}
	public void setBoDate(Date boDate) {
		this.boDate = boDate;
	}
	public int getBoHit() {
		return boHit;
	}
	public void setBoHit(int boHit) {
		this.boHit = boHit;
	}
	
	
}

 


게시물등록

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
*{margin:0; padding:0;}
a{text-decoration: none; color: #000;}
</style>
</head>
<body>
<h3>메뉴</h3>
<ul>
	<li>
		<a href="./board.jsp" target="_blank">게시물등록</a>
	</li>
</ul>
</body>
</html>

제목, 내용외의 다른 필드의 값은 기본값이 있거나 자동증가이므로

html구조는 아래처럼 쓸 수 있다.

board_write.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
* {	margin: 0;	padding: 0;}
a {	text-decoration: none;	color: #000;}
h3{ text-align: center; line-height: 50px;}
form{ width: 500px; margin: 20px auto; border: 1px solid #ddd; padding: 20px;}
p{ width: 100%;}
p label{ display: block; width: 100%; line-height: 30px; height: 30px; font-size: 15px;}
p input{ display: block; width: 100%; line-height: 30px; height: 30px; font-size: 15px;}
p textarea{ display: block; width: 100%; line-height: 30px; height: auto; font-size: 15px;}
</style>
</head>
<body>
	<h3>게시물등록</h3>
	<form action="board_update.jsp" method="post">
		<p>
			<label for="boTit">제목 </label>
			<input type="text" name="boTit" id="boTit" maxlength="50"/>
		</p>
		<p>
			<label for="boCon">내용 </label>
			<textarea cols="30" rows="10" name="boCon" maxlength="200"></textarea>
		</p>
		<p>
			<input type="submit" value="등록" />
		</p>
	</form>
</body>
</html>

board_update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="boardpackage.boardtable" %>

<%
request.setCharacterEncoding("utf-8");
String boTit = request.getParameter("boTit");
String boCon = request.getParameter("boCon");
boardtable newBoardTable = new boardtable();
newBoardTable.setBoTit(boTit);
newBoardTable.setBoCon(boCon);

try {
	// DB연결관리객체 : Connection, 명령전달객체 : Statement, 질의결과관리객체 : ResultSet
	//드라이버 로드
    Class.forName("com.mysql.cj.jdbc.Driver");

	//드라이버 관리자를 통해 DBConManagement 얻음
	Connection DBConManagement = DriverManager
	.getConnection("jdbc:mysql://localhost:3306/boardtest?useUnicode=true&characterEncoding=utf8", "root", "1234");

	//DBConManagement를 통해 CommandState 얻음
	Statement CommandState = DBConManagement.createStatement();

	//명령
	String NewSQL = "INSERT INTO boardtable(boTit, boCon) VALUES('" + newBoardTable.getBoTit() + "', '" + newBoardTable.getBoCon() + "')";
	CommandState.executeUpdate(NewSQL);
    
	//연결끊음
	DBConManagement.close();
} catch (Exception e) {
	e.printStackTrace();
}

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= newBoardTable.getBoTit() %> 게시물 등록이 완료되었습니다.
</body>
</html>

 

 

 


 

반응형

'IT > Java' 카테고리의 다른 글

게시판 만들기#ver1_basedata  (0) 2021.05.29
게시판 만들기#ver1_list and view  (0) 2021.05.28
Java) Scanner test  (0) 2021.05.15
Java) Array 배열이 뭐냐  (0) 2021.05.13
Java) 데이터 타입  (0) 2021.05.12
Comments