package idc.main.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcShareBoardDBDTO;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IdcShareBoardReadFormCmd implements IdcCmd
{
	IdcDAO dao;
	
	public IdcShareBoardReadFormCmd()
	{
		dao = new IdcDAO();
	}
	
	//공용페이지 읽기 페이지
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		//int num = Integer.parseInt(request.getParameter("num"));
		int code = Integer.parseInt(request.getParameter("code"));
		IdcShareBoardDBDTO dto = idcShareBoardReadForm(code);
		request.setAttribute("shareboardDB", dto);
	}
	private IdcShareBoardDBDTO idcShareBoardReadForm(int code)
	{
		IdcShareBoardDBDTO dto = new IdcShareBoardDBDTO();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM shareboarddb WHERE code=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, code);
			rs = pstmt.executeQuery();
			
			if(rs.next())
			{
				dto.setContent(rs.getString("content"));
				dto.setFilename(rs.getString("filename"));
				dto.setId(rs.getString("id"));
				dto.setNum(rs.getInt("num"));
				dto.setTitle(rs.getString("title"));
				dto.setWrite_date(rs.getDate("write_date"));
				dto.setCode(rs.getInt("code"));
			}
		}
		catch (Exception e) 
		{
			System.out.println("idcShareBoardReadForm 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("idcShareBoardReadForm 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return dto;
	}
}
