package web;
import java.io.*;
import java.sql.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;
public class BBSPostServlet extends HttpServlet {
	public void doPost(HttpServletRequest request,
			HttpServletResponse response)
			throws IOException, ServletException {
		HttpSession session = request.getSession();
		String id = (String) session.getAttribute("LOGIN_ID");
		if (id == null)
			throw new ServletException("게시글을 등록하려면 먼저 로그인을 해야 합니다.");
			String title = request.getParameter("TITLE");
			String content = request.getParameter("CONTENT");
			if (title == null || content == null)
			throw new ServletException("데이터를 입력하십시오.");
			int seqNo = 1;
			GregorianCalendar now = new GregorianCalendar();
			Connection conn = null;
			Statement stmt = null;
			try {
		    	
		        Class.forName("com.mysql.jdbc.Driver");
		        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/webdb", "root", "1234");
		        if (conn == null)
		        	throw new ServletException("데이터베이스에 연결할 수 없습니다.");
		        stmt = conn.createStatement();
		        ResultSet rs = stmt.executeQuery("select max(seqno) as max_seqno from bbs");
		        if (rs.next())
		        	seqNo = rs.getInt("max_seqno") + 1;
		        String command = String.format("insert into bbs (seqNo, title, content, writer, wdate, wtime) values (%d, '%s', '%s', '%s', '%TF', '%TT');",seqNo, title, content, id, now, now);
		        int rowNum = stmt.executeUpdate(command);
		        if (rowNum < 1)
		        	throw new ServletException("데이터를 DB에 입력할 수 없습니다.");
			}
		        catch (ClassNotFoundException cnfe) {
		        	throw new ServletException(cnfe);
		        }
		        catch (SQLException se) {
		        	throw new ServletException(se);
	}
		        finally {
		        	try {
		        	stmt.close();
		        	}
		        	catch (Exception ignored) {
		        	}
		        	try {
		        	conn.close();
		        	}
		        	catch (Exception ignored) {
		        	}
		        	}
		        	response.sendRedirect("bbs-list");
		        	 }
		        	 }


