project 환경

java : 1.8

tomcat : 9.0

eclipse : neon

mysql : 5.5


board 테이블 구조


프로젝트 구조 (이클립스 다이나믹 웹 프로젝트)

※ WEB-INF/lib폴더에 있는  mysql -connector-java-5.1.40-bin.jar 파일은 http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.40http://dev.mysql.com/downloads/connector/j 둘중 하나에서 다운로드 받으면 됩니다. 


boardAddForm.jsp : 게시글을 입력하는 폼을 가진 페이지

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>BOARD ADD</title>
</head>
<body>
<h1>BOARD ADD</h1>
<form action="<%=request.getContextPath()%>/board/boardAddAction.jsp" method="post">
    <div>boardPw : </div>
    <div><input name="boardPw" id="boardPw" type="password"/></div>
    <div>boardTitle : </div>
    <div><input name="boardTitle" id="boardTitle" type="text"/></div>
    <div>boardContent : </div>
    <div><textarea name="boardContent" id="boardContent" rows="5" cols="50"></textarea></div>
    <div>boardName : </div>
    <div><input name="boardUser" id="boardUser" type="text"/></div>
    <div>
        <input type="submit" value="글입력"/>
        <input type="reset" value="초기화"/>
    </div>
</form>
</body>
</html>
cs


boardAddAction.jsp : 입력한 내용을 테이블에 입력하는 페이지

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title></title>
</head>
<body>
<%
request.setCharacterEncoding("euc-kr");
String boardPw = request.getParameter("boardPw");
System.out.println("param boardPw:"+boardPw);
String boardTitle = request.getParameter("boardTitle");
System.out.println("param boardTitle:"+boardTitle);
String boardContent = request.getParameter("boardContent");
System.out.println("param boardContent:"+boardContent);
String boardUser = request.getParameter("boardUser");
System.out.println("param boardUser:"+boardUser);
 
String dbUrl = "jdbc:mysql://127.0.0.1:3306/injava?useUnicode=true&characterEncoding=euckr";
String dbUser = "root";
String dbPw = "java0000";
Connection connection = null;
PreparedStatement statement = null;
try {
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(dbUrl, dbUser, dbPw);
    String sql = "INSERT INTO board(board_pw, board_title, board_content, board_user, board_date) values(?,?,?,?,now())";
    statement = connection.prepareStatement(sql);
    statement.setString(1,boardPw);
    statement.setString(2,boardTitle);
    statement.setString(3,boardContent);
    statement.setString(4,boardUser);
    statement.executeUpdate();
//  response.sendRedirect(request.getContextPath()+"/board/boardList.jsp");
catch(Exception e) {
    e.printStackTrace();
    out.print("입력 예외 발생");
finally {
    try {statement.close();} catch(Exception e){}
    try {connection.close();} catch(Exception e){}
}
%>
</body>
</html>
cs







+ Recent posts