title: How to retrieve data from database using webservices (JAX - RS) in eclipse using Java
body: <p>I have done inserting a record into database but I don't know how to retrieve it.
  My code is:
  <strong>Account.java:</strong></p>
  
  <pre><code>package com.fetch;
  
  import java.io.Serializable;
  import javax.persistence.Column;
  import javax.persistence.Id;
  
  public class Account implements Serializable 
  {   
    @Id
    private long id;
  
    @Column(name="NAME")  
    private String name;
  
    public Account()  
    {
  
    }
  
    public Account(int id, String name)   
    {     
       this.id = id;      
       this.name = name;  
    }
  
    public long getId() 
    {     
       return id; 
    }
  
    public void setId(long id) 
    {     
       this.id = id;  
    }
  
    public String getName() 
    {     
       return name;   
    }
  
    public void setName(String name)
    {     
       this.name = name;  
    }
  }
  </code></pre>
  
  <p><strong>MyAccount.java:</strong></p>
  
  <pre><code>package com.fetch;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.util.ArrayList;
  import java.util.Collection;
  import javax.jws.WebMethod;
  import javax.jws.WebService;
  import javax.persistence.Entity;
  import javax.persistence.NamedQuery;
  import javax.persistence.Table;
  import javax.ws.rs.FormParam;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  
  @WebService()
  @Entity
  @Table(name = "CUSTOMER")
  @Path("/user")
  @NamedQuery(name="loginquery", query="select ID,NAME from CUSTOMER")
  public class MyAccount 
  {   
     private Account ac;  
     public MyAccount()   
     {    
       // TODO Auto-generated constructor stub      
       ac = new Account();    
     }    
  
     @POST    
     @Path("/fetch")  
     @WebMethod(operationName = "insert") 
     public String insert(@FormParam("name") String name) 
     {     
        try   
        {     
           Class.forName("com.mysql.jdbc.Driver");        
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo", "root", "root");
  
           String query = "insert into CUSTOMER"+"(NAME) VALUES"+"(?)";
       PreparedStatement st = con.prepareStatement(query);    
           st.setString(1,name);  
       st.executeUpdate();
        }     
        catch (Exception e)   
        {     
           System.out.println(e.getMessage());    
        } 
        return"Record inserted successfully";
     }    
    public Account getAc() 
    {     
       return ac; 
    } 
    public void setAc(Account ac) 
    {     
       this.ac = ac;  
    }
  }
  </code></pre>
  
  <p><strong>insert.html:</strong></p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Insert&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action="rest/user/fetch" method="POST"&gt;
          &lt;p&gt;
              Name : &lt;input id="name" name="name" /&gt;
          &lt;/p&gt;
          &lt;input type="submit" value="Add" /&gt;
              &lt;input type="submit" value="Retrive" /&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>web.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
     &lt;display-name&gt;FetchAndInsert&lt;/display-name&gt;
     &lt;servlet&gt;
        &lt;servlet-name&gt;REST Servlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
        &lt;init-param&gt;
           &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
           &lt;param-value&gt;com.fetch&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
     &lt;/servlet&gt;
     &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;REST Servlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
     &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>How to retrieve the data from the database when the user clicks on the retrieve button, and display all the records in another HTML form?
  Please give suggestions on how to do it.</p>
  
  <p>In my application, when the user clicks the retrieve button it is performing the insert operation.
  But I want is, when the user clicks it should go to the another page and display the data in table from database.</p>
  
  <p>Can anybody tell the mistake what I have done, and give suggestions for finding solution for it and how to display the dynamic table in other page when the user clicks the retrieve button?</p>