어제는 코드를 따라쳐서 만든거고 오늘은 구글링과 조금의 커닝으로... 만들어 보라고 해서 만들어봄
list.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<style>
table { width:780px;
background-color: antiquewhite;
text-align:center;}
td { background-color: white;}
th { background-color: antiquewhite;}
</style>
<%
Class.forName("org.mariadb.jdbc.Driver");
try {
Connection conn = DriverManager.getConnection(
"jdbc:mariadb://localhost:3307/jspdb", "root", "maria");
//쿼리 실행
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from board order by num desc");
{
%>
<table border="1">
<tr>
<th style="width: 10px;">번호</th>
<th style="width: 30px;">제목</th>
<th style="width: 10px;">작성자</th>
<th style="width: 10px;">등록일</th>
<th style="width: 0px; padding: 10px;"> 조회</th>
</tr>
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getInt("num") %></td>
<td style="text-align:left;">
<a href="view.jsp?num=<%=rs.getInt("num") %>">
<%=rs.getString("title") %>
</a>
</td>
<td><%=rs.getString("writer") %></td>
<td><%=rs.getString("regtime") %></td>
<td><%=rs.getInt("hits") %></td>
</tr>
<%
}
}}catch(Exception e){
e.printStackTrace();
}
%>
</table>
<br>
<input type="button" value="글쓰기" onclick="location.href='view.jsp'">
</body>
</html>
view.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
int num = Integer.parseInt(request.getParameter("num"));
String writer = "";
String title = "";
String content = "";
String url = "jdbc:mariadb://localhost:3307/jspdb";
String user = "root";
String pass = "maria";
int hits = 0;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<style>
table { width:680px;
background-color: antiquewhite;
text-align:center;}
td { background-color: white;
width:600px; text-align:left;
}
th { background-color: antiquewhite; }
.content { height:500px;}
</style>
<body>
<%
try {
Class.forName("org.mariadb.jdbc.Driver");
con = DriverManager.getConnection(url, user, pass);
stmt = con.createStatement();
rs = stmt.executeQuery("select * from board where num=" + num);
System.out.println("DB접속 확인");
if(rs.next()){
writer = rs.getString("writer");
title = rs.getString("title");
content = rs.getString("content");
hits = rs.getInt("hits");
}
//조회수
stmt.executeUpdate(
"update board set hits=hits+1 where num=" + num);
%>
<table border="1">
<tr>
<th>조회수</th>
<td><%=hits %></td>
</tr>
<tr>
<th>제목</th>
<td><%=writer %></td>
</tr>
<tr>
<th>작성자</th>
<td><%=title %></td>
</tr>
<tr>
<th>내용</th>
<td class="content" ><%=content %></td>
</tr>
</table>
<%
}catch (Exception e){
e.printStackTrace();
}
%>
<input type="button" value="목록보기" onclick="location.href='list.jsp'">
<input type="button" value="수정" onclick="location.href='write.jsp?num=<%=num%>'">
<input type="button" value="삭제" onclick="location.href='delete.jsp?num=<%=num%>'">
</body>
</html>
'프로젝트 기반 자바(JAVA) 응용 SW개발자 취업과정 > JSP 활용 연습' 카테고리의 다른 글
게시판 만들기_2 ( write.jsp / update.jsp )MariaDB (0) | 2023.06.19 |
---|---|
게시판 만들기_1_1 ( update.jsp / write.jsp )MariaDB (0) | 2023.06.16 |
게시판 만들기_1 ( list.jsp / view.jsp )MariaDB (0) | 2023.06.15 |
List 회원 클릭 시 회원 정보 조회 하기 (0) | 2023.06.13 |
MariaDB활용 테이블 추가(Insert) (0) | 2023.06.12 |