Main funtions
1. Connection Pool
2. Action tags : <jsp:useBean...> / <jsp:setProperty...>
3. DTO, DAO class
4. Paging ( inline view)
5. Uploading files ( using cos.jar library)
dbcpAPITest.jsp
To check the connection
<%@ page language="java" contentType="text/html; charset=EUC-KR"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%
Connection conn = null;
try {
Context init = new InitialContext();
DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/orcl");
conn = ds.getConnection();
out.println("<h3>Connected.</h3>");
}catch(Exception e){
out.println("<h3>Failed to connect.</h3>");
e.printStackTrace();
}
%>
Create a SQL file and create a table and sequence.
BoardDatabean.java(DTO)
package upload;
import java.sql.Timestamp;
public class BoardDatabean {
private int num;
private String writer;
private String email;
private String subject;
private String passwd;
private Timestamp reg_date;
private int readcount;
private String content;
private String ip;
private String upload;
public int getNum() {
return num;
}
public String getWriter() {
return writer;
}
public String getEmail() {
return email;
}
public String getSubject() {
return subject;
}
public String getPasswd() {
return passwd;
}
public Timestamp getReg_date() {
return reg_date;
}
public int getReadcount() {
return readcount;
}
public String getContent() {
return content;
}
public String getIp() {
return ip;
}
public String getUpload() {
return upload;
}
public void setNum(int num) {
this.num = num;
}
public void setWriter(String writer) {
this.writer = writer;
}
public void setEmail(String email) {
this.email = email;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public void setReg_date(Timestamp reg_date) {
this.reg_date = reg_date;
}
public void setReadcount(int readcount) {
this.readcount = readcount;
}
public void setContent(String content) {
this.content = content;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setUpload(String upload) {
this.upload = upload;
}
}
BoardDBBean.java(DAO)
This is the basics of DAO class, and we will keep adding more codes.
package upload;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
// Singleton
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance(){ // static field
return instance;
}
// Method that gets connection from the Connection Pool
private Connection getConnection() throws Exception{
Context init = new InitialContext();
DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/orcl");
return ds.getConnection();
}
}
writeForm.jsp
<%@ page contentType="text/html; charset=utf-8" %>
<%@ include file="color.jsp"%>
<html>
<head>
<title>Your Post</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="check.js"></script>
</head>
<body bgcolor="<%=bodyback_c%>">
<center><b>Your Post</b>
<br>
<form method="post" name="writeform" action="writePro.jsp"
enctype="multipart/form-data">
<table width="430" border="1" cellspacing="0" cellpadding="0" bgcolor="<%=bodyback_c%>" align="center">
<tr>
<td align="right" colspan="2" bgcolor="<%=value_c%>">
<a href="list.jsp"> Posts</a>
</td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center">Name</td>
<td width="330">
<input autofocus type="text" size="10" maxlength="10" id="writer" name="writer"></td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center" >Title</td>
<td width="330">
<input type="text" size="40" maxlength="50" id="subject" name="subject"></td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center">Email</td>
<td width="330">
<input type="text" size="40" maxlength="30" id="email" name="email" ></td>
</tr>
<tr>
<td width="70" bgcolor="<%=value_c%>" align="center">Attach File</td>
<td width="330">
<input type="file" size="40" name="upload" ></td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center" >Content</td>
<td width="330" >
<textarea id="content" name="content" rows="13" cols="40"></textarea> </td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center" >Password</td>
<td width="330" >
<input type="password" size="8" maxlength="12" id="passwd" name="passwd">
</td>
</tr>
<tr>
<td colspan=2 bgcolor="<%=value_c%>" align="center">
<input type="submit" value="Post" >
<input type="reset" value="Reset">
<input type="button" value="List" OnClick="location.href='list.jsp'">
</td></tr></table>
</form>
</body>
</html>
writePro.jsp
<%@page import="upload.BoardDBBean"%>
<%@page import="upload.BoardDataBean"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
// Location of Directory
String path = request.getRealPath("upload");
System.out.println("path: " + path);
// Size of the files : 1MB
int size = 1024 * 1024;
// To upload files -> MultipartRequest object
MultipartRequest multi = new MultipartRequest(request,
path, // Location of Directory
size, // Size
"utf-8", // Encoding type
new DefaultFileRenamePolicy()); // Overlapped files
String writer = multi.getParameter("writer");
String subject = multi.getParameter("subject");
String email = multi.getParameter("email");
String content = multi.getParameter("content");
String passwd = multi.getParameter("passwd");
// Original file name: file name uploaded by the client
String upload0 = multi.getOriginalFileName("upload");
// Save file name
String upload = multi.getFilesystemName("upload");
BoardDataBean board = new BoardDataBean();
board.setWriter(writer);
board.setEmail(email);
board.setSubject(subject);
board.setContent(content);
board.setPasswd(passwd);
board.setIp(request.getRemoteAddr()); // Client's IP address
board.setUpload(upload); // Saved file's name
BoardDBBean dao = BoardDBBean.getInstance();
int result = dao.insert(board);
if (result == 1) {
%>
<script>
alert("Posted successfully.");
location.href = "list.jsp";
</script>
<%
} else {
%>
<script>
alert("Failed to post.");
history.go(-1);
</script>
<%
}
%>
list.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="upload.BoardDataBean"%>
<%@page import="java.util.List"%>
<%@page import="upload.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
int page_size = 10;
String pageNum = request.getParameter("page");
if(pageNum == null){
pageNum = "1";
}
int currentPage = Integer.parseInt(pageNum);
int startRow = (currentPage - 1) * page_size + 1;
int endRow = currentPage * page_size;
// Total data
int count = 0;
BoardDBBean dao = BoardDBBean.getInstance();
count = dao.getCount();
System.out.println("count:"+count);
List<BoardDataBean> list = null;
if(count > 0){
list = dao.getList(startRow, endRow);
}
System.out.println("list:"+list);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
if(count == 0){
%>
No posts yet.
<% }else{ %>
<a href="writeForm.jsp">Write</a> Total posts : <%=count %>
<table border=1 width=700 align=center>
<caption>List</caption>
<tr>
<th>No.</th>
<th>Title</th>
<th>Writer</th>
<th>Date</th>
<th>View</th>
<th>IP Address</th>
</tr>
<%
// start number
int number = count - (currentPage-1) * page_size;
SimpleDateFormat sd =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i=0; i<list.size(); i++){
BoardDataBean board = list.get(i);
%>
<tr>
<td><%=number-- %></td>
<td>
<a href="content.jsp?num=<%=board.getNum()%>&page=<%=currentPage%>">
<%=board.getSubject() %>
</a>
</td>
<td><%=board.getWriter() %></td>
<td><%=sd.format(board.getReg_date()) %></td>
<td><%=board.getReadcount() %></td>
<td><%=board.getIp() %></td>
</tr>
<% } // for end
%>
</table>
<% } // else end
%>
<!-- Page Link -->
<center>
<%
if(count > 0){
int pageCount=count/page_size + ((count%page_size==0) ? 0:1);
System.out.println("pageCount:"+pageCount);
// startRow & endRow
// 1 page : startRow=1, endRow=10
// 2 page : startRow=11, endRow=20
// 3 page : startRow=21, endRow=30
int startPage = ((currentPage-1)/10) * 10 + 1;
int block = 10;
int endPage = startPage + block - 1;
if(endPage > pageCount){
endPage = pageCount;
}
%>
<!-- Move to page 1 -->
<a href="list.jsp?page=1" style="text-decoration:none"> << </a>
<%
// Previous block
if(startPage > 10){
%>
<a href="list.jsp?page=<%=startPage-10%>">[Prev]</a>
<% }
for(int i=startPage; i<=endPage; i++){
if(i==currentPage){ %>
[<%=i%>]
<% }else{ %>
<a href="list.jsp?page=<%=i%>">[<%=i%>]</a>
<% }
} // for end
// Next block
if(endPage < pageCount){ %>
<a href="list.jsp?page=<%=startPage+10%>">[Next]</a>
<% } %>
<!-- Move to the last page -->
<a href="list.jsp?page=<%=pageCount%>"
style="text-decoration:none"> >> </a>
<%
}
%>
</center>
</body>
</html>
content.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="upload.BoardDataBean"%>
<%@page import="upload.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
int num = Integer.parseInt(request.getParameter("num"));
String nowpage = request.getParameter("page");
BoardDBBean dao = BoardDBBean.getInstance();
// view count +1 & get detail
BoardDataBean board = dao.updateContent(num);
SimpleDateFormat sd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String content = board.getContent().replace("\n", "<br>");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Detail</title>
</head>
<body>
<table border=1 width=500 align=center>
<caption>Detail</caption>
<tr>
<td>No.</td>
<td><%=board.getNum()%></td>
<td>View</td>
<td><%=board.getReadcount()%></td>
</tr>
<tr>
<td>Writer</td>
<td><%=board.getWriter()%></td>
<td>Date</td>
<td><%=sd.format(board.getReg_date())%></td>
</tr>
<tr>
<td>Title</td>
<td colspan=3><%=board.getSubject()%></td>
</tr>
<tr>
<td>Content</td>
<td colspan=3><%=board.getContent()%> <%=content%></td>
</tr>
<tr>
<td>Attached File</td>
<td colspan=3>
<!-- If there is files to attach --> <%
if (board.getUpload() != null) {
%> <a href="file_down.jsp?file_name=<%=board.getUpload()%>"> <%=board.getUpload()%>
</a> <%
}
%>
</td>
</tr>
<tr>
<td colspan=4 align=center><input type="button" value="Edit"
onClick="location.href='updateForm.jsp?num=<%=num%>&page=<%=nowpage%>'">
<input type="button" value="Delete"
onClick="location.href='deleteForm.jsp?num=<%=num%>&page=<%=nowpage%>'">
<input type="button" value="Go back to List"
onClick="location.href='list.jsp?page=<%=nowpage%>'"></td>
</tr>
</table>
</body>
</html>
updateForm.jsp
<%@page import="upload.BoardDataBean"%>
<%@page import="upload.BoardDBBean"%>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ include file="color.jsp"%>
<%
int num = Integer.parseInt(request.getParameter("num"));
String nowpage = request.getParameter("page");
BoardDBBean dao = BoardDBBean.getInstance();
// To get detail
BoardDataBean board = dao.getContent(num);
%>
<html>
<head>
<title>Your Post</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="check.js"></script>
</head>
<body bgcolor="<%=bodyback_c%>">
<center><b>Your Post</b>
<br>
<form method="post" name="writeform" action="updatePro.jsp"
enctype="multipart/form-data">
<input type="hidden" name="num" value="<%=num %>">
<input type="hidden" name="page" value="<%=nowpage %>">
<table width="430" border="1" cellspacing="0" cellpadding="0" bgcolor="<%=bodyback_c%>" align="center">
<tr>
<td align="right" colspan="2" bgcolor="<%=value_c%>">
<a href="list.jsp?page=<%=nowpage%>"> Posts</a>
</td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center">Name</td>
<td width="330">
<input autofocus type="text" size="10" maxlength="10" id="writer" name="writer"
value="<%=board.getWriter()%>"></td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center" >Title</td>
<td width="330">
<input type="text" size="40" maxlength="50" id="subject" name="subject" value="<%=board.getSubject()%>"></td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center">Email</td>
<td width="330">
<input type="text" size="40" maxlength="30" id="email" name="email" value="<%=board.getEmail()%>" ></td>
</tr>
<tr>
<td width="70" bgcolor="<%=value_c%>" align="center">Attach File</td>
<td width="330">
<input type="file" size="40" name="upload" ><%=board.getUpload() %></td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center" >Content</td>
<td width="330" >
<textarea id="content" name="content" rows="13" cols="40"><%=board.getContent() %></textarea> </td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center" >Password</td>
<td width="330" >
<input type="password" size="8" maxlength="12" id="passwd" name="passwd">
</td>
</tr>
<tr>
<td colspan=2 bgcolor="<%=value_c%>" align="center">
<input type="submit" value="Post" >
<input type="reset" value="Reset">
<input type="button" value="List" OnClick="location.href='list.jsp?page=<%=nowpage%>'">
</td></tr></table>
</form>
</body>
</html>
updatePro.jsp
<%@page import="upload.BoardDBBean"%>
<%@page import="upload.BoardDataBean"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
// Location of Directory
String path = request.getRealPath("upload");
System.out.println("path: " + path);
// Size of the files : 1MB
int size = 1024 * 1024;
// To upload files -> MultipartRequest object
MultipartRequest multi = new MultipartRequest(request,
path, // Location of Directory
size, // Size
"utf-8", // Encoding type
new DefaultFileRenamePolicy()); // Overlapped files
int num = Integer.parseInt(multi.getParameter("num"));
String nowpage = multi.getParameter("page");
String writer = multi.getParameter("writer");
String subject = multi.getParameter("subject");
String email = multi.getParameter("email");
String content = multi.getParameter("content");
String passwd = multi.getParameter("passwd");
// Original file name: file name uploaded by the client
String upload0 = multi.getOriginalFileName("upload");
// Save file name
String upload = multi.getFilesystemName("upload");
BoardDataBean board = new BoardDataBean();
board.setNum(num);
board.setWriter(writer);
board.setEmail(email);
board.setSubject(subject);
board.setContent(content);
board.setPasswd(passwd);
board.setIp(request.getRemoteAddr()); // Client's IP address
// board.setUpload(upload); // Saved file's name
BoardDBBean dao = BoardDBBean.getInstance();
BoardDataBean old = dao.getContent(num);
if(upload != null){ //If the attached file is edited
board.setUpload(upload);
}else{ // If the attached file is not edited
board.setUpload(old.getUpload());
}
System.out.println(old.getPasswd());
System.out.println(passwd);
// To compare passwords
if(old.getPasswd().equals(passwd)) {
int result = dao.update(board);
if(result ==1){
%>
<script>
alert("Posted successfully.");
location.href="list.jsp?page=<%=nowpage%>";
</script>
<% }
}else{ // Incorrect Password %>
<script>
alert("Incorrect password.");
history.go(-1);
</script>
<%
}
%>
deleteForm.jsp
<%@page import="upload.BoardDataBean"%>
<%@page import="upload.BoardDBBean"%>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ include file="color.jsp"%>
<%
int num = Integer.parseInt(request.getParameter("num"));
String nowpage = request.getParameter("page");
%>
<html>
<head>
<title>Your Post</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="check.js"></script>
</head>
<body bgcolor="<%=bodyback_c%>">
<center><b>Please enter your password to delete your post.</b>
<br>
<form method="post" name="writeform" action="deletePro.jsp">
<input type="hidden" name="num" value="<%=num %>">
<input type="hidden" name="page" value="<%=nowpage %>">
<table width="430" border="1" cellspacing="0" cellpadding="0" bgcolor="<%=bodyback_c%>" align="center">
<tr>
<td align="right" colspan="2" bgcolor="<%=value_c%>">
<a href="list.jsp?page=<%=nowpage%>"> Posts</a>
</td>
</tr>
<tr>
<td width="100" bgcolor="<%=value_c%>" align="center" >Password</td>
<td width="330" >
<input type="password" size="8" maxlength="12" id="passwd" name="passwd">
</td>
</tr>
<tr>
<td colspan=2 bgcolor="<%=value_c%>" align="center">
<input type="submit" value="Delete" >
<input type="reset" value="Reset">
<input type="button" value="List" OnClick="location.href='list.jsp?page=<%=nowpage%>'">
</td></tr></table>
</form>
</body>
</html>
deletePro.jsp
When it comes to deletion, the attached files have to be deleted along with the posts that are being deleted.
<%@page import="upload.BoardDataBean"%>
<%@page import="upload.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="board" class="upload.BoardDataBean"/>
<jsp:setProperty property="*" name="board"/>
<%
String nowpage = request.getParameter("page");
String path = request.getRealPath("upload");
System.out.println("Path: "+ path);
BoardDBBean dao = BoardDBBean.getInstance();
BoardDataBean old = dao.getContent(board.getNum());
// To compare the passwords
if(old.getPasswd().equals(board.getPasswd())){ // Correct password
int result = dao.delete(old, path);
if(result == 1) {
%>
<script>
alert("Successfully deleted.");
location.href="list.jsp?page=<%=nowpage%>";
</script>
<% } %>
<% }else { %> // Incorrect password
<script>
alert("Incorrect password.");
history.go(-1);
</script>
<% } %>
Please refer to my last post about deleting the attached file :
2022.09.14 - [Java] - Deleting attached files
BoardDBBean.java
// DAO(Data Access Object)
package upload;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
// Singleton
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance(){ // static field
return instance;
}
// Method that gets connection from the Connection Pool
private Connection getConnection() throws Exception{
Context init = new InitialContext();
DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/orcl");
return ds.getConnection();
}
// insert method
public int insert(BoardDataBean board) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql="insert into upload values(upload_seq.nextval,?,?,?,?,";
sql+="sysdate,?,?,?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, board.getWriter());
pstmt.setString(2, board.getEmail());
pstmt.setString(3, board.getSubject());
pstmt.setString(4, board.getPasswd());
pstmt.setInt(5, board.getReadcount()); // view count
pstmt.setString(6, board.getContent());
pstmt.setString(7, board.getIp());
pstmt.setString(8, board.getUpload());
result = pstmt.executeUpdate(); // To execute the SQL
}catch(Exception e) {
}finally {
if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
if(con != null) try { con.close();} catch(Exception e) {}
}
return result;
}
// getCount Method
public int getCount() {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
String sql="select count(*) from upload";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
result = rs.getInt(1);
result = rs.getInt("count(*)");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
if(con != null) try { con.close();} catch(Exception e) {}
if(rs != null) try { rs.close();} catch(Exception e) {}
}
return result;
}
// List : Extract 10 data
public List<BoardDataBean> getList(int start, int end){
List<BoardDataBean> list = new ArrayList<BoardDataBean>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
String sql="select * from (select rownum rnum, upload.* from ";
sql+=" (select * from upload order by num desc) upload) ";
sql+=" where rnum >=? and rnum <=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, start);
pstmt.setInt(2, end);
rs = pstmt.executeQuery();
while(rs.next()) {
BoardDataBean board = new BoardDataBean();
board.setNum(rs.getInt("num"));
board.setWriter(rs.getString("writer"));
board.setEmail(rs.getString("email"));
board.setSubject(rs.getString("subject"));
board.setSubject(rs.getString("subject"));
board.setPasswd(rs.getString("passwd"));
board.setReg_date(rs.getTimestamp("reg_date"));
board.setReadcount(rs.getInt("readcount"));
board.setContent(rs.getString("content"));
board.setIp(rs.getString("ip"));
board.setUpload(rs.getString("upload"));
list.add(board);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
if(con != null) try { con.close();} catch(Exception e) {}
if(rs != null) try { rs.close();} catch(Exception e) {}
}
return list;
}
// Detail page : view count +1 & detail information
public BoardDataBean updateContent(int num) {
BoardDataBean board = new BoardDataBean();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
String sql="update upload set readcount = readcount + 1 ";
sql+=" where num = ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
pstmt.executeUpdate(); //To execute SQL
sql="select * from upload where num = ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
rs = pstmt.executeQuery();
if(rs.next()) {
board.setNum(rs.getInt("num"));
board.setWriter(rs.getString("writer"));
board.setEmail(rs.getString("email"));
board.setSubject(rs.getString("subject"));
board.setSubject(rs.getString("subject"));
board.setPasswd(rs.getString("passwd"));
board.setReg_date(rs.getTimestamp("reg_date"));
board.setReadcount(rs.getInt("readcount"));
board.setContent(rs.getString("content"));
board.setIp(rs.getString("ip"));
board.setUpload(rs.getString("upload"));
}
}catch(Exception e) {
}finally {
if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
if(con != null) try { con.close();} catch(Exception e) {}
if(rs != null) try { rs.close();} catch(Exception e) {}
}
return board;
}
// Update : to get detail
public BoardDataBean getContent(int num) {
BoardDataBean board = new BoardDataBean();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
String sql="update upload set readcount = readcount + 1 ";
sql+=" where num = ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
pstmt.executeUpdate(); //To execute SQL
sql="select * from upload where num = ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
rs = pstmt.executeQuery();
if(rs.next()) {
board.setNum(rs.getInt("num"));
board.setWriter(rs.getString("writer"));
board.setEmail(rs.getString("email"));
board.setSubject(rs.getString("subject"));
board.setSubject(rs.getString("subject"));
board.setPasswd(rs.getString("passwd"));
board.setReg_date(rs.getTimestamp("reg_date"));
board.setReadcount(rs.getInt("readcount"));
board.setContent(rs.getString("content"));
board.setIp(rs.getString("ip"));
board.setUpload(rs.getString("upload"));
}
}catch(Exception e) {
}finally {
if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
if(con != null) try { con.close();} catch(Exception e) {}
if(rs != null) try { rs.close();} catch(Exception e) {}
}
return board;
}
// Update method
public int update(BoardDataBean board) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql="update upload set writer=?,email=?,subject=?,";
sql+="content=?,ip=?,upload=? where num=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, board.getWriter());
pstmt.setString(2, board.getEmail());
pstmt.setString(3, board.getSubject());
pstmt.setString(4, board.getContent());
pstmt.setString(5, board.getIp());
pstmt.setString(6, board.getUpload());
pstmt.setInt(7, board.getNum());
result = pstmt.executeUpdate(); // To execute SQL
}catch(Exception e) {
}finally {
if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
if(con != null) try { con.close();} catch(Exception e) {}
}
return result;
}
// Delete the post & attached file
public int delete(BoardDataBean board, String path) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql="delete from upload where num=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, board.getNum());
result = pstmt.executeUpdate(); // To execute SQL
if(board.getUpload() != null) { // if there is any file attached
File file = new File(path);
// To read all files in the upload directory
File[] f = file.listFiles();
for(int i=0; i<f.length; i++) {
if(f[i].getName().equals(board.getUpload())) {
f[i].delete(); // To delete the attached file
}
}
}
}catch(Exception e) {
}finally {
if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
if(con != null) try { con.close();} catch(Exception e) {}
}
return result;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<script>
location.href = "upload1/list.jsp";
</script>
</body>
</html>
'Codes & Projects' 카테고리의 다른 글
JSP / EL / JSTL / HTML / JavaScript / Oracle) Model2 Bulletin Board(1) (0) | 2022.09.26 |
---|---|
JSP/ HTML/ java / Oracle /ajax ) Customer Management System with Model 2 (1) (0) | 2022.09.22 |
JSP) Model2 - Java Servlet 2 (0) | 2022.09.16 |
Java) Temperature Converter (0) | 2022.09.10 |
HTML / JSP) Multiplication table - <select> (0) | 2022.09.04 |