jsp와 스크립트릿을 활용하여 간단한 게시판을 만들어 보겠습니다
package kr.ac.mlec.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionFactory {
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "hr";
String password = "hr";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
[결과]
[만들어야 할 것]
1. DB 구축
2. ConnectionFactory.java 만들기
3. JDBCClose.java 만들기
4. board.jsp 만들기
5. ojdbc6 혹은 ojdbc8.jar파일 추가 : WEB-INF/lib/ojdbc8.jar
[1. DB구축]
- DB를 생성하고 다음과 같이 입력해준 뒤,
" insert into t_board ~ '내용입니다'); " 부분을 5번정도 ctrl+ enter해줍니다
create table t_board(
no number(5) primary key
, title varchar2(200) not null
, writer varchar2(100) not null
, content varchar2(4000) not null
, view_cnt number(5) default 0
, reg_date date default sysdate
);
create sequence seq_t_board_no;
insert into t_board(no, title, writer, content)
values(seq_t_board_no.nextval, '제목입니다', '홍길동', '내용입니다');
commit;
[2. ConnectionFactory.java 만들기]
package kr.ac.mlec.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionFactory {
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "hr";
String password = "hr";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
[3. JDBCClose.java 만들기]
package kr.ac.mlec.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCClose {
public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement pstmt) {
if(pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection conn, PreparedStatement pstmt) {
close(pstmt);
close(conn);
}
}
[4. board.jsp 만들기]
<%@page import="kr.ac.mlec.util.JDBCClose"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="kr.ac.mlec.util.ConnectionFactory"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Connection conn = new ConnectionFactory().getConnection();
//string: +쓸 때 속도느려져서 StringBuilder를 쓰겠다
StringBuilder sql = new StringBuilder();
sql.append("select no, title, writer, to_char(reg_date, 'yyyy-mm-dd') as reg_date");
sql.append(" from t_board ");
sql.append(" order by no desc ");
//sql을 string으로 다시 바꿔줄때 toString을 쓴다
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
ResultSet rs = pstmt.executeQuery();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align ="center">
<hr width="70%">
<h1>전제게시물 목록</h1>
<hr width="70%">
<table border="1" style="width: 70%;">
<tr>
<th width="7%">번호</th>
<th>제목</th>
<th width="16%">작성자</th>
<th width="20%">등록일</th>
</tr>
<%
while(rs.next()){
int no = rs.getInt("no");
String title = rs.getString("title");
String writer = rs.getString("writer");
String regDate = rs.getString("reg_date");
%>
<tr>
<td><%= no %></td>
<td><%= title %></td>
<td><%= writer %></td>
<td><%= regDate %></td>
</tr>
<%
}
%>
</table>
</div>
</body>
</html>
<%
JDBCClose.close(conn, pstmt);
%>
[5. ojdbc6 혹은 ojdbc8.jar파일 추가 >>경로: WEB-INF/lib/ojdbc8.jar]
이렇게 하면
다음과 같이 간단한 게시판이 완성됩니다~
제목은 제가 따로 DB에서 구분하기 쉽게 숫자를 앞에 붙여줬습니다
'Java > 게시판만들기' 카테고리의 다른 글
[2.게시판만들기] EL과 jstl을 활용하여 간단히 게시판 만들기 (0) | 2021.05.10 |
---|