package cs3220.controller; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/AddComment") public class AddComment extends HttpServlet { private static final long serialVersionUID = 1L; public AddComment() { super(); } protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { request.getRequestDispatcher( "/WEB-INF/AddComment.jsp" ) .forward( request, response ); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { Connection c = null; try { String url = "jdbc:mysql://localhost/cs3220stu31"; String username = "cs3220stu31"; String password = "abcd"; String sql = "insert into guestbook (name, message, date) values (?, ?, now())"; c = DriverManager.getConnection( url, username, password ); PreparedStatement pstmt = c.prepareStatement( sql ); pstmt.setString( 1, request.getParameter( "name" ) ); pstmt.setString( 2, request.getParameter( "message" ) ); pstmt.executeUpdate(); c.close(); } catch( SQLException e ) { throw new ServletException( e ); } finally { try { if( c != null ) c.close(); } catch( SQLException e ) { throw new ServletException( e ); } } response.sendRedirect( "GuestBook" ); } }