Advertisement
Guest User

Untitled

a guest
Apr 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. // Saved as "ebookshop\WEB-INF\classes\QueryServlet.java".
  2. import java.io.*;
  3. import java.sql.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6.  
  7. public class Display extends HttpServlet { // JDK 6 and above only
  8.  
  9. // The doGet() runs once per HTTP GET request to this servlet.
  10. @Override
  11. public void doGet(HttpServletRequest request, HttpServletResponse response)
  12. throws ServletException, IOException {
  13. // Set the MIME type for the response message
  14. response.setContentType("text/html");
  15. // Get a output writer to write the response message into the network socket
  16. PrintWriter out = response.getWriter();
  17.  
  18. Connection conn = null;
  19. Statement stmt = null;
  20. try {
  21. // Step 1: Create a database "Connection" object
  22. // For MySQL
  23. Class.forName("com.mysql.jdbc.Driver"); // Needed for JDK9/Tomcat9
  24. conn = DriverManager.getConnection(
  25. "jdbc:mysql://localhost:3306/clicker?useSSL=false&serverTimezone=UTC", "myuser", "xxxx"); // <<== Check
  26.  
  27. // Step 2: Create a "Statement" object inside the "Connection"
  28. stmt = conn.createStatement();
  29.  
  30. // Print an HTML page as output of query
  31. //String[] questionNo = {"8"};
  32. String[] questionNo = request.getParameterValues("questionNo");
  33. // String[] name = request.getParameterValues("name");
  34. // String[] email = request.getParameterValues("email");
  35. if(questionNo != null){
  36. String sqlStr;
  37. String [] choices = {"a", "b", "c", "d"};
  38. int count;
  39. ResultSet rset;
  40. out.println("<html><head><title>Display</title><link rel='stylesheet' href='style.css'><link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet'></head><body class='default'><h1 class='title-head'>MENTI<span class='subscript'>v2</span></h1>");
  41. out.println("<canvas id='myChart' width='80' height='30'><p>fallback</p></canvas><script src='https://cdn.jsdelivr.net/npm/chart.js@2.8.0'></script><script>");
  42. out.println("var ctx = document.getElementById('myChart').getContext('2d');");
  43. out.println("var chart = new Chart(ctx, {type: 'bar',data: {labels: ['A', 'B', 'C', 'D'],datasets: [{");
  44.  
  45.  
  46. for(int i = 0; i<questionNo.length; i++){
  47. out.println("label: 'Question "+questionNo[i]+"',");
  48. out.println("backgroundColor: 'rgb(21, 101, 192)',borderColor: 'rgb(21, 101, 192)',");
  49. out.println("data: [");
  50. for(int j=0; j<choices.length; j++){
  51. //out.println("<p>SELECT questionNo, count(*) FROM responses WHERE questionNo= '" + questionNo[i] + "' AND choice= '"+ choices[j] +"'</p>");
  52. sqlStr = "SELECT questionNo, count(*) FROM responses WHERE questionNo= '" + questionNo[i] + "' AND choice= '"+ choices[j] +"'";
  53. rset = stmt.executeQuery(sqlStr);
  54. if(j<choices.length-1){
  55. while(rset.next()){
  56. out.println(rset.getInt("count(*)")+", ");
  57. }
  58. }else{
  59. while(rset.next()){
  60. out.println(rset.getInt("count(*)"));
  61. }
  62. }
  63. }
  64.  
  65. out.println("]}],borderWidth: 1},");
  66. out.println("options: {legend:{display: false,},label: {display: false,},scales: {yAxes: [{ticks: {beginAtZero: true},gridLines: {display: false,},}],xAxes: [{gridLines: {display: false,}}],}}});</script></body></html>");
  67.  
  68. }
  69.  
  70. }
  71.  
  72. } catch (SQLException ex) {
  73. ex.printStackTrace();
  74. } catch (ClassNotFoundException ex) {
  75. ex.printStackTrace();
  76. } finally {
  77. out.close();
  78. try {
  79. // Step 5: Close the Statement and Connection
  80. if (stmt != null) stmt.close();
  81. if (conn != null) conn.close();
  82. } catch (SQLException ex) {
  83. ex.printStackTrace();
  84. }
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement