Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. <%@page import="java.sql.*" %>
  2. <%@page import="java.util.*" %>
  3. <%@page import="org.json.JSONObject" %>
  4.  
  5. <%
  6. Connection con= null;
  7. try{
  8. Class.forName("com.mysql.jdbc.Driver").newInstance();
  9. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sentimentposts?autoReconnect=true&useSSL=false","root","root");
  10.  
  11. ResultSet rs1 = null;
  12. ResultSet rs2 = null;
  13.  
  14. List opdetails = new LinkedList();
  15. JSONObject responseObj = new JSONObject();
  16.  
  17. String query1 = "select poster_gender, count(*) as gender_count from post group by poster_gender";
  18. PreparedStatement pstm1= con.prepareStatement(query1);
  19.  
  20. String query2 = "select poster_location, count(*) as location_count from post group by poster_location";
  21. PreparedStatement pstm2= con.prepareStatement(query2);
  22.  
  23. rs1 = pstm1.executeQuery();
  24. rs2 = pstm2.executeQuery();
  25.  
  26. JSONObject opObj = new JSONObject();
  27.  
  28. while (rs1.next()) {
  29. String gender = rs1.getString("poster_gender");
  30. System.out.println(gender);
  31. int count = rs1.getInt("gender_count");
  32. System.out.println(count);
  33. opObj.put("gender", gender);
  34. opObj.put("count", count);
  35. }
  36.  
  37. while(rs2.next()){
  38. String location = rs2.getString("poster_location");
  39. System.out.println(location);
  40. int count2 = rs2.getInt("location_count");
  41. System.out.println(count2);
  42. opObj.put("location", location);
  43. opObj.put("count2", count2);
  44. }
  45. opdetails.add(opObj);
  46. responseObj.put("opdetails", opdetails);
  47. out.print(responseObj.toString());
  48. }
  49. catch(Exception e){
  50. e.printStackTrace();
  51. }finally{
  52. if(con!= null){
  53. try{
  54. con.close();
  55. }catch(Exception e){
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60. %>
  61.  
  62. <!DOCTYPE html>
  63. <html>
  64. <head>
  65. <title>Opinion Chart Multi</title>
  66. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  67. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  68. <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  69.  
  70. <script type="text/javascript">
  71. google.load("visualization", "1.0", {packages:["corechart", "bar"]});
  72. google.setOnLoadCallback(drawCharts);
  73.  
  74. var queryObject="";
  75. var queryObjectLen="";
  76. $.ajax({
  77. url : 'getdata.jsp',
  78. dataType:'json',
  79. success : function(data) {
  80. queryObject = eval('(' + JSON.stringify(data) + ')');
  81. queryObjectLen = queryObject.opdetails.length;
  82. },
  83. error : function(xhr, type) {
  84. alert('server error occoured')
  85. }
  86. });
  87.  
  88. function drawCharts() {
  89.  
  90. var data = new google.visualization.DataTable();
  91. data.addColumn('string', 'gender');
  92. data.addColumn('number', 'count');
  93. for(var i=0;i<queryObjectLen;i++)
  94. {
  95. var gender = queryObject.opdetails[i].gender;
  96. var count = queryObject.opdetails[i].count;
  97. data.addRows([
  98. [gender,parseInt(count)]
  99. ]);
  100. }
  101.  
  102. var data2 = new google.visualization.DataTable();
  103. data2.addColumn('string', 'location');
  104. data2.addColumn('number', 'count2');
  105. for(var i=0;i<queryObjectLen;i++)
  106. {
  107. var location = queryObject.opdetails[i].location;
  108. var count2 = queryObject.opdetails[i].count2;
  109. data2.addRows([
  110. [location,parseInt(count2)]
  111. ]);
  112. }
  113.  
  114. var options = {
  115. title: 'Posts By Gender',
  116. };
  117.  
  118. var options2 = {
  119. title: 'Posts by Location',
  120. bar: { groupWidth: '90%' },
  121. bars: 'horizontal',
  122. };
  123. var chart1 = new google.visualization.PieChart(document.getElementById('chart1'));
  124. chart1.draw(data,options);
  125.  
  126. var chart2 = new google.visualization.Bar(document.getElementById('chart2'));
  127. chart2.draw(data2,options2);
  128. }
  129. </script>
  130. </head>
  131. <body>
  132. <table class="columns">
  133. <tr>
  134. <td><div id="chart1"></div></td>
  135. <td><div id="chart2"></div></td>
  136. </tr>
  137. </table>
  138.  
  139. </body>
  140. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement