top of page

Creating JAX-WS Service to lookup Oracle Database

Pre-requisites :

1. Any JVM that supports JAX-WS(Apache, Glassfish, Weblogic). The example uses Glassfish.

2. Oracle Database

3. Eclipse.

 

Installing Glassfish Server

1. Download the latest version from https://glassfish.java.net/download.html and install based on the platform.

2. Create a sample domain

Steps to configure JNDI in glassfish Server

1. Create a JDBC Connection Pool :

Use the DB URL like :

jdbc:oracle:thin:@dbserver:1521:dbname

 

2. Configure JDBC Resource

The setup in Glassfish is complete.

 

 

Creating DB Objects

In this example we will create a couple of tables and do a lookup on the same.

 

create table vehicle

(

vin varchar2(10),

model varchar2(10),

year varchar2(4),

cost varchar2(8)

);

 

create table vehicle_specs

(

vin varchar2(10),

color varchar2(10),

engine varchar2(4)

);

 

Insert few sample records to the database.

 

Steps in Eclipse

1. Open Eclipse in J2EE Perspective or create your own perspective.

2. File > New Project > Dynamic Web Project

3. Set the project name to JaxDBLookup

4. Click New Runtime

5. Choose Glassfish

 

6. Click on “Installed JRE preferences” > Add > Standard VM > Select the JDK installed on your machine(JDK 1.7 minimum)

 

8. Also choose the Glassfish Directory that was installed on your machine.

9. Finish

 

Writing the code.

We will write the following java classes

 

1. ConnectDB class in the package com.jaxws.util. This class will connect to DB using JNDI.

2. Fault, Vehicle classes in the package com.jaxws.bean. These two will serve a EJB beans for the application

3. ServiceException class will serve as the fault handling class in com.jaxws.error

4. VehicleManager class will serve as the bean manager in the class com.jaxws.manager

5. VehicleService class will serve as the main class which will be the entry point for the web service invocation in the package com.jaxws.service.

 

Creating ConnectDB class.

 

package com.jaxws.util;

import java.sql.Connection;

import java.sql.SQLException;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

public class ConnectDB {

private static ConnectDB instance = null;

private Connection dbcon = null;

public void ConnDB() {

}

public static ConnectDB getInstance() {

if (instance == null) {

instance = new ConnectDB();

}

return instance;

}

public boolean openConnection() {

try {

InitialContext ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("jdbc/testdb");

dbcon = ds.getConnection();

} catch (SQLException | NamingException sqle) {

}

return true;

}

public Connection getConnection() {

if (openConnection()) {

return dbcon;

}

return dbcon;

}

public void closeConnection() {

try {

dbcon.close();

dbcon = null;

} catch (SQLException e) {

e.printStackTrace();

}

}

}

 

 

 

The above program will create only a single instance of the database connection.

 

Creating the Vehicle and Fault Bean.

 

a. Right click on the package com.jaxws.bean > New > class

b. Name the class as Vehicle

c. Add the below variables inside the class.

private String vin;

private String model;

private String year;

private String color;

private String engine;

private String price;

 

d) Right click on the editor > Source > Generate getters and setters

 

e) choose all the variables and click ok.  The code will be as below

 

package com.jaxws.bean;

 

