package homework; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import homework.ToDoList; @WebServlet("/ToDo") public class ToDo extends HttpServlet { private static final long serialVersionUID = 1L; public ToDo() { super(); } public void init(ServletConfig config) throws ServletException { super.init(config); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new ServletException(e); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List entries = new ArrayList(); Connection c = null; try { String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu49"; String username = "cs3220stu49"; String password = "#CKr.Npn"; c = DriverManager.getConnection(url, username, password); Statement stmt = c.createStatement(); ResultSet rs = stmt.executeQuery("select * from todo where archive=0"); while (rs.next()) { ToDoList entry = new ToDoList(rs.getInt("id"), rs.getString("title"), rs.getString("state"), rs.getInt("archive")); entries.add(entry); } String counter = "SELECT * from todo WHERE archive=0"; PreparedStatement pstmt = c.prepareStatement(counter); int count = 0; ResultSet r2 = pstmt.executeQuery(counter); while (r2.next()) { count++; System.out.println(count); } String tot = "select * from todo where state='notdone'"; ResultSet r = pstmt.executeQuery(tot); int total = 0; while (r.next()) { total++; System.out.println(total); } } catch (SQLException e) { throw new ServletException(e); } finally { try { if (c != null) c.close(); } catch (SQLException e) { throw new ServletException(e); } } request.setAttribute("entries", entries); request.setAttribute("count", count); //request.setAttribute("total", total); request.getRequestDispatcher("/WEB-INF/ToDo/ToDo.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String title = request.getParameter("newTask"); String state = "notdone"; int archive = 0; Connection c = null; try { String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu49"; String username = "cs3220stu49"; String password = "#CKr.Npn"; String sql = "INSERT into todo (title, state, archive) values (?, ?, ?)"; c = DriverManager.getConnection(url, username, password); PreparedStatement pstmt = c.prepareStatement(sql); pstmt.setString(1, title); pstmt.setString(2, state); pstmt.setInt(3, archive); pstmt.executeUpdate(); } catch (SQLException e) { throw new ServletException(e); } finally { try { if (c != null) c.close(); } catch (SQLException e) { throw new ServletException(e); } } doGet(request, response); } }