0% found this document useful (0 votes)
52 views

Practical 4: Program: 1. Open Netbeans, Click On Services Tab Databases Right Click On Java DB Start Server

The document describes steps to create a RESTful web service for performing CRUD operations on a database table using JAVA. It involves starting the database server, creating a table, connecting to it, generating RESTful web services, and then creating clients to test CRUD operations by inserting, retrieving, updating and deleting records from the table. Code snippets are provided for creating a new record, retrieving all records, and methods for other operations.

Uploaded by

Priya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Practical 4: Program: 1. Open Netbeans, Click On Services Tab Databases Right Click On Java DB Start Server

The document describes steps to create a RESTful web service for performing CRUD operations on a database table using JAVA. It involves starting the database server, creating a table, connecting to it, generating RESTful web services, and then creating clients to test CRUD operations by inserting, retrieving, updating and deleting records from the table. Code snippets are provided for creating a new record, retrieving all records, and methods for other operations.

Uploaded by

Priya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

ROLL NO: 38 CLOUD COMPUTING PRIYA S.

PRACTICAL 4
PROGRAM:
1. Open Netbeans, click on Services tab>Databases>right click on Java DB>start
server.

2. Create a database playerDB with with table name and password as


shown.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

3. Connect to the database.


ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Create new java project (Server named PlayerServer).


ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Run the jsp page.


ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Generate restful services:


ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Test restful web services.


ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Now run the server, and start creating clients for create, read, update,
delete operations.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Create operation:
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Code:
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.GenericType;
import org.glassfish.jersey.client.ClientResponse;
import playerentities.Player;
public static void main(String args[])
{
CreatePlayerJerseyClient c1=new CreatePlayerJerseyClient();
ClientResponse response=c1.findAll_XML(ClientResponse.class);
GenericType<List<Player>> genericType=new GenericType<List<Player>>() {};
List<Player> data=new ArrayList<Player>();
data=(response.readEntity(genericType));

Player p=new Player();


p.setFirstname("Michael");
p.setId(30);
p.setJerseynumber(60);
p.setLastname("Phelps");
p.setLastspokenwords("Thanks to my fans");
c1.create_XML(p);
}
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

1. View the changes in the database


ROLL NO: 38 CLOUD COMPUTING PRIYA S.

2. You can also test the web service in the browser.


ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Retrieve operation:
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Code:
import java.util.ArrayList;
import java.util.List;
import playerentities.Player;
public static void main(String args[]) throws UniformInterfaceException
{
GetPlayerJerseyClient client1=new GetPlayerJerseyClient();
ClientResponse response=client1.findAll_XML(ClientResponse.class);

GenericType<List<Player>> genericType=new GenericType<List<Player>>() {};


List<Player> data=new ArrayList<Player>();
data=(response.getEntity(genericType));
System.out.println("Retrieving and displaying Player Details");
for(Player players:data)
{
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

System.out.println("Firstname: "+players.getFirstname());
System.out.println("Lastname: "+players.getLastname());
System.out.println("PlayerID: "+players.getId());
System.out.println("Jersey number: "+players.getJerseynumber());
System.out.println("Last spoken words: "+players.getLastspokenwords());

1. Run the file.


ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Update operation:
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

Delete operation:
ROLL NO: 38 CLOUD COMPUTING PRIYA S.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

PRACTICAL 6

Using REST to Invoke the API

Making a request

REST, or Representational State Transfer, in the JSON/Atom Custom Search API is somewhat
different from traditional REST. Instead of providing access to resources, the API provides access to a
service. As a result, the API provides a single URI that acts as the service endpoint.

You can retrieve results for a particular search by sending an HTTP GET request to its URI. You pass
in the details of the search request as query parameters. The format for the JSON/Atom Custom
Search API URI is:

https://www.googleapis.com/customsearch/v1?parameters

Three query parameters are required with each search request:

 API key - Use the key query parameter to identify your application.
 Custom search engine ID - Use cx to specify the custom search engine you want to use to
perform this search. The search engine must be created with the Control Panel
 Search query - Use the q query parameter to specify your search expression.

All other query parameters are optional.

Steps:

1. Generate API key using your Google account.


2. Once the key is generated, add the websites from which you want to fetch the results or
perform the search in.
3. This is done by adding Custom Search Engine (CSE) using your Google account.
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

JAVASCRIPT CODE:
<html>
<head>
<title>my site</title>
<head>
<body>
<div1>Google Search Client</div1>
<script>
(function() {
var cx = '003455161757857532124:l8sam5l60gw';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

})();
</script>
<gcse:search></gcse:search>
<div2></div2>
</body>
</html>

OUTPUT:
ROLL NO: 38 CLOUD COMPUTING PRIYA S.

You might also like