public class Vehicle {

private String vin;

private String model;

private String year;

private String color;

private String engine;

private String price;

public String getVin() {

return vin;

}

public void setVin(String vin) {

this.vin = vin;

}

public String getModel() {

return model;

}

public void setModel(String model) {

this.model = model;

}

public String getYear() {

return year;

}

public void setYear(String year) {

this.year = year;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public String getEngine() {

return engine;

}

public void setEngine(String engine) {

this.engine = engine;

}

public String getPrice() {

return price;

}

public void setPrice(String price) {

this.price = price;

}

}

 

Follow the same steps for Fault with two variables faultCode, faultString.

package com.jaxws.bean;

public class Fault {

public String faultCode;

public String faultString;

public String getFaultCode() {

return faultCode;

}

public void setFaultCode(String faultCode) {

this.faultCode = faultCode;

}

public String getFaultString() {

return faultString;

}

public void setFaultString(String faultString) {

this.faultString = faultString;

}

 

}

 

Creating the fault handler class.

 

The class ServiceException will extend the class Exception and will have an JAX-WS annotation @WebFault

 

package com.jaxws.error;

import javax.xml.ws.WebFault;

import com.jaxws.bean.Fault;

@WebFault(name = "VINNotFoundFault", targetNamespace = "http://www.example.com/VINNotFoundFault")

public class ServiceException extends Exception {

 

private static final long serialVersionUID = 1L;

private Fault fault;

 

public ServiceException() {

}

 

protected ServiceException(String message, Fault faultInfo) {

super(message);

this.fault = faultInfo;

}

 

public ServiceException(String message, Fault faultInfo, Throwable cause) {

super(message, cause);

this.fault = faultInfo;

}

 

public ServiceException(String message) {

super(message);

}

 

public ServiceException(String code, String message) {

super(message);

this.fault = new Fault();

this.fault.setFaultCode(code);

this.fault.setFaultString(message);

}

 

public ServiceException(Throwable cause) {

super(cause);

}

 

public ServiceException(String message, Throwable cause) {

super(message, cause);

}

}

 

 

Creating the bean Manager class.

 

This class is called the bean manager, because the getters and setters of beans are used here.

 

package com.jaxws.manager;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import com.jaxws.bean.Fault;

import com.jaxws.bean.Vehicle;

import com.jaxws.error.ServiceException;

import com.jaxws.util.ConnectDB;

 

public class VehicleManager {

private static Connection dbcon = ConnectDB.getInstance().getConnection();

public static Vehicle getPrice(Vehicle v) throws ServiceException {

String sql1="select a.vin vin, a.model model, a.year year, a.cost cost, b.color color, b.engine engine from vehicle a, vehicle_specs b where a.vin=b.vin and a.vin=?";

Boolean NO_RECORD_FOUND = true;

try {

PreparedStatement psPrice = dbcon.prepareStatement(sql1);

psPrice.setString(1, v.getVin());

 

ResultSet rsPrice=psPrice.executeQuery();

while(rsPrice.next()) {

v.setVin(rsPrice.getString("vin"));

v.setModel(rsPrice.getString("model"));

v.setPrice(rsPrice.getString("cost"));

v.setYear(rsPrice.getString("year"));

v.setEngine(rsPrice.getString("engine"));

v.setColor(rsPrice.getString("color"));

NO_RECORD_FOUND=false;

}

 

} catch (SQLException e) {

e.printStackTrace();

}

if(NO_RECORD_FOUND ) {

Fault fault = new Fault();

fault.setFaultCode("1");

fault.setFaultString("VIN Not Found");

throw new ServiceException("1", "VIN Not Found");

}

return v;

 

}

}

 

Finally the main class. VehicleService.

Here we use 2 annotations @WebService, @WebMethod. The parameters inside are optional.

 

package com.jaxws.service;

import javax.jws.WebService;

import javax.jws.WebMethod;

import com.jaxws.bean.Vehicle;

import com.jaxws.error.ServiceException;

import com.jaxws.manager.VehicleManager;

@WebService(serviceName="VinLookup",targetNamespace="http://www.example.com/vinlookup",name="vinLookupService" )

public class VehicleService {

public void constructor() {

}

@WebMethod

public Vehicle getVehiclePrice(Vehicle veh) throws ServiceException {

Vehicle vehret;

vehret = VehicleManager.getPrice(veh);

return vehret;

}

}

 

Deploying and validating the web service.

 

1. Right click on the project > Export > War File > Choose a destination.

2. Open the glassfish server

3. Applications > Deploy

4. Choose the war file and click ok.

5. Click on the application JaxDBLookup  > View Endpoint > Get the wsdl URL

 

Open SoapUI

6. File > New SOAP Project > Paste the wsdl URL and click OK

 

Test the service with both cases.

 

That ends the tutorial.

bottom of page