package mainpackage; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import javax.ws.rs.core.context; import javax.ws.rs.core.uriinfo; import javax.ws.rs.pathparam; import javax.ws.rs.consumes; import javax.ws.rs.produces; import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.put; import net.sf.json.jsonarray; import net.sf.json.jsonobject; /** * REST Web Service * * @author zzaier */ @Path("cegepgim") public class MainClass { /** * @param args the command line arguments */ // JDBC driver name and database URL static final String JDBC_DRIVER = "oracle.jdbc.oracledriver"; static final String DB_URL = "jdbc:oracle:thin:@144.217.163.57:1521:xe"; // Database credentials static final String USER = "hr"; static final String PASS = "cegepgim"; @Context private UriInfo context; /** * Creates a new instance of MainClass 1.1 of 16 2017.05.08 15:05:32
1.2 of 16 2017.05.08 15:05:32
*/ public MainClass() { @GET @Path("employeeslist") @Produces("application/json") public String getjsonemployees() { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createstatement(); String sql; sql = "SELECT * FROM employees e natural join jobs j left join " + "departments d on d.department_id=e.department_id"; ResultSet rs = stmt.executequery(sql); //STEP 5: Extract data from result set JSONArray listemployees= new JSONArray(); JSONObject singleemployee= new JSONObject(); while(rs.next()){ //Retrieve by column name String fname = rs.getstring("first_name"); String lname = rs.getstring("last_name"); double salary = rs.getdouble("salary"); String jobtitle = rs.getstring("job_title"); String departmentname = rs.getstring("department_name"); //Display values singleemployee.accumulate("first_name", fname); singleemployee.accumulate("last_name", lname); singleemployee.accumulate("salary", salary); 2.1 of 16 2017.05.08 15:05:32
2.2 of 16 2017.05.08 15:05:32
singleemployee.accumulate("jobtitle", jobtitle); singleemployee.accumulate("departmentname", departmentname); listemployees.add(singleemployee); singleemployee.clear(); System.out.println(listEmployees.toString()); //STEP 9: Clean-up environment rs.close(); stmt.close(); conn.close(); return listemployees.tostring(); catch(sqlexception se){ //Handle errors for JDBC se.printstacktrace(); catch(exception e){ //Handle errors for Class.forName e.printstacktrace(); System.out.println("Goodbye!"); return "{"; @GET @Path("locationslist") @Produces("application/json") public String getjsonlocation() { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createstatement(); String sql; sql = "SELECT * FROM locations natural join countries natural join regions 3.1 of 16 2017.05.08 15:05:32
s"; 3.2 of 16 2017.05.08 15:05:32
ResultSet rs = stmt.executequery(sql); //STEP 5: Extract data from result set JSONArray listlocations= new JSONArray(); JSONObject singlelocation= new JSONObject(); while(rs.next()){ //Retrieve by column name String city = rs.getstring("city"); String street = rs.getstring("street_address"); String country = rs.getstring("country_name"); String region = rs.getstring("region_name"); //Display values singlelocation.accumulate("city", city); singlelocation.accumulate("street", street); singlelocation.accumulate("country", country); singlelocation.accumulate("region", region); listlocations.add(singlelocation); singlelocation.clear(); System.out.println(listLocations.toString()); //STEP 9: Clean-up environment rs.close(); stmt.close(); conn.close(); return listlocations.tostring(); catch(sqlexception se){ //Handle errors for JDBC se.printstacktrace(); catch(exception e){ //Handle errors for Class.forName e.printstacktrace(); System.out.println("Goodbye!"); return "{"; @GET @Path("employeedetails&{id") @Produces("application/json") 4.1 of 16 2017.05.08 15:05:32
4.2 of 16 2017.05.08 15:05:32
public String getjsonemployeedetails(@pathparam("id")int theid) { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createstatement(); String sql; sql = "SELECT * FROM employees e natural join jobs j left join " + "departments d on d.department_id=e.department_id " + "where e.employee_id="+theid; ResultSet rs = stmt.executequery(sql); //STEP 5: Extract data from result set JSONObject singleemployee= new JSONObject(); if(rs.next()){ //Retrieve by column name String fname = rs.getstring("first_name"); String lname = rs.getstring("last_name"); double salary = rs.getdouble("salary"); String jobtitle = rs.getstring("job_title"); String departmentname = rs.getstring("department_name"); //Display values singleemployee.accumulate("first_name", fname); singleemployee.accumulate("last_name", lname); singleemployee.accumulate("salary", salary); singleemployee.accumulate("jobtitle", jobtitle); singleemployee.accumulate("departmentname", departmentname); else{ singleemployee.accumulate("message","employee Not Available" ); 5.1 of 16 2017.05.08 15:05:32
5.2 of 16 2017.05.08 15:05:32
System.out.println(singleEmployee.toString()); //STEP 9: Clean-up environment rs.close(); stmt.close(); conn.close(); return singleemployee.tostring(); catch(sqlexception se){ //Handle errors for JDBC se.printstacktrace(); catch(exception e){ //Handle errors for Class.forName e.printstacktrace(); System.out.println("Goodbye!"); return "{"; @GET @Path("jobhistorydetails&{id") @Produces("application/json") public String getjsonemployeehistory(@pathparam("id")int theid) { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName(JDBC_DRIVER); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createstatement(); String sql; sql = "SELECT * FROM employees e left join job_history j on " + "e.employee_id=j.employee_id left join jobs d on " + "d.job_id=j.job_id where e.employee_id="+theid; ResultSet rs = stmt.executequery(sql); 6.1 of 16 2017.05.08 15:05:32
6.2 of 16 2017.05.08 15:05:32
//STEP 5: Extract data from result set JSONObject singleemployee= new JSONObject(); JSONObject singlejob= new JSONObject(); JSONArray listjobs= new JSONArray(); if (rs.next()){ //Retrieve by column name String fname = rs.getstring("first_name"); String lname = rs.getstring("last_name"); //Display values singleemployee.accumulate("first_name", fname); singleemployee.accumulate("last_name", lname); do { String jobtitle = rs.getstring("job_title"); String start = rs.getstring("start_date"); String end = rs.getstring("end_date"); if(jobtitle!=null){ singlejob.clear(); singlejob.accumulate("jobtitle", jobtitle); singlejob.accumulate("startdate", start); singlejob.accumulate("enddate", end); listjobs.add(singlejob); while(rs.next()); singleemployee.accumulate("jobhistory",listjobs); else{ singleemployee.accumulate("message","employee Not Available" ); System.out.println(singleEmployee.toString()); //STEP 9: Clean-up environment rs.close(); 7.1 of 16 2017.05.08 15:05:32
7.2 of 16 2017.05.08 15:05:32
stmt.close(); conn.close(); return singleemployee.tostring(); catch(sqlexception se){ //Handle errors for JDBC se.printstacktrace(); catch(exception e){ //Handle errors for Class.forName e.printstacktrace(); System.out.println("Goodbye!"); return "{"; 8.1 of 16 2017.05.08 15:05:32
8.2 of 16 2017.05.08 15:05:32