Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. package homework;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. import javax.servlet.ServletConfig;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.annotation.WebServlet;
  16. import javax.servlet.http.HttpServlet;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19.  
  20. import homework.ToDoList;
  21.  
  22. @WebServlet("/ToDo")
  23. public class ToDo extends HttpServlet {
  24. private static final long serialVersionUID = 1L;
  25.  
  26. public ToDo() {
  27. super();
  28. }
  29.  
  30. public void init(ServletConfig config) throws ServletException {
  31. super.init(config);
  32.  
  33. try {
  34. Class.forName("com.mysql.jdbc.Driver");
  35. } catch (ClassNotFoundException e) {
  36. throw new ServletException(e);
  37. }
  38. }
  39.  
  40. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  41. throws ServletException, IOException {
  42. List<ToDoList> entries = new ArrayList<ToDoList>();
  43. Connection c = null;
  44. try {
  45. String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu49";
  46. String username = "cs3220stu49";
  47. String password = "#CKr.Npn";
  48.  
  49. c = DriverManager.getConnection(url, username, password);
  50. Statement stmt = c.createStatement();
  51. ResultSet rs = stmt.executeQuery("select * from todo where archive=0");
  52.  
  53. while (rs.next()) {
  54. ToDoList entry = new ToDoList(rs.getInt("id"), rs.getString("title"), rs.getString("state"),
  55. rs.getInt("archive"));
  56. entries.add(entry);
  57. }
  58.  
  59. String counter = "SELECT * from todo WHERE archive=0";
  60. PreparedStatement pstmt = c.prepareStatement(counter);
  61. int count = 0;
  62. ResultSet r2 = pstmt.executeQuery(counter);
  63. while (r2.next()) {
  64. count++;
  65. System.out.println(count);
  66. }
  67.  
  68. String tot = "select * from todo where state='notdone'";
  69. ResultSet r = pstmt.executeQuery(tot);
  70. int total = 0;
  71. while (r.next()) {
  72. total++;
  73. System.out.println(total);
  74. }
  75.  
  76. } catch (SQLException e) {
  77. throw new ServletException(e);
  78. } finally {
  79. try {
  80. if (c != null)
  81. c.close();
  82. } catch (SQLException e) {
  83. throw new ServletException(e);
  84. }
  85. }
  86. request.setAttribute("entries", entries);
  87. request.setAttribute("count", count);
  88. //request.setAttribute("total", total);
  89. request.getRequestDispatcher("/WEB-INF/ToDo/ToDo.jsp").forward(request, response);
  90.  
  91. }
  92.  
  93. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  94. throws ServletException, IOException {
  95. String title = request.getParameter("newTask");
  96. String state = "notdone";
  97. int archive = 0;
  98.  
  99. Connection c = null;
  100. try {
  101. String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu49";
  102. String username = "cs3220stu49";
  103. String password = "#CKr.Npn";
  104.  
  105. String sql = "INSERT into todo (title, state, archive) values (?, ?, ?)";
  106.  
  107. c = DriverManager.getConnection(url, username, password);
  108. PreparedStatement pstmt = c.prepareStatement(sql);
  109. pstmt.setString(1, title);
  110. pstmt.setString(2, state);
  111. pstmt.setInt(3, archive);
  112. pstmt.executeUpdate();
  113.  
  114. } catch (SQLException e) {
  115. throw new ServletException(e);
  116. } finally {
  117. try {
  118. if (c != null)
  119. c.close();
  120. } catch (SQLException e) {
  121. throw new ServletException(e);
  122. }
  123. }
  124.  
  125. doGet(request, response);
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement