emluy 개발 일기

Eclipse - Maven project 와 Oracle 연동 & 게시판 만들기 본문

Eclipse

Eclipse - Maven project 와 Oracle 연동 & 게시판 만들기

yulme 2021. 4. 8. 11:11
SMALL

 

* 게시물 작성폼이 있는 insert.jsp와 입력된 값을 oracle DB에 저장하는 insert_action.jsp 만들어보자!

 

1. maven project 만들기

* Create

2. 입력 폼을 만들자 - insert.jsp 파일

#xcvweb03/src/main/webapp/jsp/insert.jsp

<%@ 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>
<body>
<form action ="insert_action.jsp" method="post">
	<table>
		<caption>게시물 쓰기</caption>
		<tr>
			<th>제목</th>
			<td><input type="text" name="title" required="required"></td>
		</tr>
		<tr>
			<th>이름</th>
			<td><input type="text" name="name" required="required"></td>
		</tr>
		<tr>
			<th>비밀번호</th>
		<td><input type="password" name="password" required="required"></td>
		</tr>
		<tr>
		<th>내용</th>
		<td><textarea name="content" rows="5" cols="40" required="required"></textarea></td>
		</tr>	
		<tr>
		<td colspan="2" align="center"><input type="submit" value="완료"></td>
		</tr>		
	</table>
</form>

</body>
</html>

3. request 받는 jsp 파일(insert_action.jsp)에 connection, query 코드 작성

#xcvweb03/src/main/webapp/jsp/insert_action.jsp

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String title = request.getParameter("title");
	String name = request.getParameter("name");
	String password = request.getParameter("password");
	String content = request.getParameter("content");
	
	
	
	StringBuffer sql = new StringBuffer();
	sql.append("insert into t_board(no, title, name, password, content)");
	sql.append("values(seq_board.nextval, ?, ?, ?, ?) ");
	
	
	boolean result = false;
	
	try{
		Class.forName("oracle.jdbc.OracleDriver");
	} catch(Exception e){
		e.printStackTrace();
	}
	
	try(Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xepdb1","xci","xci");
			PreparedStatement ps = conn.prepareStatement(sql.toString())){
			ps.setString(1,title);
			ps.setString(2,name);
			ps.setString(3, password);
			ps.setString(4,content);
			ps.executeUpdate();
			result = true;

	} catch(Exception e){
		e.printStackTrace();
	}
	
%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= result %>
</body>
</html>

 

4. oracle연동

4-1. 아래 페이지에서 maven dependency 코드 복사

mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8/21.1.0.0

 

4-2. Eclipse 프로젝트로 돌아가서 pom.xml 파일에 <dependencies></dependencies> 안에 붙여넣기

* Read

5. 게시판 목록 만들기

5-1. list.jsp 만들기

5-2.  다음과 같이 작성

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
 <%
 	
	StringBuffer sql = new StringBuffer();
	sql.append("select no, name, title, regdate, readcount ");
	sql.append("from t_board ");
	sql.append("order by no desc ");
	
	
	boolean result = false;
	
	try{
		Class.forName("oracle.jdbc.OracleDriver");
	} catch(Exception e){
		e.printStackTrace();
	}
	
	try(Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/xepdb1","xci","xci");
			PreparedStatement ps = conn.prepareStatement(sql.toString())){
			try(ResultSet rs = ps.executeQuery()){
				
				
				

		
			

 %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1">
	<thead>
		<tr>
			<td>번호</td>
			<td>제목</td>
			<td>이름</td>
			<td>날짜</td>
			<td>조회수</td>
		</tr>
	</thead>
	<tbody>
		
		<%

				while(rs.next()){
					int no;
					String title;
					String name;
					String regdate;
					String readcount;
					
					
					no = rs.getInt("no");
					title = rs.getString("title");
					name = rs.getString("name");
					regdate = rs.getString("regdate");
					readcount = rs.getString("readcount");
					
		%>
		<tr>
		<td><%=no%></td>
		<td><%=title %></td>
		<td><%=name %></td>
		<td><%=regdate %></td>
		<td><%=readcount %></td>
		<%		
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		%>
		</tr>
	</tbody>
</table>
<%

} catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
반응형

'Eclipse' 카테고리의 다른 글

JDBC - CallableStatement  (0) 2021.03.29
JDBC, Oracle 연동하기  (0) 2021.03.26
Eclipse - Java FX 설치  (0) 2021.03.23
Comments