반응형
JSPEx02.jdbc01 - Oracle 연동
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Java 전처리 -->
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
String sql = "select deptno, dname, loc from dept";
rs = stmt.executeQuery(sql);
while(rs.next()) {
sb.append(rs.getString("deptno") + " : " + rs.getString("dname") + "<br />");
}
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
%>
<!-- html 전처리 -->
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>Insert title here</title>
</head>
<body>
<%= sb %>
</body>
</html>
JSPEx02.zipcode_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Java 전처리 -->
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8"); // 한글 지원 기능
String dong = request.getParameter("dong"); // dong을 받겠다
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
String sql = "select zipcode, sido, gugun, dong, ri, bunji from zipcode where dong like '" +dong +"%'";
rs = stmt.executeQuery(sql);
sb.append("<table width='800' border='1'>");
while(rs.next()) {
sb.append("<tr>");
sb.append(" <td>").append(rs.getString("zipcode")).append("</td>");
sb.append(" <td>").append(rs.getString("sido")).append("</td>");
sb.append(" <td>").append(rs.getString("gugun")).append("</td>");
sb.append(" <td>").append(rs.getString("dong")).append("</td>");
sb.append(" <td>").append(rs.getString("ri") == null ? "" : rs.getString("ri")).append("</td>");
sb.append(" <td>").append(rs.getString("bunji") == null ? "" : rs.getString("bunji")).append("</td>");
sb.append("</tr>");
}
sb.append("</table>");
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>Insert title here</title>
</head>
<body>
<%= sb %>
</body>
</html>
JSPEx02.zipcode
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>Insert title here</title>
<script type="text/javascript">
function checkfrm() {
if(document.frm.dong.value.length <= 1) {
alert("동이름을 한 자 이상 입력하셔야 합니다.");
return;
}
document.frm.submit();
}
</script>
</head>
<body>
<form action="./zipcode_ok.jsp" method="post" name="frm">
동이름 <input type="text" name="dong" placeholder="동이름 입력" size="30" maxlength="5" />
<input type="button" value="검색" onclick="checkfrm()" />
</form>
</body>
</html>
JSPEx02.Zipcode2
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Java 전처리 -->
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8"); // 한글 지원 기능
String dong = request.getParameter("dong"); // dong을 받겠다
StringBuffer sb = new StringBuffer();
if(dong != null && !dong.equals("")) {
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "scott";
String password = "tiger";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
String sql = "select zipcode, sido, gugun, dong, ri, bunji from zipcode where dong like '" +dong +"%'";
rs = stmt.executeQuery(sql);
sb.append("<table width='800' border='1'>");
while(rs.next()) {
sb.append("<tr>");
sb.append(" <td>").append(rs.getString("zipcode")).append("</td>");
sb.append(" <td>").append(rs.getString("sido")).append("</td>");
sb.append(" <td>").append(rs.getString("gugun")).append("</td>");
sb.append(" <td>").append(rs.getString("dong")).append("</td>");
sb.append(" <td>").append(rs.getString("ri") == null ? "" : rs.getString("ri")).append("</td>");
sb.append(" <td>").append(rs.getString("bunji") == null ? "" : rs.getString("bunji")).append("</td>");
sb.append("</tr>");
}
sb.append("</table>");
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
}
%>
<!-- html 선언부 -->
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>Insert title here</title>
<script type="text/javascript">
function checkfrm() {
if(document.frm.dong.value.length <= 1) {
alert("동이름을 한 자 이상 입력하셔야 합니다.");
return;
}
document.frm.submit();
}
</script>
</head>
<body>
<form action="./zipcode2.jsp" method="post" name="frm">
동이름 <input type="text" name="dong" placeholder="동이름 입력" size="30" maxlength="5" />
<input type="button" value="검색" onclick="checkfrm()" />
</form>
<br /><br />
<%= sb %>
</body>
</html>
웹프로그래머(시작 -> 끝)
-> 게시판(crud)
목록 -> 쓰기
-> 자세히 보기
-> 수정
-> 삭제
* 디자인(웹 디자이너/퍼블리셔)
* ..
* 프로그램
페이지 네비게이션
: 데이터의 흐름도
목록 -> 쓰기
-> 자세히 보기
-> 수정
-> 삭제
파일명 가지고 ...
board_list1.jsp
board_write1.jsp
board_view1.jsp
board_modify1.jsp
board_delete1.jsp
디자인 페이지와 프로그램 페이지(안보이는 페이지)가 다르다
BoardEx01
SQL> conn system/123456
연결되었습니다.
SQL> create user project identified by project
2 ;
사용자가 생성되었습니다.
SQL> grant connect, resource to project;
권한이 부여되었습니다.
SQL> conn project/project
연결되었습니다.
SQL> create table board1 (
2 seq number not null primary key,
3 subject varchar2(100) not null,
4 writer varchar2(30) not null,
5 mail varchar2(50),
6 password varchar2(10) not null,
7 content varchar2(2000),
8 hit number not null,
9 wip varchar2(15) not null,
10 wdate date not null
11 );
테이블이 생성되었습니다.
SQL> conn system/123456
연결되었습니다.
SQL> grant unlimited tablespace to project;
권한이 부여되었습니다.
SQL> conn project/project
연결되었습니다.
insert into board1 values (
board_seq.nextval, '제목', '이름', 'test@test.com',
'1234', '내용', 0, '000.000.000.000', sysdate
);
SQL> select * from board1;
SEQ
----------
SUBJECT
--------------------------------------------------------------------------------
WRITER
------------------------------------------------------------
MAIL
--------------------------------------------------------------------------------
PASSWORD
--------------------
CONTENT
--------------------------------------------------------------------------------
HIT WIP WDATE
---------- ------------------------------ --------
1
SEQ
----------
SUBJECT
--------------------------------------------------------------------------------
WRITER
------------------------------------------------------------
MAIL
--------------------------------------------------------------------------------
PASSWORD
--------------------
CONTENT
--------------------------------------------------------------------------------
HIT WIP WDATE
---------- ------------------------------ --------
제목
SEQ
----------
SUBJECT
--------------------------------------------------------------------------------
WRITER
------------------------------------------------------------
MAIL
--------------------------------------------------------------------------------
PASSWORD
--------------------
CONTENT
--------------------------------------------------------------------------------
HIT WIP WDATE
---------- ------------------------------ --------
이름
SEQ
----------
SUBJECT
--------------------------------------------------------------------------------
WRITER
------------------------------------------------------------
MAIL
--------------------------------------------------------------------------------
PASSWORD
--------------------
CONTENT
--------------------------------------------------------------------------------
HIT WIP WDATE
---------- ------------------------------ --------
test@test.com
SEQ
----------
SUBJECT
--------------------------------------------------------------------------------
WRITER
------------------------------------------------------------
MAIL
--------------------------------------------------------------------------------
PASSWORD
--------------------
CONTENT
--------------------------------------------------------------------------------
HIT WIP WDATE
---------- ------------------------------ --------
1234
SEQ
----------
SUBJECT
--------------------------------------------------------------------------------
WRITER
------------------------------------------------------------
MAIL
--------------------------------------------------------------------------------
PASSWORD
--------------------
CONTENT
--------------------------------------------------------------------------------
HIT WIP WDATE
---------- ------------------------------ --------
내용
SEQ
----------
SUBJECT
--------------------------------------------------------------------------------
WRITER
------------------------------------------------------------
MAIL
--------------------------------------------------------------------------------
PASSWORD
--------------------
CONTENT
--------------------------------------------------------------------------------
HIT WIP WDATE
---------- ------------------------------ --------
0 000.000.000.000 18/07/19
SEQ
----------
SUBJECT
--------------------------------------------------------------------------------
WRITER
------------------------------------------------------------
MAIL
--------------------------------------------------------------------------------
PASSWORD
--------------------
CONTENT
--------------------------------------------------------------------------------
HIT WIP WDATE
---------- ------------------------------ --------
페이지 네비게이션
board_list1.jsp 3(7)
board_write1.jsp 1
board_write1_ok.jsp(*) 2
board_view1.jsp 4
board_modify1.jsp 5
board_modify1_ok.jsp(*) 6
board_delete1.jsp 5
board_delete1_ok.jsp(*) 6
boardEx01.board1.board_write1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_write.css">
<script type="text/javascript">
window.onload = function() {
document.getElementById('submit1').onclick = function() {
alert('click');
};
};
</script>
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<form action="./board_write1_ok.jsp" method="post" name="frm">
<div class="contents_sub">
<!--게시판-->
<div class="board_write">
<table>
<tr>
<th class="top">글쓴이</th>
<td class="top" colspan="3"><input type="text" name="writer" value="" class="board_view_input_mail" maxlength="5" /></td>
</tr>
<tr>
<th>제목</th>
<td colspan="3"><input type="text" name="subject" value="" class="board_view_input" /></td>
</tr>
<tr>
<th>비밀번호</th>
<td colspan="3"><input type="password" name="password" value="" class="board_view_input_mail"/></td>
</tr>
<tr>
<th>내용</th>
<td colspan="3"><textarea name="content" class="board_editor_area"></textarea></td>
</tr>
<tr>
<th>이메일</th>
<td colspan="3"><input type="text" name="mail1" value="" class="board_view_input_mail"/> @ <input type="text" name="mail2" value="" class="board_view_input_mail"/></td>
</tr>
</table>
<table>
<tr>
<br />
<td style="text-align:left;border:1px solid #e0e0e0;background-color:f9f9f9;padding:5px">
<div style="padding-top:7px;padding-bottom:5px;font-weight:bold;padding-left:7px;font-family: Gulim,Tahoma,verdana;">※ 개인정보 수집 및 이용에 관한 안내</div>
<div style="padding-left:10px;">
<div style="width:97%;height:95px;font-size:11px;letter-spacing: -0.1em;border:1px solid #c5c5c5;background-color:#fff;padding-left:14px;padding-top:7px;">
1. 수집 개인정보 항목 : 회사명, 담당자명, 메일 주소, 전화번호, 홈페이지 주소, 팩스번호, 주소 <br />
2. 개인정보의 수집 및 이용목적 : 제휴신청에 따른 본인확인 및 원활한 의사소통 경로 확보 <br />
3. 개인정보의 이용기간 : 모든 검토가 완료된 후 3개월간 이용자의 조회를 위하여 보관하며, 이후 해당정보를 지체 없이 파기합니다. <br />
4. 그 밖의 사항은 개인정보취급방침을 준수합니다.
</div>
</div>
<div style="padding-top:7px;padding-left:5px;padding-bottom:7px;font-family: Gulim,Tahoma,verdana;">
<input type="checkbox" name="info" value="1" class="input_radio"> 개인정보 수집 및 이용에 대해 동의합니다.
</div>
</td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp'">목록</button>
</div>
<div class="align_right">
<button type="button" id="submit1" class="btn_write btn_txt01" style="cursor: pointer;">등록</button>
</div>
</div>
<!--//게시판-->
</div>
</form>
</div>
<!-- 하단 디자인 -->
</body>
</html>
javascript 영역에서 id="submit1"으로 등록을 누를때 확인을 한번 하는 구문을 추가해주었다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_write.css">
<script type="text/javascript">
window.onload = function() {
document.getElementById('submit1').onclick = function() {
//alert('click');
if(document.frm.info.checked == false) {
alert('동의하셔야 합니다.');
return;
}
if(document.frm.writer.value.trim() == "") {
alert('이름을 입력하셔야 합니다.');
return;
}
if(document.frm.password.value.trim() == "") {
alert('비밀번호를 입력하셔야 합니다.');
return;
}
if(document.frm.subject.value.trim() == "") {
alert('제목을 입력하셔야 합니다.');
return;
}
document.frm.submit();
};
};
</script>
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<form action="./board_write1_ok.jsp" method="post" name="frm">
<div class="contents_sub">
<!--게시판-->
<div class="board_write">
<table>
<tr>
<th class="top">글쓴이</th>
<td class="top" colspan="3"><input type="text" name="writer" value="" class="board_view_input_mail" maxlength="5" /></td>
</tr>
<tr>
<th>제목</th>
<td colspan="3"><input type="text" name="subject" value="" class="board_view_input" /></td>
</tr>
<tr>
<th>비밀번호</th>
<td colspan="3"><input type="password" name="password" value="" class="board_view_input_mail"/></td>
</tr>
<tr>
<th>내용</th>
<td colspan="3"><textarea name="content" class="board_editor_area"></textarea></td>
</tr>
<tr>
<th>이메일</th>
<td colspan="3"><input type="text" name="mail1" value="" class="board_view_input_mail"/> @ <input type="text" name="mail2" value="" class="board_view_input_mail"/></td>
</tr>
</table>
<table>
<tr>
<br />
<td style="text-align:left;border:1px solid #e0e0e0;background-color:f9f9f9;padding:5px">
<div style="padding-top:7px;padding-bottom:5px;font-weight:bold;padding-left:7px;font-family: Gulim,Tahoma,verdana;">※ 개인정보 수집 및 이용에 관한 안내</div>
<div style="padding-left:10px;">
<div style="width:97%;height:95px;font-size:11px;letter-spacing: -0.1em;border:1px solid #c5c5c5;background-color:#fff;padding-left:14px;padding-top:7px;">
1. 수집 개인정보 항목 : 회사명, 담당자명, 메일 주소, 전화번호, 홈페이지 주소, 팩스번호, 주소 <br />
2. 개인정보의 수집 및 이용목적 : 제휴신청에 따른 본인확인 및 원활한 의사소통 경로 확보 <br />
3. 개인정보의 이용기간 : 모든 검토가 완료된 후 3개월간 이용자의 조회를 위하여 보관하며, 이후 해당정보를 지체 없이 파기합니다. <br />
4. 그 밖의 사항은 개인정보취급방침을 준수합니다.
</div>
</div>
<div style="padding-top:7px;padding-left:5px;padding-bottom:7px;font-family: Gulim,Tahoma,verdana;">
<input type="checkbox" name="info" value="1" class="input_radio"> 개인정보 수집 및 이용에 대해 동의합니다.
</div>
</td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp'">목록</button>
</div>
<div class="align_right">
<button type="button" id="submit1" class="btn_write btn_txt01" style="cursor: pointer;">등록</button>
</div>
</div>
<!--//게시판-->
</div>
</form>
</div>
<!-- 하단 디자인 -->
</body>
</html>
등록버튼을 눌러서 다음 프로세스로 넘어갔다
board_write1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_write.css">
<script type="text/javascript">
window.onload = function() {
document.getElementById('submit1').onclick = function() {
//alert('click');
if(document.frm.info.checked == false) {
alert('동의하셔야 합니다.');
return;
}
if(document.frm.writer.value.trim() == "") {
alert('이름을 입력하셔야 합니다.');
return;
}
if(document.frm.password.value.trim() == "") {
alert('비밀번호를 입력하셔야 합니다.');
return;
}
if(document.frm.subject.value.trim() == "") {
alert('제목을 입력하셔야 합니다.');
return;
}
document.frm.submit();
};
};
</script>
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<form action="./board_write1_ok.jsp" method="post" name="frm">
<div class="contents_sub">
<!--게시판-->
<div class="board_write">
<table>
<tr>
<th class="top">글쓴이</th>
<td class="top" colspan="3"><input type="text" name="writer" value="" class="board_view_input_mail" maxlength="5" /></td>
</tr>
<tr>
<th>제목</th>
<td colspan="3"><input type="text" name="subject" value="" class="board_view_input" /></td>
</tr>
<tr>
<th>비밀번호</th>
<td colspan="3"><input type="password" name="password" value="" class="board_view_input_mail"/></td>
</tr>
<tr>
<th>내용</th>
<td colspan="3"><textarea name="content" class="board_editor_area"></textarea></td>
</tr>
<tr>
<th>이메일</th>
<td colspan="3"><input type="text" name="mail1" value="" class="board_view_input_mail"/> @ <input type="text" name="mail2" value="" class="board_view_input_mail"/></td>
</tr>
</table>
<table>
<tr>
<br />
<td style="text-align:left;border:1px solid #e0e0e0;background-color:f9f9f9;padding:5px">
<div style="padding-top:7px;padding-bottom:5px;font-weight:bold;padding-left:7px;font-family: Gulim,Tahoma,verdana;">※ 개인정보 수집 및 이용에 관한 안내</div>
<div style="padding-left:10px;">
<div style="width:97%;height:95px;font-size:11px;letter-spacing: -0.1em;border:1px solid #c5c5c5;background-color:#fff;padding-left:14px;padding-top:7px;">
1. 수집 개인정보 항목 : 회사명, 담당자명, 메일 주소, 전화번호, 홈페이지 주소, 팩스번호, 주소 <br />
2. 개인정보의 수집 및 이용목적 : 제휴신청에 따른 본인확인 및 원활한 의사소통 경로 확보 <br />
3. 개인정보의 이용기간 : 모든 검토가 완료된 후 3개월간 이용자의 조회를 위하여 보관하며, 이후 해당정보를 지체 없이 파기합니다. <br />
4. 그 밖의 사항은 개인정보취급방침을 준수합니다.
</div>
</div>
<div style="padding-top:7px;padding-left:5px;padding-bottom:7px;font-family: Gulim,Tahoma,verdana;">
<input type="checkbox" name="info" value="1" class="input_radio"> 개인정보 수집 및 이용에 대해 동의합니다.
</div>
</td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp'">목록</button>
</div>
<div class="align_right">
<button type="button" id="submit1" class="btn_write btn_txt01" style="cursor: pointer;">등록</button>
</div>
</div>
<!--//게시판-->
</div>
</form>
</div>
<!-- 하단 디자인 -->
</body>
</html>
board_write1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String subject = request.getParameter("subject");
String writer = request.getParameter("writer");
String mail = request.getParameter("mail1") + "@" + request.getParameter("mail2");
String password = request.getParameter("password");
String content = request.getParameter("content");
String wip = request.getRemoteAddr();
String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
String dbUser = "project";
String dbPassword = "project";
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 1;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
String sql = "insert into board1 values (board_seq.nextval, ?, ?, ?, ?, ?, 0, ?, sysdate)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subject);
pstmt.setString(2, writer);
pstmt.setString(3, mail);
pstmt.setString(4, password);
pstmt.setString(5, content);
pstmt.setString(6, wip);
int result = pstmt.executeUpdate();
if(result == 1) {
flag = 0;
}
} catch(ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('글쓰기에 성공했습니다.');");
out.println("location.href='./board_list1.jsp';");
} else if(flag == 1) {
out.println("alert('글쓰기에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
board.board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- Java 전처리 -->
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
String dbUser = "project";
String dbPassword = "project";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
String sql = "select seq, subject, writer, to_char(wdate, 'YYYY/MM/DD') wdate, hit from board1 order by seq desc";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
sb.append("<tr>");
sb.append(" <td> </td>");
//sb.append(" <td>").append(rs.getString("seq")).append("</td>");
sb.append(" <td>" + rs.getString("seq") + "</td>");
sb.append(" <td class='left'><a href='./board_view1.jsp?seq=" + rs.getString("seq") + "'>" + rs.getString("subject") + "</a> <img src='../images/icon_hot.gif' alt='HOT'></td>");
sb.append(" <td>" + rs.getString("writer") + "</td>");
sb.append(" <td>" + rs.getString("wdate") + "</td>");
sb.append(" <td>" + rs.getString("hit") + "</td>");
sb.append(" <td> </td>");
sb.append("</tr>");
}
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_list.css">
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<div class="contents_sub">
<div class="board_top">
<div class="bold">총 <span class="txt_orange">1</span>건</div>
</div>
<!--게시판-->
<div class="board">
<table>
<tr>
<th width="3%"> </th>
<th width="5%">번호</th>
<th>제목</th>
<th width="10%">글쓴이</th>
<th width="17%">등록일</th>
<th width="5%">조회</th>
<th width="3%"> </th>
</tr>
<tr>
<!-- 시작 -->
<%= sb %>
<!-- 끝 -->
</tr>
</table>
</div>
<!--//게시판-->
<div class="align_right">
<button type="button" class="btn_write btn_txt01" style="cursor: pointer;" onclick="location.href='board_write1.jsp'">쓰기</button>
</div>
</div>
</div>
<!--//하단 디자인 -->
</body>
</html>
String sql = "select seq, subject, writer, to_char(wdate, 'YYYY/MM/DD') wdate, hit from board1 order by seq desc"; 로 수정하여 위 그림과 같이 seq=14라며 get 방식으로 데이터를 읽어올수 있다.
board1.board1_view
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String seq = request.getParameter("seq");
String subject = "";
String writer = "";
String wdate = "";
String hit = "";
String content = "";
String mail = "";
String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
String dbUser = "project";
String dbPassword = "project";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
String sql = "select subject, writer, wdate, hit, content, mail from board1 where seq = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
if(rs.next()) {
subject = rs.getString("subject");
writer = rs.getString("writer");
wdate = rs.getString("wdate");
hit = rs.getString("hit");
content = rs.getString("content");
mail = rs.getString("mail");
}
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_view.css">
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<div class="contents_sub">
<!--게시판-->
<div class="board_view">
<table>
<tr>
<th width="10%">제목</th>
<td width="60%"><%= subject %></td>
<th width="10%">등록일</th>
<td width="20%"><%= wdate %></td>
</tr>
<tr>
<th>글쓴이</th>
<td><%= writer %><%= '(' + mail + ')' %></td>
<th>조회</th>
<td><%= hit %></td>
</tr>
<tr>
<td colspan="4" height="200" valign="top" style="padding: 20px; line-height: 160%"><%= content %></td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp'">목록</button>
</div>
<div class="align_right">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_modify1.jsp?seq=<%= seq %>'">수정</button>
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_delete1.jsp?seq=<%= seq %>'">삭제</button>
<button type="button" class="btn_write btn_txt01" style="cursor: pointer;" onclick="location.href='board_write1.jsp'">쓰기</button>
</div>
</div>
<!--//게시판-->
</div>
</div>
<!-- 하단 디자인 -->
</body>
</html>
board1.board1_delete1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String seq = request.getParameter("seq");
String subject = "";
String writer = "";
String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
String dbUser = "project";
String dbPassword = "project";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
String sql = "select subject, writer from board1 where seq = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
if(rs.next()) {
subject = rs.getString("subject");
writer = rs.getString("writer");
}
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_write.css">
<script type="text/javascript">
window.onload = function() {
document.getElementById('submit1').onclick = function() {
if(document.frm.password.value.trim() == "") {
alert('비밀번호를 입력하셔야 합니다.');
return;
}
document.frm.submit();
};
};
</script>
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<form action="./board_delete1_ok.jsp" method="post" name="frm">
<input type="hidden" name="seq" value="<%=seq %>" /> <!-- delete할 때 seq를 받아오기 위해 hidden으로해서 작성 / 브라우저에서 소스보기 하면 값 확인가능 -->
<div class="contents_sub">
<!--게시판-->
<div class="board_write">
<table>
<tr>
<th class="top">글쓴이</th>
<td class="top" colspan="3"><input type="text" name="writer" value="<%= writer %>" class="board_view_input_mail" maxlength="5" readonly/></td>
</tr>
<tr>
<th>제목</th>
<td colspan="3"><input type="text" name="subject" value="<%= subject %>" class="board_view_input" readonly/></td>
</tr>
<tr>
<th>비밀번호</th>
<td colspan="3"><input type="password" name="password" value="" class="board_view_input_mail"/></td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp'">목록</button>
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_view1.jsp?seq=<%=seq %>'">보기</button>
</div>
<div class="align_right">
<button type="button" id="submit1" class="btn_write btn_txt01" style="cursor: pointer;">삭제</button>
</div>
</div>
<!--//게시판-->
</div>
</form>
</div>
<!-- 하단 디자인 -->
</body>
</html>
비밀번호를 입력하지 않았을 때
board1.board_delete1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String seq = request.getParameter("seq");
String password = request.getParameter("password");
String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
String dbUser = "project";
String dbPassword = "project";
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 2;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
String sql = "delete from board1 where seq = ? and password = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
pstmt.setString(2, password);
int result = pstmt.executeUpdate();
if(result == 1) {
// 정상
flag = 0;
} else if (result == 0) {
// 비밀번호 오류
flag = 1;
}
} catch(ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('글 삭제에 성공했습니다.');");
out.println("location.href='./board_list1.jsp';");
} else if(flag == 1) {
out.println("alert('비밀번호 오류입니다.');");
out.println("history.back();");
} else if(flag == 2) {
out.println("alert('글 삭제에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
board1.board_modify1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String seq = request.getParameter("seq");
String subject = "";
String writer = "";
String password = "";
String content = "";
String mail = "";
String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
String dbUser = "project";
String dbPassword = "project";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
String sql = "select writer from board1 where seq = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
if(rs.next()) {
writer = rs.getString("writer");
}
} catch (ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/board_write.css">
<script type="text/javascript">
window.onload = function() {
document.getElementById('submit1').onclick = function() {
if(document.frm.writer.value.trim() == "") {
alert('이름을 입력하셔야 합니다.');
return;
}
if(document.frm.password.value.trim() == "") {
alert('비밀번호를 입력하셔야 합니다.');
return;
}
if(document.frm.subject.value.trim() == "") {
alert('제목을 입력하셔야 합니다.');
return;
}
document.frm.submit();
};
};
</script>
</head>
<body>
<!-- 상단 디자인 -->
<div class="con_title">
<h3>게시판</h3>
<p>HOME > 게시판 > <strong>게시판</strong></p>
</div>
<div class="con_txt">
<form action="./board_modify1_ok.jsp" method="post" name="frm">
<input type="hidden" name="seq" value="<%=seq %>" />
<div class="contents_sub">
<!--게시판-->
<div class="board_write">
<table>
<tr>
<th class="top">글쓴이</th>
<td class="top" colspan="3"><input type="text" name="writer" value="<%=writer %>" class="board_view_input_mail" maxlength="5" readonly/></td>
</tr>
<tr>
<th>제목</th>
<td colspan="3"><input type="text" name="subject" value="<%=subject %>" class="board_view_input" /></td>
</tr>
<tr>
<th>비밀번호</th>
<td colspan="3"><input type="password" name="password" value="" class="board_view_input_mail"/></td>
</tr>
<tr>
<th>내용</th>
<td colspan="3"><textarea name="content" class="board_editor_area"></textarea></td>
</tr>
<tr>
<th>이메일</th>
<td colspan="3"><input type="text" name="mail1" value="" class="board_view_input_mail"/> @ <input type="text" name="mail2" value="" class="board_view_input_mail"/></td>
</tr>
</table>
</div>
<div class="btn_area">
<div class="align_left">
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_list1.jsp'">목록</button>
<button type="button" class="btn_list btn_txt02" style="cursor: pointer;" onclick="location.href='board_view1.jsp?seq=<%=seq %>'">보기</button>
</div>
<div class="align_right">
<button type="button" id="submit1" class="btn_write btn_txt01" style="cursor: pointer;">수정</button>
</div>
</div>
<!--//게시판-->
</div>
</form>
</div>
<!-- 하단 디자인 -->
</body>
</html>
board1.board_modify1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>
<%
request.setCharacterEncoding("utf-8");
String seq = request.getParameter("seq");
String subject = request.getParameter("subject");
String mail = request.getParameter("mail1") + "@" + request.getParameter("mail2");
String password = request.getParameter("password");
String content = request.getParameter("content");
String wip = request.getRemoteAddr();
String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
String dbUser = "project";
String dbPassword = "project";
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 2;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
String sql = "update board1 set subject=?, mail=?, password=?, content=? where seq = ? and password = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subject);
pstmt.setString(2, mail);
pstmt.setString(3, password);
pstmt.setString(4, content);
pstmt.setString(5, seq);
pstmt.setString(6, password);
int result = pstmt.executeUpdate();
if(result == 1) {
// 정상
flag = 0;
} else if (result == 0) {
// 비밀번호 오류
flag = 1;
}
} catch(ClassNotFoundException e) {
System.out.println("[에러] " + e.getMessage());
} catch(SQLException e) {
System.out.println("[에러] " + e.getMessage());
} finally {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('글 수정에 성공했습니다.');");
out.println("location.href='./board_list1.jsp';");
} else if(flag == 1) {
out.println("alert('비밀번호 오류입니다.');");
out.println("history.back();");
} else if(flag == 2) {
out.println("alert('글 수정에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
반응형
댓글