Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. public class vals {
  2. public vals(String date,double input, double expenses )
  3. {
  4. this.setDate(date);
  5. this.setInput(input);
  6. this.setExpenses(expenses);
  7. }
  8.  
  9. public vals() {
  10.  
  11. }
  12.  
  13. private String date;
  14. private double input;
  15. private double expenses;
  16.  
  17.  
  18. public void setDate(String code) {
  19. this.date = date;
  20. }
  21. public String getDate() {
  22. return date;
  23. }
  24.  
  25. public void setInput(Double input) {
  26. this.input = input;
  27. }
  28.  
  29. public double getInput() {
  30. return input;
  31. }
  32.  
  33. public void setExpenses(Double expenses) {
  34. this.expenses = expenses;
  35. }
  36.  
  37. public Double getExpenses() {
  38. return expenses;
  39. }
  40. }
  41.  
  42. public class FetchData {
  43.  
  44. private static Connection connection = null;
  45.  
  46. public static Connection getConnection() {
  47. if (connection != null)
  48. return connection;
  49. else {
  50. try {
  51. Class.forName("com.mysql.jdbc.Driver");
  52. connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db4.0","root","konig");
  53. } catch (ClassNotFoundException e) {
  54. e.printStackTrace();
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. return connection;
  59. }
  60.  
  61. }
  62.  
  63. public static ArrayList<vals> getAllValues() {
  64. connection = FetchData.getConnection();
  65. ArrayList<vals> ValList = new ArrayList<vals>();
  66. try {
  67. Statement statement = connection.createStatement();
  68. ResultSet rs = statement.executeQuery("select * from net");
  69.  
  70. while(rs.next()) {
  71. vals value=new vals();
  72. value.setDate(rs.getDate(1).toString());
  73. value.setInput(rs.getDouble(3));
  74. value.setExpenses(rs.getDouble(2)+rs.getDouble(4));
  75. ValList.add(value);
  76. }
  77. } catch (SQLException e) {
  78. e.printStackTrace();
  79. }
  80.  
  81. return ValList;
  82. }
  83. }
  84.  
  85. @WebServlet("/PopulateTable")
  86. public class PopulateTable extends HttpServlet {
  87. private static final long serialVersionUID = 1L;
  88.  
  89. public PopulateTable() {
  90.  
  91. }
  92.  
  93. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  94.  
  95. ArrayList<vals> values=new ArrayList<vals>();
  96. values=FetchData.getAllValues();
  97. Gson gson = new Gson();
  98. JsonElement element = gson.toJsonTree(values, new TypeToken<List<vals>>() {}.getType());
  99.  
  100. JsonArray jsonArray = element.getAsJsonArray();
  101. response.setContentType("application/json");
  102. response.getWriter().print(jsonArray);
  103.  
  104. }
  105.  
  106.  
  107. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  108.  
  109. }
  110.  
  111. }
  112.  
  113. <html>
  114. <head>
  115. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  116. <title>AJAX JsonArray</title>
  117. <link href='http://fonts.googleapis.com/css?family=Oxygen' rel='stylesheet' type='text/css'>
  118. <style type="text/css">
  119. table, td, th
  120. {
  121. border:1px solid green;
  122. font-family: 'Oxygen', sans-serif;
  123. }
  124. th
  125. {
  126. background-color:green;
  127. color:white;
  128. }
  129. body
  130. {
  131. text-align: center;
  132. }
  133. .container
  134. {
  135. margin-left: auto;
  136. margin-right: auto;
  137. width: 40em;
  138. }
  139. </style>
  140. <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  141. <script type="text/javascript">
  142. $(document).ready(function() {
  143. $("#tablediv").hide();
  144. $("#showTable").click(function(event){
  145. $.get('PopulateTable',function(responseJson) {
  146. if(responseJson!=null){
  147. $("#datatable").find("tr:gt(0)").remove();
  148. var table1 = $("#datatable");
  149. $.each(responseJson, function(key,value) {
  150. var rowNew = $("<tr><td></td><td></td><td></td></tr>");
  151. rowNew.children().eq(0).text(value['Date']);
  152. rowNew.children().eq(1).text(value['Input']);
  153. rowNew.children().eq(2).text(value['Expenses']);
  154. rowNew.appendTo(table1);
  155. });
  156. }
  157. });
  158. $("#tablediv").show();
  159. });
  160. });
  161. </script>
  162. </head>
  163. <body class="container">
  164. <h1>Table</h1>
  165. <input type="button" value="Show Table" id="showTable"/>
  166. <div id="tablediv">
  167. <table cellspacing="0" id="datatable">
  168. <tr>
  169. <th scope="col">Date</th>
  170. <th scope="col">Input</th>
  171. <th scope="col">Expenses</th>
  172.  
  173. </tr>
  174. </table>
  175. </div>
  176. </body>
  177. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement