기록해! 정리해!

JSP - 자료실 본문

JAVA/JSP

JSP - 자료실

zsuling 2022. 8. 8. 17:27

 

º mySQL.sql

create table T0808(
idx int not null primary key auto_increment,
name varchar(20) not null,
file  varchar(30) not null
);

select* from T0808;

 

º form_ok.jsp  

-- new MultipartRequest(request, realFolder, maxSize, encType, new DefaultFileRenamePolicy());

// out,print 는 웹에 띄우기 

   System.out.println은 콘솔에 띄우기

 

 

º form_ok.jsp  

-- 파일저장

 저장되는 공간

: C:\Users\jisoo\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jsp0717

 

--데이터베이스 연동

<%
//데이터베이스 저장하기
 String url ="jdbc:mysql://localhost:3306/ppk400";
 String user="root";
 String password="autoset";
 
 Class.forName("com.mysql.jdbc.Driver");
 Connection con = DriverManager.getConnection(url,user,password);

 String SQL = "insert into T0808(name,file) values (?,?)";
 PreparedStatement pstmt = con.prepareStatement(SQL);
 pstmt.setString(1,name);
 pstmt.setString(2,file);
 int k = pstmt.executeUpdate();
 if(k==1){
	 System.out.println("저장성공");
 }else{
	 out.println("저장실패");
 }
%>

결과확인1
결과확인2
결과확인3

 

º list.jsp  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	String path = request.getContextPath(); //절대경로

	 String url ="jdbc:mysql://localhost:3306/ppk400";
	 String user="root";
	 String password="autoset";
	 
	 Class.forName("com.mysql.jdbc.Driver");
	 Connection con = DriverManager.getConnection(url,user,password);

	 String SQL = "select idx, name, file from T0808";
	 PreparedStatement pstmt = con.prepareStatement(SQL);
	 ResultSet rs = pstmt.executeQuery();
	%>
	
	<div align=center> 
	<h2>목 록 보 기 (<a href=form.jsp>저장하기</a>)</h2>
	<table border=1 width=400>
		<tr>
			<td>번호</td>
			<td>이름</td>
			<td>파일명</td>
			<td>이미지</td>
		</tr>
		<%
		while (rs.next()) {
		%>
		<tr>
			<td><%=rs.getString("idx")%></td>
			<td> <a href=edit.jsp?idx=<%=rs.getString("idx")%>>
			<%=rs.getString("name")%></a></td>
			<td><%=rs.getString("file")%></td>
			<td align=center><img src='<%=path%>/files/<%=rs.getString("file")%>'
				width=50 height=50></td>
		</tr>

		<%
		}
		%>
	</table>
	</div>
	<%
	rs.close();
	pstmt.close();
	con.close();
	%>
</body>

</html>

-form_ok.jsp

 if(k==1){
     response.sendRedirect("list.jsp");
 }else{
	 out.println("저장실패");
 }

º edit.jsp  

<%@ page language="java" 
         contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%> 
<%
String path=request.getContextPath();
String idx = request.getParameter("idx");
// 데이터베이스 저장하기 
String url ="jdbc:mysql://localhost:3306/ppk400";
String user="root";
String password="autoset"; 

Class.forName("com.mysql.jdbc.Driver");
Connection  con = DriverManager.getConnection(url, user, password );

String SQL ="select idx,  name, file from T0808 where idx=? ";
PreparedStatement pstmt=con.prepareStatement(SQL);
pstmt.setString(1, idx);
ResultSet rs= pstmt.executeQuery();
rs.next();
idx =rs.getString("idx");
String name =rs.getString("name");
String file =rs.getString("file");
%>         
         
         
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<div  align="center">
<br> <h2> 자료 수정하기  </h2>
<img src="<%=path%>/files/<%=file %>" width=300  height=200>
<form method=post  enctype="multipart/form-data"
      action=update_ok.jsp >
      
      
<table  border=1>
<tr><td>이름</td>
    <td> 
    <input  type=hidden  name=idx  value="<%=idx%>">
    <input  type=text  name=name value="<%=name%>"></td>
</tr>
    
<tr><td>이미지</td> 
    <td><input  type=file  name=file></td>
</tr>
<tr>
    <td colspan=2 align="center" >
    <input  type=submit value="수정하기" >
    <input  type=button onClick="list()" value="목록보기" >
    </td>
</tr>
</table>
</form>
</div>
</body>
</html>

