C:/Users/zzaier/Documents/NetBeansProjects/WebApplication4/src/java/mainpackage/MainClass.java

Similar documents
Java Database Connectivity (JDBC) 25.1 What is JDBC?

SQream Connector JDBC SQream Technologies Version 2.9.3

Topic 12: Database Programming using JDBC. Database & DBMS SQL JDBC

Unit 3 - Java Data Base Connectivity

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Cập nhật ResultSet trong JDBC

Preview from Notesale.co.uk Page 21 of 162

Java Database Connectivity

Java. Curs 2. Danciu Gabriel Mihail. Septembrie 2018

DATABASE DESIGN I - 1DL300

COP4540 TUTORIAL PROFESSOR: DR SHU-CHING CHEN TA: H S IN-YU HA

DB I. 1 Dr. Ahmed ElShafee, Java course

Servlet 5.1 JDBC 5.2 JDBC

1. PhP Project. Create a new PhP Project as shown below and click next

Visit for more.

Databases 2012 Embedded SQL

CHAPTER 2 JDBC FUNDAMENTALS

More Database Programming. CS157A Chris Pollett Nov. 2, 2005.

Three-Tier Architecture

VanillaCore Walkthrough Part 1. Introduction to Database Systems DataLab CS, NTHU

Accessing a database from Java. Using JDBC

The Many Faces Of Apache Ignite. David Robinson, Software Engineer May 13, 2016

Accessing databases in Java using JDBC

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

A hybrid replication routing protocol for delay tolerant networks

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

Lab # 9. Java to Database Connection

CHAPTER 2 JDBC FUNDAMENTALS

IBM Netezza JDBC 연동가이드

Database connectivity (II)

while (rs.next()) { String[] temp_array = {"","",""}; int prodid = rs.getint(1); temp_array[0] = ""+prodid;

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

Döcu Content IBM Notes Domino, DB2 Oracle JDeveloper, WebLogic

CSCI/CMPE Object-Oriented Programming in Java JDBC. Dongchul Kim. Department of Computer Science University of Texas Rio Grande Valley

Introduction to Databases

Introduction to SQL & Database Application Development Using Java

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

JDBC drivers are divided into four types or levels. The different types of jdbc drivers are:

Web Applications and Database Connectivity using JDBC (Part II)

Logging and Recovery. 444 Section, April 23, 2009

SQL Client Example using KnoxShell in Apache Knox

access to a JCA connection in WebSphere Application Server

Instructor: Jinze Liu. Fall 2008

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

Project Documentation

An IBM Rational Software TechNote

JDBC [Java DataBase Connectivity]

Tiers (or layers) Separation of concerns

3) execute() Usage: when you cannot determine whether SQL is an update or query return true if row is returned, use getresultset() to get the

Coleman Leviter, OCP IBM/NYC MTA/IOUG Oracle Database Developer

Database Application Development Part 2 - Chapter

Database Lab#9 Connecting to postgresql database from java programs

Using a CVP VoiceXML application to implement a logical shadow queue for ICM

School of Computing and Information Technology. Examination Paper Autumn Session 2017

Chapter 10 Java and SQL. Wang Yang

WebSphere Connection Pooling. by Deb Erickson Shawn Lauzon Melissa Modjeski

Programming in Java

MANTHLY TEST SEPTEMBER 2017 QUESTION BANK CLASS: XII SUBJECT: INFORMATICS PRACTICES (065)

How to program applications. CS 2550 / Spring 2006 Principles of Database Systems. SQL is not enough. Roadmap

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

UNIT-3 Java Database Client/Server

CSE 135. Three-Tier Architecture. Applications Utilizing Databases. Browser. App. Server. Database. Server

S.No File Name Path Description 1 DataSourceElement.java Jmeter/src/protocol/jdbc/org/a pache/jmeter/protocol/jdbc/ autocsv_jdbcconfig

CSC309: Introduction to Web Programming. Lecture 13

WEB SERVICES EXAMPLE 2

Database Applications (15-415)

Databases and JDBC. by Vlad Costel Ungureanu for Learn Stuff

Prof. Edwar Saliba Júnior

Chettinad College of Engineering and Technology CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND TECHNOLOGY

Content Services for JDBC Driver User Guide

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Departamento de Lenguajes y Sistemas Informáticos

JBASE JDBC DRIVER VERSION 1.0

Sub: Advance Java Programming Laboratory

CreateServlet.java

Oracle Database 10g Java Web

EJB - ACCESS DATABASE

Oracle Exam 1z0-809 Java SE 8 Programmer II Version: 6.0 [ Total Questions: 128 ]

INTRODUCTION TO JDBC - Revised spring

Advanced Programming Techniques. Database Systems. Christopher Moretti

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 9. Nadia Polikarpova

JDeveloper. Read Lotus Notes Data via URL Part 2

HR Database. Sample Output from TechWriter 2007 for Databases

LAMPIRAN. Lampiran 1 Source Code service a. Source Code koneksi. b. source code service user

Introduction to Java Development with IDS

Advanced Programming Languages Effective Java Item 1. Spring 2015 Chungnam National Univ Eun-Sun Cho

EXISTS NOT EXISTS WITH

Prof. Edwar Saliba Júnior

Distributed Systems Project 5 Assigned: Friday, April 6, 2012 Due: Friday, April 20, 11:59:59 PM

Databases and MySQL. COMP 342: Programming Methods. 16 September Databases and MySQL

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

Database Programming with SQL

INTRODUCTION TO JDBC - Revised Spring

Beans and HTML Forms. Usually Beans are used to represent the data of HTML forms

Brekeke PBX Version 3 IVR Developer s Guide Brekeke Software, Inc.

Databases and SQL Lab EECS 448

Transcription:

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