Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class vals {
- public vals(String date,double input, double expenses )
- {
- this.setDate(date);
- this.setInput(input);
- this.setExpenses(expenses);
- }
- public vals() {
- }
- private String date;
- private double input;
- private double expenses;
- public void setDate(String code) {
- this.date = date;
- }
- public String getDate() {
- return date;
- }
- public void setInput(Double input) {
- this.input = input;
- }
- public double getInput() {
- return input;
- }
- public void setExpenses(Double expenses) {
- this.expenses = expenses;
- }
- public Double getExpenses() {
- return expenses;
- }
- }
- public class FetchData {
- private static Connection connection = null;
- public static Connection getConnection() {
- if (connection != null)
- return connection;
- else {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db4.0","root","konig");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return connection;
- }
- }
- public static ArrayList<vals> getAllValues() {
- connection = FetchData.getConnection();
- ArrayList<vals> ValList = new ArrayList<vals>();
- try {
- Statement statement = connection.createStatement();
- ResultSet rs = statement.executeQuery("select * from net");
- while(rs.next()) {
- vals value=new vals();
- value.setDate(rs.getDate(1).toString());
- value.setInput(rs.getDouble(3));
- value.setExpenses(rs.getDouble(2)+rs.getDouble(4));
- ValList.add(value);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return ValList;
- }
- }
- @WebServlet("/PopulateTable")
- public class PopulateTable extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public PopulateTable() {
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- ArrayList<vals> values=new ArrayList<vals>();
- values=FetchData.getAllValues();
- Gson gson = new Gson();
- JsonElement element = gson.toJsonTree(values, new TypeToken<List<vals>>() {}.getType());
- JsonArray jsonArray = element.getAsJsonArray();
- response.setContentType("application/json");
- response.getWriter().print(jsonArray);
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- }
- }
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>AJAX JsonArray</title>
- <link href='http://fonts.googleapis.com/css?family=Oxygen' rel='stylesheet' type='text/css'>
- <style type="text/css">
- table, td, th
- {
- border:1px solid green;
- font-family: 'Oxygen', sans-serif;
- }
- th
- {
- background-color:green;
- color:white;
- }
- body
- {
- text-align: center;
- }
- .container
- {
- margin-left: auto;
- margin-right: auto;
- width: 40em;
- }
- </style>
- <script src="http://code.jquery.com/jquery-latest.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- $("#tablediv").hide();
- $("#showTable").click(function(event){
- $.get('PopulateTable',function(responseJson) {
- if(responseJson!=null){
- $("#datatable").find("tr:gt(0)").remove();
- var table1 = $("#datatable");
- $.each(responseJson, function(key,value) {
- var rowNew = $("<tr><td></td><td></td><td></td></tr>");
- rowNew.children().eq(0).text(value['Date']);
- rowNew.children().eq(1).text(value['Input']);
- rowNew.children().eq(2).text(value['Expenses']);
- rowNew.appendTo(table1);
- });
- }
- });
- $("#tablediv").show();
- });
- });
- </script>
- </head>
- <body class="container">
- <h1>Table</h1>
- <input type="button" value="Show Table" id="showTable"/>
- <div id="tablediv">
- <table cellspacing="0" id="datatable">
- <tr>
- <th scope="col">Date</th>
- <th scope="col">Input</th>
- <th scope="col">Expenses</th>
- </tr>
- </table>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement