title: How to retrieve data from MySQL database and place in a HTML Form using Rest Web Services using Java, AJAX
body: <p>I am trying to retrieve user data from MySQL database and place them in an HTML form. I am using AJAX to call web services in Java. But it is not working. It doesn't give any error. But data won't appear. When I tried with POSTMAN it works fine and details appear in JSON. But using AJAX it's not working. Can anybody help me? Thanks in advance. <code>Users.java</code> contains Getters and Setters.</p>
  
  <p>The code in Details.jsp</p>
  
  <pre><code>&lt;form&gt;
  Enter Username:&lt;br&gt;
  &lt;input type="text" id="usernameSe" name="username"&gt;&lt;br&gt;
  &lt;h3&gt;Details Will Appear Here&lt;/h3&gt;&lt;br&gt;
  Username:&lt;br&gt;
  &lt;input type="text" id="username" name="username"&gt;&lt;br&gt;
  Email:&lt;br&gt;
  &lt;input type="text" id="email" name="email"&gt;&lt;br&gt;&lt;br&gt;
  Password:&lt;br&gt;
  &lt;input type="text" id="password" name="password"&gt;&lt;br&gt;
  
  &lt;button id="btnGet"&gt;Save&lt;/button&gt;
  &lt;/form&gt;
  
  
  
  &lt;script type="text/javascript"&gt;
  $(document).ready(function()
  {
      $("#btnGet").click(function(event)
      {
          event.preventDefault();
          var searchDetails;
  
          $.ajax(
          {
              type: 'GET',
              url:  "http://localhost:8080/WebService/webresources/users/get/"+$('#usernameSe').val(),
              dataType: "json",
              success: function(data)
              {
                  searchDetails = data;
                  renderDetails(searchDetails);
              },
              error: function(jqXHR, textStatus, errorThrown) 
              {
                  alert('Error: ' + textStatus);
              }
          });
      });
  });
  
  function renderDetails(user)
  {
      $('#username').val(user.username);
      $('#email').val(user.email);
      $('#password').val(user.password);
  };
  &lt;/script&gt;
  </code></pre>
  
  <p>Code in Service</p>
  
  <pre><code>@GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("get/{username}")
  public String getUsersByName(@PathParam("username")String username) throws ClassNotFoundException, SQLException
  {
      Gson g = new Gson();
      UsersDAO dao = new UsersDAO();
      List&lt;Users&gt; users = dao.getUsersByName(username);
  
      return g.toJson(users);
  }
  </code></pre>
  
  <p>The code in UserDAO.java</p>
  
  <pre><code>public List&lt;Users&gt; getUsersByName(String username) throws ClassNotFoundException, SQLException
  {
      List&lt;Users&gt; user = new ArrayList&lt;&gt;();
      Users u = null;
      sql = "SELECT * FROM users WHERE username = ?";
      con = DbConnection.getConnection();
      pst = con.prepareStatement(sql);
      pst.setString(1, username);
      rs = pst.executeQuery();
      while(rs.next())
      {
          u = new Users();
          u.setUsername(rs.getString("username"));
          u.setEmail(rs.getString("email"));
          u.setPassword(rs.getString("password"));
  
          user.add(u);
      }
      DbConnection.db();
      return user;
  }
  </code></pre>