본문 바로가기
JSP

230201_DB

by 경 훈 2023. 2. 1.

 

폼에서 form 태그 옆에 enctype="multipart/form-data" 기입

<input type="file" name="">

이미지가 저장되는 경로 설정

저장이미지 크기 설정

한글 깨짐 방지

MultipartRequest 클래스로 객체생성후 자료 불러옴

String sd = "C://JSP//경로";
	int filesize = 1024*1024*10;
	String encType="UTF-8";
	MultipartRequest multi = new MultipartRequest(
			request,sd,filesize,encType);
name = multi.getParameter("name"); //값을 받아오는
poster = multi.getFilesystemName("poster"); //파일명과 확장자

image 폴더에 저장된 파일들은 F5 키를 눌러 재인식을 시켜야함

 

<%@ 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="2" align="center">
					<input type="submit" value="전송">
					<input type="reset" value="취소">
				</td>
			</tr>
		</table>
	</form>
</section>
<jsp:include page="footer.jsp"/>
</html>

 

<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="dbcon.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
	String name,poster;
	%>
	<%
	request.setCharacterEncoding("utf-8");
	//이미지 저장위치
	String sd = "C://12월국비//JSP//day13_db_image//src//main//webapp//image";
	//파일의 크기
	int filesize = 1024*1024*10;
	//한글모드
	String encType="UTF-8";
	//request -> multirequest
	MultipartRequest multi = new MultipartRequest(
			request,sd,filesize,encType
			);
	name = multi.getParameter("name"); //값을 받아오는
	poster = multi.getFilesystemName("poster"); //파일명과 확장자
	String sql = "insert into movie values (?,?)";
	psmt = con.prepareStatement(sql);
	psmt.setString(1, name);
	psmt.setString(2, poster);
	psmt.executeUpdate();
	response.sendRedirect("index.jsp");
	%>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="top.jsp" %>
<%@include file="dbcon.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
	<%!
	
	%>
	<%
	String sql = "select * from movie";
	psmt = con.prepareStatement(sql);
	rs = psmt.executeQuery();
	%>
	<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>

<%@page import="com.ezen.DTO"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="com.ezen.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="com.ezen.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>
	<%
	DAO dao = new DAO();
	ResultSet rs = dao.out();
	%>
	<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 com.ezen;

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;
	}
}

<%@ 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">
			<tr>
				<th>상품명</th>
				<td><input type="text" name="spname"></td>
			</tr>	
			<tr>
				<th>생산자</th>
				<td><input type="text" name="pro"></td>
			</tr>	
			<tr>
				<th>생산지</th>
				<td><input type="text" name="area"></td>
			</tr>		
			<tr>
				<th>가격</th>
				<td><input type="text" name="price"></td>
			</tr>	
			<tr>
				<th>이미지</th>
				<td><input type="file" name="image"></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_shop1.DAO"%>
<%@page import="day13_shop1.DTO"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ 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_shop1//src//main//webapp//image";
	int filesize = 1024*1024*10;
	String encType= "UTF-8";
	MultipartRequest multi = new MultipartRequest(
			request,sd,filesize,encType);
	String spname = multi.getParameter("spname");
	String pro = multi.getParameter("pro");
	String area = multi.getParameter("area");
	int price = Integer.parseInt(multi.getParameter("price"));
	String image = multi.getFilesystemName("image");
	DTO dto = new DTO(spname,pro,area,price,image);
	DAO dao = new DAO();
	dao.insert(dto);
	response.sendRedirect("index.jsp");
	%>
</body>
</html>

 

<%@page import="day13_shop1.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>
<style>
button {
	border: none;
	background-color: #ffffff;
	cursor: pointer;
}
</style>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<section>
	<%
	DAO dao = new DAO();
	ResultSet rs = dao.out();
	%>
	<table border="1" align="center" width="600px">
		<tr>
			<th>상품명</th><th>생산자</th><th>생산지</th>
			<th>가격</th><th>이미지</th><th>비고</th>
		</tr>
	<%
	while(rs.next()) {
		String spname = rs.getString(1);
		String pro = rs.getString(2);
		String area = rs.getString(3);
		int price = rs.getInt(4);
		String image = rs.getString(5);
	%>
		<tr>
			<td><%=spname%></td><td><%=pro%></td><td><%=area%></td>
			<td><%=price%></td>
			<td><img src="image/<%=image%>" width="120px" height="80px"></td>
			<td>
				<a href="delete.jsp?spname=<%=spname%>"><button>&#10060;</button></a>
				<a href="update1.jsp?spname=<%=spname%>"><button>&#9989;</button></a>
			</td>
		</tr>
	<%
	}
	%>	
	</table>
</section>
<jsp:include page="footer.jsp"/>
</html>

 

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

 

