Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7766771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:30:22+00:00 2026-06-01T15:30:22+00:00

I have done inserting a record into database but I don’t know how to

  • 0

I have done inserting a record into database but I don’t know how to retrieve it.
My code is:
Account.java:

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;  
  }
}

MyAccount.java:

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;  
  }
}

insert.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert</title>
</head>
<body>
    <form action="rest/user/fetch" method="POST">
        <p>
            Name : <input id="name" name="name" />
        </p>
        <input type="submit" value="Add" />
            <input type="submit" value="Retrive" />
    </form>
</body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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">
   <display-name>FetchAndInsert</display-name>
   <servlet>
      <servlet-name>REST Servlet</servlet-name>
      <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
      <init-param>
         <param-name>com.sun.jersey.config.property.packages</param-name>
         <param-value>com.fetch</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>REST Servlet</servlet-name>
      <url-pattern>/rest/*</url-pattern>
   </servlet-mapping>
</web-app>

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.

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:30:24+00:00Added an answer on June 1, 2026 at 3:30 pm

    Finally I have done it, Now I am able to perform both insertion and retriving of data from database using jersey(JAX – RS) webservices and able to display it in a html page.

    Account.java:

    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(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;
        }
    }
    

    MyAccount.java:

    package com.fetch;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.persistence.Entity;
    import javax.ws.rs.FormParam;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    
    @WebService()
    @Entity
    @Path("/user")
    public class MyAccount
    {
        @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";
        }
    
        @GET
        @Path("/retrive")
        @Produces("text/html")
        @WebMethod(operationName = "retrive")
        public String retrive() 
        {
            ResultSet rs = null;
            String details = ""; 
            try 
            {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo", "root", "root");
    
                String query = "select ID,NAME from CUSTOMER";
    
                PreparedStatement st = con.prepareStatement(query);
                rs = st.executeQuery();
    
                details = "<html><body>"; 
                details = details + "<table border=1>";
                details = details + "<tr><td><Strong>Id </Strong></td>" +
                                        "<td><Strong>Name </Strong></td>" + "</tr>";
                while (rs.next()) 
                {
                    details = details + "<tr><td>" + rs.getInt("ID") + "</td>" +
                                            "<td>" + rs.getString("NAME") + "</td></tr>";
                }
                details += "</table></body></html>"; 
            } 
            catch (Exception e) 
            {
                System.out.println(e.getMessage());
            }   
            return details;
        }
    }
    

    insert.html:

     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert</title>
    </head>
    <body>
        <form action="rest/user/fetch" method="POST">
            <p>
                Name : <input id="name" name="name" />
            </p>
            <input type="submit" value="Add" />
        </form>
        <form action="rest/user/retrive" method="GET">
            <input type="submit" value="Retrive" />
        </form>
    </body>
    </html>
    

    my web.xml is:

     <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        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">
        <display-name>FetchAndInsert</display-name>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>REST Servlet</servlet-name>
            <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>com.sun.jersey.config.property.packages</param-name>
                <param-value>com.fetch</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>REST Servlet</servlet-name>
            <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>
    </web-app>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done Java and JSP programming in the past, but I am new
i have a problem in inserting some data into a table. I have done
When inserting a new line character into a string I have usually done this:
I have done a bit of research into this and it seems that the
I have done a little Django development, but it has all been in a
I have done jQuery and Ajax, but I am not able to get the
I have done the following code in JavaScript to put focus on the particular
I have created a php script to import rss feed into the database. The
We have an application that takes real time data and inserts it into database.
Hello I have a database for a certain record where it needs to store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.