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