ㄱㄷㅎ
50
2021-06-10 22:24:50 작성 2021-06-10 23:09:46 수정됨
3
111

서블릿 db


삭제버튼을 누르면 db에 있는 줄이 삭제되어야 하는데 400에러가뜨네요 뭐가잘못된거죠??
밑은 전체 서블릿이고
package exam;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MenuServlet
 */
@WebServlet("/menu")
public class MenuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doHandle(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doHandle(request, response);
	}

	protected void doHandle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		MenuDAO dao = new MenuDAO();
		PrintWriter out = response.getWriter();
		String command = request.getParameter("command");
		if ( command != null && command.equals("addMenu")) {
			String _name = request.getParameter("name");
			Integer _price = Integer.parseInt(request.getParameter("price"));
			MenuVO vo = new MenuVO();
			vo.setname(_name);
			vo.setprice(_price);
			dao.addMenu(vo);
		}
		else if ( command != null && command.equals("delMenu")) {
			String name = request.getParameter("name");
			dao.delMenu(name);
		}
	
		
		
		List<MenuVO> list = dao.listMenu();
		
		out.print("<html><body>");
		out.print("<table border=1>");
		out.print("<tr align=center width=50>");
		out.print("<td width=100>메뉴이름</td>");
		out.print("<td width=100>가격</td>");
		out.print("<td width=100>등록일자</td>");
		out.print("<td width=100>삭제</td>");
		out.print("</tr>");
		
		for ( int i = 0; i < list.size(); i++ )
		{
			MenuVO menuVO = (MenuVO)list.get(i);
			String name = menuVO.getname();
			Integer price = menuVO.getprice();
			Date regDate = menuVO.getregDate();
			
			out.print("<html><body><table border=1>");
			out.print("<tr align=center>");
			out.print("<td width=100>"+name+"</td>");
			out.print("<td width=100>"+price+"</td>");
			out.print("<td width=100>"+regDate+"</td>");
			out.print("<td width=100><a href='/exam/menu?command=delMenu&name="+name+"'>삭제</a></td>");
			out.print("</tr>");
		}
		
		
		out.print("</table>");
		out.print("<a href='/exam/menuform.html'>메뉴추가</a>");
		out.print("</body></html>");
		
	}

}
밑은 DAO 클래스입니다.

package exam;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import exam.MenuVO;

public class MenuDAO {
	private Connection con;
	private PreparedStatement pstmt;
	private DataSource dataFactory;
	
	public MenuDAO() 
	{
		try 
		{
			Context cont = new InitialContext();
			Context envContext = (Context) cont.lookup("java:/comp/env");
			dataFactory = (DataSource) envContext.lookup("jdbc/oracle");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	public List<MenuVO> listMenu()
	{
		List<MenuVO> list = new ArrayList<MenuVO>();
		try
		{
			
			connDB();
			
			
			String query = "select * from t_menu";
			System.out.println("preparedstatement: " + query);
			pstmt = con.prepareStatement(query);
			ResultSet rs = pstmt. executeQuery();
			while (rs.next())
			{
				String name = rs.getString("name");
				Integer price = rs.getInt("price");
				Date regDate = rs.getDate("regDate");
		
				MenuVO vo = new MenuVO();
				
				vo.setname(name);
				vo.setprice(price);
				vo.setregDate(regDate);
				
				list.add(vo);
			}
			
			rs.close();		// 결과셋 -> 운영체제 반환
			pstmt.close();	// 준비질의객체 -> 운영체제 반환
			con.close();	// 데이터베이스 접속 끊고 운영체제한테 반환
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return list;
	}
	
	public void addMenu(MenuVO menuVO) {
		try {
			con = dataFactory.getConnection();
			String name = menuVO.getname();
			int price = menuVO.getprice();
			String query = "insert into t_menu";
			query += " (name,price) ";
			query += " values (?,?) ";
			System.out.println("prepareStatement: " + query);
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, name);
			pstmt.setInt(2, price);
			pstmt.executeUpdate();
			pstmt.close();
		}	catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void delMenu(String name)
	{
		try {
			con = dataFactory.getConnection();
			
			String query = "delete from t_menu" + "where name=?";
			System.out.println("preparestatment:"+ query);
			pstmt = con.prepareStatement(query);
			pstmt.setString(1, name);
			pstmt.executeUpdate();
			pstmt.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void connDB()
	{
		try
		{	
			String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
			String dbID = "scott";
			String dbPW = "tiger";
			
			Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("Oracle 드라이버 로딩 성공");
			
			con = DriverManager.getConnection(dbURL, dbID, dbPW);
			System.out.println("Connection 생성 성공");
			pstmt = (PreparedStatement) con.createStatement();
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

delMenu가  삭제버튼 눌렀을때 실행되야 하는데 400에러가 뜹니다.




그리고 박스에 텍스트 입력하고 버튼 누르면 db에 데이터가 넣어져야하는데 넣어지지가 않습니다. 오류가 뜨는건 아닌데 db에 추가가 안됩니다.




0
  • 답변 3

  • youngyoung
    2k
    2021-06-10 22:50:07

    query 잘 살펴보세요 스페이스만 넣어주면 에러 없어질거 같은데 ㅎㅎ

  • ㄱㄷㅎ
    50
    2021-06-10 22:55:04

    어디말씀하시는건가요??.. 스페이스 제대로 넣은거같은데..

  • 초무쿤
    6k
    2021-06-11 00:12:31 작성 2021-06-11 00:12:45 수정됨

    400이면 bad request인데요.. 들어가지도 못한거 같은데요.

    break 걸어보세유.

  • 로그인을 하시면 답변을 등록할 수 있습니다.