How to retrieve data from database using webservices (JAX - RS) in eclipse using Java

前端 未结 2 494
醉话见心
醉话见心 2021-01-03 04:42

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         


        
2条回答
  •  一生所求
    2021-01-03 05:03

    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 = ""; 
                details = details + "";
                details = details + "" +
                                        "" + "";
                while (rs.next()) 
                {
                    details = details + "" +
                                            "";
                }
                details += "
    Id Name
    " + rs.getInt("ID") + "" + rs.getString("NAME") + "
    "; } catch (Exception e) { System.out.println(e.getMessage()); } return details; } }

    insert.html:

     
    
    
    
    Insert
    
    
        

    Name :

    my web.xml is:

     
    
        FetchAndInsert
        
            index.jsp
        
        
            REST Servlet
            com.sun.jersey.spi.container.servlet.ServletContainer
            
                com.sun.jersey.config.property.packages
                com.fetch
            
            1
        
        
            REST Servlet
            /rest/*
        
    
    

提交回复
热议问题