학원/JSP - 학원
게시판 1단계 2
수풀속의고라니
2022. 3. 25. 14:07
728x90
board.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
function check_ok(){
if(document.reg_form.b_name.value.length ==0){
alert("작성자를 써주세요");
reg_form.b_name.focus();
return;
}
if(document.reg_form.b_title.value.length ==0){
alert("제목을 써주세요");
reg_form.b_title.focus();
return;
}
if(document.reg_form.b_content.value.length ==0){
alert("글내용을 써주세요");
reg_form.b_content.focus();
return;
}
document.reg_form.submit();
}
|
cs |
write.jsp
- <head>안에 <script type="text/javascript" src="board.js" charset="utf-8"></script> 이거 추가
- <form>에 폼이름 지정 - reg_form
BoardDBBean.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package magic.board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance() {
return instance;
}
public Connection getConnection() throws Exception {
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
return ds.getConnection();
}
public int insertBoard(BoardBean board) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;;
String sql="";
int re = -1;
int number;
try {
con = getConnection();
sql = "select max(b_id) from boardT";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()) {
number = rs.getInt(1)+1;
}else {
number=1;
}
sql = "insert into boardT (b_id,b_name,b_email,b_title,b_content) values(?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setInt(1, number);
ps.setString(2, board.getB_name());
ps.setString(3, board.getB_email());
ps.setString(4, board.getB_title());
ps.setString(5, board.getB_content());
ps.executeUpdate();
re = 1;
ps.close();
con.close();
System.out.println("추가 성공");
} catch (Exception e) {
System.out.println("추가 실패");
e.printStackTrace();
}finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return re;
}
}
|
cs |
- b_id의 최대값을 구하는 쿼리문을 먼저 받아서 조회하고 그 값을 rs에 저장
- if문을 사용해서 rs에 값이 있다면 1번 컬럼에 +1을 해서 number에 넣어줌
- 만약 값이 없다면 number에 1을 넣어줌
- 다음으로 값을 집어넣는 쿼리문을 읽도록 설정하고, 각 컬럼에 값 추가
728x90