---
# System prepended metadata

title: Untitled

---

```java=
1. **#insert.java**
package chapter15;

import bean.Product;
import dao.ProductDAO;
import tool.Page;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.util.List;
@WebServlet(urlPatterns={"/chapter15/insert"})
public class Insert extends HttpServlet {

	public void doPost (
		HttpServletRequest request, HttpServletResponse response
	) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		Page.header(out);
		try {
			String name=request.getParameter("name");
			int price=Integer.parseInt(request.getParameter("price"));
			//新しい商品かどうか判定
			ProductDAO d=new ProductDAO();
			List<Product> l=d.searchPerfectly(name);
			Boolean isNew = (l.size() < 1);

			if(isNew){
				//新しい商品の場合
				Product p=new Product();
				p.setName(name);
				p.setPrice(price);
	
				ProductDAO dao=new ProductDAO();
				int line=dao.insert(p);
	
				if (line>0) {
					out.println("追加に成功しました。");
				}	
			}else{
				//既存の商品の場合

				//既存の商品情報（複数ある場合は１つめだけ）
				Product p_ = l.get(0);
				int id = p_.getId();

				Product p= new Product();
				p.setId(id);
				p.setName(name);
				p.setPrice(price);

				ProductDAO dao = new ProductDAO();
				int line=dao.update(p);

				if (line>0) {
					out.println("更新に成功しました。");
				}
			

			}
		} catch (Exception e) {
			e.printStackTrace(out);
		}
		Page.footer(out);
	}
}




**#ProductDAO**
package dao;

import bean.Product;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;

public class ProductDAO extends DAO {
	/*public List<Product> search(String keyword) throws Exception {
		List<Product> list=new ArrayList<Product>();

		Connection con=getConnection();

		PreparedStatement st=con.prepareStatement(
			"select * from product where name like ?");
		st.setString(1, "%"+keyword+"%");
		ResultSet rs=st.executeQuery();

		while (rs.next()) {
			Product p=new Product();
			p.setId(rs.getInt("id"));
			p.setName(rs.getString("name"));
			p.setPrice(rs.getInt("price"));
			list.add(p);
		}

		st.close();
		con.close();

		return list;
	}*/

	public List<Product> searchPerfectly(String keyword) throws Exception {
		List<Product> list=new ArrayList<Product>();

		Connection con=getConnection();

		PreparedStatement st=con.prepareStatement(
			"select * from product where name like ?");
		st.setString(1,keyword);
		ResultSet rs=st.executeQuery();

		while (rs.next()) {
			Product p=new Product();
			p.setId(rs.getInt("id"));
			p.setName(rs.getString("name"));
			p.setPrice(rs.getInt("price"));
			list.add(p);
		}

		st.close();
		con.close();

		return list;
	}

	public int update(Product product) throws Exception {
		Connection con=getConnection();

		PreparedStatement st=con.prepareStatement(
			"update product set price=? where id=?");
		st.setInt(1, product.getPrice());
		st.setInt(2, product.getId());
		int line=st.executeUpdate();

		st.close();
		con.close();
		return line;
	}
	public int insert(Product product) throws Exception {
		Connection con=getConnection();

		PreparedStatement st=con.prepareStatement(
			"insert into product values(null, ?, ?)");
		st.setString(1, product.getName());
		st.setInt(2, product.getPrice());
		int line=st.executeUpdate();

		st.close();
		con.close();
		return line;
	}
}