º update_ok.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ page import="com.oreilly.servlet.MultipartRequest" %> 
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %> 
<%@ page import="java.util.*"%>    
<%@ page import="java.io.*"%> 
<%@ page import="java.sql.*"%> 

<% 

// 데이터베이스 저장하기 
	 String url ="jdbc:mysql://localhost:3306/ppk400";
  String user="root";
  String password="autoset"; 

  Class.forName("com.mysql.jdbc.Driver");
  Connection  con = DriverManager.getConnection(url, user, password );
    
  // 이클립스 와 배포시 저장되는 위치 잡기 
  String files = "/files/";
  ServletContext context = getServletContext();
  String realFolder=context.getRealPath(files);
  
  int maxSize = 5 * 1024 * 1024 ;  // 5MB 
  String encType = "UTF-8";
  
  MultipartRequest multi =null;
   multi = new MultipartRequest(request, realFolder, maxSize,
		                encType, new DefaultFileRenamePolicy());
  
   String idx = multi.getParameter("idx");
   
   String SQL ="select idx, name, file from T0808 where idx=? ";
   PreparedStatement pstmt=con.prepareStatement(SQL);
   pstmt.setString(1, idx);
   ResultSet rs= pstmt.executeQuery();
   rs.next();

   String  rs_file =rs.getString("file");
   String  DeletefileName = realFolder + rs_file ; // 경로 + 파일명
   
   File f= new File(DeletefileName); // 파일 객체 생성  
   
   if (f.exists()){ // 파일이 존재하면 ...
	   f.delete();  //  파일 삭제
   }
   
   String name = multi.getParameter("name");
   String file=multi.getFilesystemName("file");


   String UPDATE_SQL ="update T0808 set name=?, file=? where idx=?";
   pstmt=con.prepareStatement(UPDATE_SQL);
   pstmt.setString(1, name);
   pstmt.setString(2, file);
   pstmt.setString(3, idx);
   int k = pstmt.executeUpdate();
   if (k==1){
	   response.sendRedirect("list.jsp");
	   System.out.println("저장성공");
   }else{
	   out.print("저장실패");  
   }
%>

원래 고양이 이미지가 삭제되고 수정된 이미지가 저장된 것을 확인 할 수 있다

 

º edit.jsp 

( 수정하기 + 삭제하기 스크립트)

 

<%@ page language="java" 
         contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%> 
<%
String path=request.getContextPath();
String idx = request.getParameter("idx");
// 데이터베이스 저장하기 
String url ="jdbc:mysql://localhost:3306/ppk400";
String user="root";
String password="autoset"; 

Class.forName("com.mysql.jdbc.Driver");
Connection  con = DriverManager.getConnection(url, user, password );

String SQL ="select idx,  name, file from T0808 where idx=? ";
PreparedStatement pstmt=con.prepareStatement(SQL);
pstmt.setString(1, idx);
ResultSet rs= pstmt.executeQuery();
rs.next();
idx =rs.getString("idx");
String name =rs.getString("name");
String file =rs.getString("file");
%>         
         
         
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script>
function listOK(){
	alert("목록보기");
	location.href="list.jsp";
}
function delOK(k){
	alert("삭제하기"+k);
	location.href="delete.jsp?idx+"+k;
}
</script>
<body>

<div  align="center">
<br> <h2> 자료 수정하기  </h2>
<img src="<%=path%>/files/<%=file %>" width=300  height=200>
<form method=post  enctype="multipart/form-data"
      action=update_ok.jsp >
      
      
<table  border=1>
<tr><td>이름</td>
    <td> 
    <input  type=hidden  name=idx  value="<%=idx%>">
    <input  type=text  name=name value="<%=name%>"></td>
</tr>
    
<tr><td>이미지</td> 
    <td><input  type=file  name=file></td>
</tr>
<tr>
    <td colspan=2 align="center" >
    <input  type=submit value="수정하기" >
    <input  type=button onClick="listOK()" value="목록보기" >
    <input  type=button onClick="delOK(<%=idx %>)" value="삭제하기" >
    </td>
</tr>
</table>
</form>
</div>
</body>
</html>

 

º delete.jsp 

 

'JAVA > JSP' 카테고리의 다른 글

JSP - 답변형 게시판(2)  (0) 2022.08.22
JSP - 답변형 게시판  (0) 2022.08.19
데이터베이스와 JSP 연동  (0) 2022.08.04
JSP 교재(3) - 액션태그 (include / forward ), MVC  (0) 2022.08.04
JSP 교재 (2)  (0) 2022.08.03
Comments