728x90
728x90
BIG
일단 게시판하나만듬
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판 글쓰기</title>
</head>
<body>
<center>
<h1>게시판 글쓰기</h1>
<form name = "boardForm" action="boardWrite.jsp">
<table name="board1">
<tr>
<td>작성자</td>
<td><input type = "text" id = "name" name = "name"
placeholder="작성자를 입력하세요"></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type = "password" name = "pwd" id = "pwd"
placeholder="비밀번호를 입력하세요">
(게시물 수정 삭제시 필요합니다)
</td>
</tr>
<tr>
<td>이메일</td>
<td><input type = "text" name = "mail" id = "mail"
placeholder="이메일을 입력하세요"></td>
</tr>
<tr>
<td>글 제목</td>
<td><input type = "text" name = "title" id = "title"
placeholder="글제목을 입력하세요"></td>
</tr>
<tr>
<td>글 내용</td>
<td>
<textarea rows="4" cols="23" name = "content" id ="content"
placeholder="글내용을 입력하세요"></textarea>
</td>
</tr>
<tr>
<td colspan=2 align="center">
<input type = "button" value="등 록" onclick="boardCheck()">
<input type = "reset" value="다시작성"></td>
</tr>
</table>
</form>
</center>
</body>
<script type="text/javascript">
function boardCheck(){
const name=document.getElementById("name");//문서(html) id가 "id"값인 Object
if(name.value.trim().length==0){
alert("작성자를 입력하세요");
name.value="";
name.focus();
return;
}
const pwd=document.getElementById("pwd");
if(pwd.value.length==0){
alert("패스워드를 입력하세요.");
pwd.focus();
return;
}
const title=document.getElementById("title");
if(title.value.length==0){
alert("글제목을 입력하세요.");
title.focus();
return;
}
const content=document.getElementById("content");
if(content.value.length==0){
alert("글내용을 입력하세요.");
content.focus();
return;
}
//return true;//아이디와 패스드워 전부 입력 submit 수행
document.boardForm.submit();
}
</script>
</html>
빈연결
package board;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCUtil {
//드라이버로딩 정적 블록
static {
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();System.exit(0);
}
}
//싱글톤 생성자~
// 싱글톤
//1. static memberDAO 객체 필드선언
//2. 생성자 private
//3. static 인 메소드하나를 만든다 getInstance : 생성자를 호출해서 객체생성 반환
private static JDBCUtil dao=null;
private JDBCUtil() {}
public static JDBCUtil getInstance() {
if(dao==null)dao=new JDBCUtil();
return dao;
}
//공통모듈
//Connection 객체 생성 반환
public Connection getConnection() {
Connection conn=null;
String url="jdbc:oracle:thin:@localhost:1521:xe";
try {
conn=DriverManager.getConnection(url, "JAVADB", "1234");
} catch (SQLException e) {
e.printStackTrace();System.exit(0);
}
return conn;
}
//닫기 - pstmt, conn
public void close(PreparedStatement pstmt,Connection conn) {
try {
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//닫기 - rs, pstmt, conn
public void close(ResultSet rs,PreparedStatement pstmt,Connection conn) {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
빈만듬
package board;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BoardBean {
int bno;
String name,pwd,email,title,content;
public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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 BoardBean() {
super();
// TODO Auto-generated constructor stub
}
public BoardBean(String name, String pwd, String email, String title, String content) {
super();
this.name = name;
this.pwd = pwd;
this.email = email;
this.title = title;
this.content = content;
}
public BoardBean(int bno, String name, String pwd, String email, String title, String content) {
super();
this.bno = bno;
this.name = name;
this.pwd = pwd;
this.email = email;
this.title = title;
this.content = content;
}
@Override
public String toString() {
return "BoardVO [bno=" + bno + ", name=" + name + ", pwd=" + pwd + ", email=" + email + ", title=" + title
+ ", content=" + content + "]";
}
public boolean insert() {
boolean r = false;
JDBCUtil dao = JDBCUtil.getInstance();
Connection conn = dao.getConnection();
PreparedStatement pstmt = null;
String sql = "insert into boardtbl values (bno_seq.nextval,?,?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, pwd);
pstmt.setString(3, email);
pstmt.setString(4, title);
pstmt.setString(5, content);
int result = pstmt.executeUpdate();
if(result==1) r=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
dao.close(pstmt, conn);
}
return r;
}
}
그다음 디비만듬
create table boardTbl(
bno number primary key,
name nvarchar2(20) not null,
pwd varchar2(20) not null,
email varchar2(30),
title nvarchar2(50) not null,
content nvarchar2(1000) not null);
create SEQUENCE bno_seq;
그다음 넘어가는 창만듬
<%@page import="board.BoardBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>입력완료</title>
</head>
<body>
<center>
<h1>입력 완료된 정보</h1>
<%
//한글깨져서그렇고
request.setCharacterEncoding("utf-8");
%>
<!--MemberBean 클래스 객체 생성 : 객체명 id 값(member) -->
<jsp:useBean id="board" class="board.BoardBean" />
<!-- 회원가입페이지에서 넘어온 모든 parameter 값을 전부 member의 필드값에 설정 -->
<jsp:setProperty property="*" name="board" />
<!-- 회원가입 처리 -->
<%
if (board.insert()) {
%>
<!--<jsp:getProperty property="name" name="board" />님 환영합니다.
<br>-->
<%
} else {
response.sendRedirect("boardWriteForm.jsp");
}
%>
<!-- <%=board.toString()%> -->
<table style="border:3px; border-style:solid">
<tr>
<td>
<table>
<tr>
<td>작성자</td>
<td><%=board.getName()%></td>
</tr>
<tr>
<td>비밀번호</td>
<td><%=board.getPwd()%></td>
</tr>
<tr>
<td>이메일</td>
<td><%=board.getEmail()%></td>
</tr>
<tr>
<td>글제목</td>
<td><%=board.getTitle()%></td>
</tr>
<tr>
<td>글내용</td>
<td><%=board.getContent()%></td>
</tr>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>
728x90
반응형
BIG
'🎵JSP' 카테고리의 다른 글
[JSP] JSTL c:set c:out (0) | 2023.02.13 |
---|---|
[JSP]EL태그 (0) | 2023.02.13 |
[JSP]Session (0) | 2023.02.09 |
[JSP] Cookie(쿠키생성, 시간설정, 보내기,다른페이지) (0) | 2023.02.09 |
[JSP] Run On Server (Always use this server when running this project 해제하는 법) (0) | 2023.02.09 |
댓글