본문 바로가기
JSP

230202_DB

by 경 훈 2023. 2. 2.

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
	<form action="save.jsp" method="post" enctype="multipart/form-data">
		<table border="1" align="center">
			<caption>상품명과 이미지 입력</caption>
			<tr>
				<th>상품명</th>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<th>이미지</th>
				<td><input type="file" name="poster"></td>
			</tr>
			<tr>
				<td colspan="3" align="center">
					<input type="submit" value="전송">
					<input type="reset" value="취소">
					<input type="button" value="홈페이지" onclick="window.location='index.jsp'">
				</td>
			</tr>
		</table>
	</form>
</section>
<jsp:include page="footer.jsp"/>
</html>

 

<%@page import="day13_db_image.DTO"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="day13_db_image.DAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	String sd = "C://12월국비//JSP//day13_db_image//src//main//webapp//image";
	int filesize = 1024*1024*10;
	String encType="UTF-8";
	MultipartRequest multi = new MultipartRequest(
			request,sd,filesize,encType);
	String name = multi.getParameter("name"); //값을 받아오는
	String poster = multi.getFilesystemName("poster"); //파일명과 확장자
	DTO dto = new DTO(name,poster);
	DAO dao = new DAO();
	dao.insert(dto);
	response.sendRedirect("index.jsp");
	%>
</body>
</html>

 

<%@page import="java.sql.ResultSet"%>
<%@page import="day13_db_image.DAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<!DOCTYPE html>
<html>
<head>
<style>
button {
	border: none;
	background-color: #ffffff;
	cursor: pointer;
}
</style>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
	<form action="search1.jsp">
		<table border="1" align="center" width="400px">
			<caption>검색</caption>
			<tr>
				<td>
					<select name="sname">
						<option value="name">상품명
						<option value="poster">상품이미지
					</select>
				</td>
				<td>
					<input type="text" name="svalue">
				</td>
			</tr>	
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="검색">
					<input type="button" value="홈으로" onclick="window.location='index.jsp'">
				</td>
			</tr>	
		</table>
	</form>
	<%
	DAO dao = new DAO();
	ResultSet rs = dao.out();
	%>
	<table border="1" align="center" width="400px" >
		<tr>
			<th>상품명</th><th>상품이미지</th><th>삭제</th><th>수정</th>
		</tr>
	<%
	while(rs.next()) {
		String name = rs.getString(1);
		String poster = rs.getString(2);
	%>	
		<tr>
			<td><%=name%></td>
			<td><img src = "image/<%=poster%>" width="200px" height="150px"></td>
			<td><a href = "delete.jsp?name=<%=name%>"><button>&#10060;</button></a></td>
			<td><a href = "modify1.jsp?name=<%=name%>"><button>&#9989;</button></a></td>
		</tr>
	<%
	}
	%>
	</table>
</section>
<jsp:include page="footer.jsp"/>
</html>

 

<%@page import="day13_db_image.DAO"%>
<%@ 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>
	<%!
	String dname;
	%>
	<%
	request.setCharacterEncoding("utf-8");
	dname=request.getParameter("name");
	DAO dao = new DAO();
	dao.delete(dname);
	response.sendRedirect("index.jsp");
	%>
</body>
</html>

 

<%@page import="day13_db_image.DAO"%>
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
	<%!
	String mname;
	%>
	<%
	request.setCharacterEncoding("utf-8");
	mname=request.getParameter("name");
	DAO dao = new DAO();
	ResultSet rs = dao.modify1(mname);
	if(rs.next()) {
		String name = rs.getString(1);
		String poster = rs.getString(2);
	%>
	<form action="modify2.jsp" method="post" enctype="multipart/form-data">
		<table border="1" align="center">
			<tr>
				<th>상품명</th>
				<td><input type="text" name="name" value="<%=name%>"></td>
			</tr>
			<tr>
				<th>상품이미지</th>
				<td colspan="2">
					<img src = "image/<%=poster%>" width="80px" height="60px">
					<input type="file" name="poster">
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="수정">
					<input type="reset" value="취소">
				</td>
			</tr>
	<%
	}
	%>
		</table>
	</form>
</section>
<jsp:include page="footer.jsp"/>
</html>

 

<%@page import="day13_db_image.DAO"%>
<%@page import="day13_db_image.DTO"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ 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>
	<%
	String sd = "C://12월국비//JSP//day13_db_image//src//main//webapp//image";
	int filesize = 1024*1024*10;
	String encType="UTF-8";
	MultipartRequest multi = new MultipartRequest(
			request,sd,filesize,encType);
	String name = multi.getParameter("name"); //값을 받아오는
	String poster = multi.getFilesystemName("poster"); //파일명과 확장자
	DTO dto = new DTO(name,poster);
	DAO dao = new DAO();
	dao.modify2(dto);
	response.sendRedirect("index.jsp");
	%>
</body>
</html>

 

<%@page import="java.sql.ResultSet"%>
<%@page import="day13_db_image.DAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
	<%!
	String sname,svalue;
	%>
	<%
	request.setCharacterEncoding("utf-8");
	sname=request.getParameter("sname");
	svalue=request.getParameter("svalue");
	DAO dao = new DAO();
	ResultSet rs = dao.search(sname, svalue);
	%>
	<table border="1" align="center" width="400px" >
		<tr>
			<th>상품명</th><th>상품이미지</th>
		</tr>
	<%
	while(rs.next()) {
		String name = rs.getString(1);
		String poster = rs.getString(2);
	%>	
		<tr>
			<td><%=name%></td>
			<td><img src = "image/<%=poster%>" width="200px" height="150px"></td>
		</tr>
	<%
	}
	%>
	</table>
</section>
<jsp:include page="footer.jsp"/>
</html>

 

package day13_db_image;

public class DTO {
	String name,poster;

	public DTO() {
	}
	public DTO(String name, String poster) {
		super();
		this.name = name;
		this.poster = poster;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPoster() {
		return poster;
	}
	public void setPoster(String poster) {
		this.poster = poster;
	}
}

 

package day13_db_image;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DAO {
	Connection con;
	PreparedStatement psmt;
	ResultSet rs;
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@localhost:1521:xe";
	public DAO() throws ClassNotFoundException {
		Class.forName(driver);
		try {
			con = DriverManager.getConnection(url, "ezen", "12345");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void insert(DTO dto){
		String name = dto.getName();
		String poster = dto.getPoster();
		String sql = "insert into movie values (?,?)";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, name);
			psmt.setString(2, poster);
			psmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public ResultSet out() {
		String sql = "select * from movie";
		try {
			psmt = con.prepareStatement(sql);
			rs = psmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	public void delete(String dname) {
		String sql ="delete from movie where name=?";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, dname);
			psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public ResultSet modify1(String mname) {
		String sql ="select * from movie where name=?";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, mname);
			rs = psmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	public void modify2(DTO dto) {
		String name = dto.getName();
		String poster = dto.getPoster();
		String sql = "update movie set poster=? where name=?";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, poster);
			psmt.setString(2, name);
			psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public ResultSet search(String sname,String svalue) {
		String sql = "select * from movie where "+sname+" like ? ";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, '%'+svalue+'%');
			rs = psmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
}

'JSP' 카테고리의 다른 글

230206_기본  (0) 2023.02.06
230203_DB  (0) 2023.02.03
230201_DB  (0) 2023.02.01
230131_DB  (0) 2023.01.31
230130_DB  (0) 2023.01.30

댓글