<%@page import="day13_shop1.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 uspname;
	%>
	<%
	request.setCharacterEncoding("utf-8");
	uspname = request.getParameter("spname");
	DAO dao = new DAO();
	ResultSet rs = dao.update1(uspname);
	if(rs.next()) {
		String spname = rs.getString(1);
		String pro = rs.getString(2);
		String area = rs.getString(3);
		int price = rs.getInt(4);
		String image = rs.getString(5);
	%>
	
	<form action="update2.jsp" method="post" enctype="multipart/form-data">
		<table border="1" align="center">
			<tr>
				<th>상품명</th>
				<td><input type="text" name="spname" value="<%=spname%>" readonly></td>
			</tr>
			<tr>
				<th>생산자</th>
				<td><input type="text" name="pro" value="<%=pro%>"></td>
			</tr>
			<tr>
				<th>생산지</th>
				<td><input type="text" name="area" value="<%=area%>"></td>
			</tr>
			<tr>
				<th>가격</th>
				<td><input type="text" name="price" value="<%=price%>"></td>
			</tr>
			<tr>
				<th>이미지</th>
				<td colspan="2">
				<img src="image/<%=image%>" width="60px" height="45px">
				<input type="file" name="image">
				</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_shop1.DAO"%>
<%@page import="day13_shop1.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_shop1//src//main//webapp//image";
	int filesize = 1024*1024*10;
	String encType= "UTF-8";
	MultipartRequest multi = new MultipartRequest(
			request,sd,filesize,encType);
	String spname = multi.getParameter("spname");
	String pro = multi.getParameter("pro");
	String area = multi.getParameter("area");
	int price = Integer.parseInt(multi.getParameter("price"));
	String image = multi.getFilesystemName("image");
	DTO dto = new DTO(spname,pro,area,price,image);
	DAO dao = new DAO();
	dao.update2(dto);
	response.sendRedirect("index.jsp");
	%>
</body>
</html>

 

<%@ 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="search2.jsp">
		<table border="1" align="center">
			<tr>
				<th colspan="2">검색키</th>
			</tr>
			<tr>
				<td>
					<select name ="sname">
						<option value="spname">상품명
						<option value="area">생산지
					</select>
				</td>
				<td>
					<input type="text" name="svalue">
				</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_shop1.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 sname;
	String 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="800px">
		<tr>
			<th>상품명</th><th>생산자</th><th>생산지</th>
			<th>가격</th><th>이미지</th>
		</tr>
	<%
	while(rs.next()) {
		String spname = rs.getString(1);
		String pro = rs.getString(2);
		String area = rs.getString(3);
		int price = rs.getInt(4);
		String image = rs.getString(5);
	%>
		<tr>
			<td><%=spname%></td><td><%=pro%></td><td><%=area%></td>
			<td><%=price%></td>
			<td><img src="image/<%=image%>" width="120px" height="80px"></td>
		</tr>
	<%
	}
	%>	
	</table>
</section>
<jsp:include page="footer.jsp"/>
</html>

 

package day13_shop1;

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 spname = dto.getSpname();
		String pro = dto.getPro();
		String area = dto.getArea();
		int price = dto.getPrice();
		String image = dto.getImage();
		String sql ="insert into db02011 values (?,?,?,?,?)";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, spname);
			psmt.setString(2, pro);
			psmt.setString(3, area);
			psmt.setInt(4, price);
			psmt.setString(5, image);
			psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public ResultSet out() {
		String sql = "select * from db02011";
		try {
			psmt = con.prepareStatement(sql);
			rs = psmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	public void delete(String dspname) {
		String sql = "delete from db02011 where spname=?";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, dspname);
			psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public ResultSet update1(String uspname) {
		String sql = "select * from db02011 where spname=?";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, uspname);
			rs = psmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	public void update2(DTO dto) {
		String spname = dto.getSpname();
		String pro = dto.getPro();
		String area = dto.getArea();
		int price = dto.getPrice();
		String image = dto.getImage();
		String sql ="update db02011 set pro=?,area=?,price=?,image=? "
				+ "where spname=?";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, pro);
			psmt.setString(2, area);
			psmt.setInt(3, price);
			psmt.setString(4, image);
			psmt.setString(5, spname);
			psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public ResultSet search(String sname,String svalue) {
		String sql = "select * from db02011 where "+sname+" like ?";
		try {
			psmt = con.prepareStatement(sql);
			psmt.setString(1, '%'+svalue+'%');
			rs = psmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
}

'JSP' 카테고리의 다른 글

230203_DB  (0) 2023.02.03
230202_DB  (0) 2023.02.02
230131_DB  (0) 2023.01.31
230130_DB  (0) 2023.01.30
230127_DB  (0) 2023.01.27

댓글