Monday, November 23, 2015

Simple CRUD operation using JSF Technologies


Image_1
1:- SiddhuCrudDao
package com.siddhu.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.siddhu.dbconnection.DBUtility;
import com.siddhu.model.User;
public class SiddhuCrudDao {
private Connection connection;
private int result;
public SiddhuCrudDao() {
connection = DBUtility.getConnection();
}
public int addUser(User user) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("insert into \"SIDDHU\".\"USER\"(userid,firstname,lastname,email) values (?,?, ?, ? )");
preparedStatement.setInt(1, user.getUserid());
preparedStatement.setString(2, user.getFirstName());
preparedStatement.setString(3, user.getLastName());
preparedStatement.setString(4, user.getEmail());
result= preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public int deleteUser(int userId) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("delete from USER where userid=?");
preparedStatement.setInt(1, userId);
result= preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public int updateUser(User user) throws ParseException {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("update USER set lastname=?,email=?" +
"where userid=?");
preparedStatement.setString(1, user.getLastName());
preparedStatement.setString(2, user.getEmail());
preparedStatement.setInt(3, user.getUserid());
result= preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public List getAllUsers() {
List users = new ArrayList();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from USER");
while (rs.next()) {
User user = new User();
user.setUserid(rs.getInt("userid"));
user.setFirstName(rs.getString("firstname"));
user.setLastName(rs.getString("lastname"));
user.setEmail(rs.getString("email"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
public User getUserById(int userId) {
User user = new User();
try {
PreparedStatement preparedStatement = connection.
prepareStatement("select * from USER where userid=?");
preparedStatement.setInt(1, userId);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
user.setUserid(rs.getInt("userid"));
user.setFirstName(rs.getString("firstname"));
user.setLastName(rs.getString("lastname"));
user.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
public static void main(String args[])
{
SiddhuCrudDao objSiddhuCrudDao = new SiddhuCrudDao();
User user=new User();
user.setUserid(2);
user.setFirstName("Siddharatha");
user.setLastName("Dhumale");
user.setEmail("shdhumale@gmail.com");
objSiddhuCrudDao.addUser(user);
}
}

2:- DBUtility
package com.siddhu.dbconnection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtility {
private static Connection connection = null;
public static Connection getConnection() {
if (connection != null)
return connection;
else {
try {
Properties prop = new Properties();
//InputStream inputStream = DBUtility.class.getClassLoader().getResourceAsStream("/config.properties");
//InputStream inputStream = DBUtility.class.getClassLoader().getResourceAsStream("config.properties");
InputStream inputStream = DBUtility.class.getClassLoader().getResourceAsStream("config.properties");
prop.load(inputStream);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
/*// Store the database URL in a string
String serverName = "127.0.0.1";
String portNumber = "1521";
String sid = "XE";
String dbUrl = "jdbc:oracle:thin:@" + serverName + ":" + portNumber
+ ":" + sid;
Class.forName("oracle.jdbc.driver.OracleDriver");
// set the url, username and password for the database
connection = DriverManager.getConnection(dbUrl, "siddhu", "siddhu"); */
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return connection;
}
}
}

3:- User
package com.siddhu.model;
public class User {
private int userid;
private String firstName;
private String lastName;
private String email;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [userid=" + userid + ", firstName=" + firstName
+ ", lastName=" + lastName + ", email="
+ email + "]";
}

}

4:- UserBean
package com.test.siddhu;
import com.siddhu.dao.SiddhuCrudDao;
import com.siddhu.model.User;
public class UserBean {
private int userID;
private String firstName;
private String lastName;
private String email;
private SiddhuCrudDao dao;
//This is Action method to add user
public String addUser() {
dao=new SiddhuCrudDao();
User user=new User();
user.setUserid(this.getUserID());
user.setFirstName(this.getFirstName());
user.setLastName(this.getLastName());
user.setEmail(this.getEmail());
//dao.addUser(user);
if (dao.addUser(user)>0)
return "success";
else
return "failure";
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

public static void main(String args[])
{
UserBean objUserBean = new UserBean();
//User user=new User();
objUserBean.setUserID(2);
objUserBean.setFirstName("Siddharatha");
objUserBean.setLastName("Dhumale");
objUserBean.setEmail("shdhumale@gmail.com");
objUserBean.addUser();
}

}

5:- faces-config.xml

6:- web.xml


7:- AddUser.jsp

8:- ErrorMessage.jsp

9:- ListUser.jsp

Simple JSF Example for Navigation

1:- Manage Bean

package com.test.siddhu;
public class UserBean {
private int userID;
private String firstName;
private String lastName;
private String email;
//This is Action method to add user
public String addUser() {
return "success";
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
2:- faces-config.xml
Image_3

Image_4


Image_5
Error
Image_6
Fill proper Data and click on Add customer Button
Image_7

Image_8

Sunday, November 22, 2015

Developing Oracle ADF application using Eclipse


Step :- 1- Before starting the application you need to first create the DB schema and relative table. Use below given script for the same.


SQL Script for HR Example to develop in Eclipse using below link



CREATE TABLE COUNTRIES
    (
     COUNTRY_ID CHAR (2 BYTE)  NOT NULL ,
     COUNTRY_NAME VARCHAR2 (40 BYTE) ,
     REGION_ID NUMBER
    ) LOGGING
;



COMMENT ON COLUMN COUNTRIES.COUNTRY_ID IS 'Primary key of countries table.'
;

COMMENT ON COLUMN COUNTRIES.COUNTRY_NAME IS 'Country name'
;

COMMENT ON COLUMN COUNTRIES.REGION_ID IS 'Region ID for the country. Foreign key to region_id column in the departments table.'
;

CREATE UNIQUE INDEX COUNTRY_C_ID_PKX ON COUNTRIES
    (
     COUNTRY_ID ASC
    )
;

ALTER TABLE COUNTRIES
    ADD CONSTRAINT COUNTRY_C_ID_PK PRIMARY KEY ( COUNTRY_ID ) ;



CREATE TABLE DEPARTMENTS
    (
     DEPARTMENT_ID NUMBER (4)  NOT NULL ,
     DEPARTMENT_NAME VARCHAR2 (30 BYTE)  NOT NULL ,
     MANAGER_ID NUMBER (6) ,
     LOCATION_ID NUMBER (4)
    ) LOGGING
;



COMMENT ON COLUMN DEPARTMENTS.DEPARTMENT_ID IS 'Primary key column of departments table.'
;

COMMENT ON COLUMN DEPARTMENTS.DEPARTMENT_NAME IS 'A not null column that shows name of a department. Administration,
Marketing, Purchasing, Human Resources, Shipping, IT, Executive, Public
Relations, Sales, Finance, and Accounting. '
;

COMMENT ON COLUMN DEPARTMENTS.MANAGER_ID IS 'Manager_id of a department. Foreign key to employee_id column of employees table. The manager_id column of the employee table references this column.'
;

COMMENT ON COLUMN DEPARTMENTS.LOCATION_ID IS 'Location id where a department is located. Foreign key to location_id column of locations table.'
;

CREATE INDEX DEPT_LOCATION_IX ON DEPARTMENTS
    (
     LOCATION_ID ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE UNIQUE INDEX DEPT_ID_PKX ON DEPARTMENTS
    (
     DEPARTMENT_ID ASC
    )
;

ALTER TABLE DEPARTMENTS
    ADD CONSTRAINT DEPT_ID_PK PRIMARY KEY ( DEPARTMENT_ID ) ;



CREATE TABLE EMPLOYEES
    (
     EMPLOYEE_ID NUMBER (6)  NOT NULL ,
     FIRST_NAME VARCHAR2 (20 BYTE) ,
     LAST_NAME VARCHAR2 (25 BYTE)  NOT NULL ,
     EMAIL VARCHAR2 (25 BYTE)  NOT NULL ,
     PHONE_NUMBER VARCHAR2 (20 BYTE) ,
     HIRE_DATE DATE  NOT NULL ,
     JOB_ID VARCHAR2 (10 BYTE)  NOT NULL ,
     SALARY NUMBER (8,2) ,
     COMMISSION_PCT NUMBER (2,2) ,
     MANAGER_ID NUMBER (6) ,
     DEPARTMENT_ID NUMBER (4)
    ) LOGGING
;



ALTER TABLE EMPLOYEES
    ADD CONSTRAINT EMP_SALARY_MIN
    CHECK ( salary > 0)
;


COMMENT ON COLUMN EMPLOYEES.EMPLOYEE_ID IS 'Primary key of employees table.'
;

COMMENT ON COLUMN EMPLOYEES.FIRST_NAME IS 'First name of the employee. A not null column.'
;

COMMENT ON COLUMN EMPLOYEES.LAST_NAME IS 'Last name of the employee. A not null column.'
;

COMMENT ON COLUMN EMPLOYEES.EMAIL IS 'Email id of the employee'
;

COMMENT ON COLUMN EMPLOYEES.PHONE_NUMBER IS 'Phone number of the employee; includes country code and area code'
;

COMMENT ON COLUMN EMPLOYEES.HIRE_DATE IS 'Date when the employee started on this job. A not null column.'
;

COMMENT ON COLUMN EMPLOYEES.JOB_ID IS 'Current job of the employee; foreign key to job_id column of the
jobs table. A not null column.'
;

COMMENT ON COLUMN EMPLOYEES.SALARY IS 'Monthly salary of the employee. Must be greater
than zero (enforced by constraint emp_salary_min)'
;

COMMENT ON COLUMN EMPLOYEES.COMMISSION_PCT IS 'Commission percentage of the employee; Only employees in sales
department elgible for commission percentage'
;

COMMENT ON COLUMN EMPLOYEES.MANAGER_ID IS 'Manager id of the employee; has same domain as manager_id in
departments table. Foreign key to employee_id column of employees table.
(useful for reflexive joins and CONNECT BY query)'
;

COMMENT ON COLUMN EMPLOYEES.DEPARTMENT_ID IS 'Department id where employee works; foreign key to department_id
column of the departments table'
;

CREATE INDEX EMP_DEPARTMENT_IX ON EMPLOYEES
    (
     DEPARTMENT_ID ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE INDEX EMP_JOB_IX ON EMPLOYEES
    (
     JOB_ID ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE INDEX EMP_MANAGER_IX ON EMPLOYEES
    (
     MANAGER_ID ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE INDEX EMP_NAME_IX ON EMPLOYEES
    (
     LAST_NAME ASC ,
     FIRST_NAME ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE UNIQUE INDEX EMP_EMP_ID_PKX ON EMPLOYEES
    (
     EMPLOYEE_ID ASC
    )
;

ALTER TABLE EMPLOYEES
    ADD CONSTRAINT EMP_EMP_ID_PK PRIMARY KEY ( EMPLOYEE_ID ) ;

ALTER TABLE EMPLOYEES
    ADD CONSTRAINT EMP_EMAIL_UK UNIQUE ( EMAIL ) ;



CREATE TABLE JOBS
    (
     JOB_ID VARCHAR2 (10 BYTE)  NOT NULL ,
     JOB_TITLE VARCHAR2 (35 BYTE)  NOT NULL ,
     MIN_SALARY NUMBER (6) ,
     MAX_SALARY NUMBER (6)
    ) LOGGING
;



COMMENT ON COLUMN JOBS.JOB_ID IS 'Primary key of jobs table.'
;

COMMENT ON COLUMN JOBS.JOB_TITLE IS 'A not null column that shows job title, e.g. AD_VP, FI_ACCOUNTANT'
;

COMMENT ON COLUMN JOBS.MIN_SALARY IS 'Minimum salary for a job title.'
;

COMMENT ON COLUMN JOBS.MAX_SALARY IS 'Maximum salary for a job title'
;

CREATE UNIQUE INDEX JOB_ID_PKX ON JOBS
    (
     JOB_ID ASC
    )
;

ALTER TABLE JOBS
    ADD CONSTRAINT JOB_ID_PK PRIMARY KEY ( JOB_ID ) ;



CREATE TABLE JOB_HISTORY
    (
     EMPLOYEE_ID NUMBER (6)  NOT NULL ,
     START_DATE DATE  NOT NULL ,
     END_DATE DATE  NOT NULL ,
     JOB_ID VARCHAR2 (10 BYTE)  NOT NULL ,
     DEPARTMENT_ID NUMBER (4)
    ) LOGGING
;



ALTER TABLE JOB_HISTORY
    ADD CONSTRAINT JHIST_DATE_CHECK
    CHECK (end_date > start_date)
        INITIALLY IMMEDIATE
        ENABLE
        VALIDATE
;


COMMENT ON COLUMN JOB_HISTORY.EMPLOYEE_ID IS 'A not null column in the complex primary key employee_id+start_date.
Foreign key to employee_id column of the employee table'
;

COMMENT ON COLUMN JOB_HISTORY.START_DATE IS 'A not null column in the complex primary key employee_id+start_date.
Must be less than the end_date of the job_history table. (enforced by
constraint jhist_date_interval)'
;

COMMENT ON COLUMN JOB_HISTORY.END_DATE IS 'Last day of the employee in this job role. A not null column. Must be
greater than the start_date of the job_history table.
(enforced by constraint jhist_date_interval)'
;

COMMENT ON COLUMN JOB_HISTORY.JOB_ID IS 'Job role in which the employee worked in the past; foreign key to
job_id column in the jobs table. A not null column.'
;

COMMENT ON COLUMN JOB_HISTORY.DEPARTMENT_ID IS 'Department id in which the employee worked in the past; foreign key to deparment_id column in the departments table'
;

CREATE INDEX JHIST_JOB_IX ON JOB_HISTORY
    (
     JOB_ID ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE INDEX JHIST_EMP_IX ON JOB_HISTORY
    (
     EMPLOYEE_ID ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE INDEX JHIST_DEPT_IX ON JOB_HISTORY
    (
     DEPARTMENT_ID ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE UNIQUE INDEX JHIST_ID_DATE_PKX ON JOB_HISTORY
    (
     EMPLOYEE_ID ASC ,
     START_DATE ASC
    )
;

ALTER TABLE JOB_HISTORY
    ADD CONSTRAINT JHIST_ID_DATE_PK PRIMARY KEY ( EMPLOYEE_ID, START_DATE ) ;



CREATE TABLE LOCATIONS
    (
     LOCATION_ID NUMBER (4)  NOT NULL ,
     STREET_ADDRESS VARCHAR2 (40 BYTE) ,
     POSTAL_CODE VARCHAR2 (12 BYTE) ,
     CITY VARCHAR2 (30 BYTE)  NOT NULL ,
     STATE_PROVINCE VARCHAR2 (25 BYTE) ,
     COUNTRY_ID CHAR (2 BYTE)
    ) LOGGING
;



COMMENT ON COLUMN LOCATIONS.LOCATION_ID IS 'Primary key of locations table'
;

COMMENT ON COLUMN LOCATIONS.STREET_ADDRESS IS 'Street address of an office, warehouse, or production site of a company.
Contains building number and street name'
;

COMMENT ON COLUMN LOCATIONS.POSTAL_CODE IS 'Postal code of the location of an office, warehouse, or production site
of a company. '
;

COMMENT ON COLUMN LOCATIONS.CITY IS 'A not null column that shows city where an office, warehouse, or
production site of a company is located. '
;

COMMENT ON COLUMN LOCATIONS.STATE_PROVINCE IS 'State or Province where an office, warehouse, or production site of a
company is located.'
;

COMMENT ON COLUMN LOCATIONS.COUNTRY_ID IS 'Country where an office, warehouse, or production site of a company is
located. Foreign key to country_id column of the countries table.'
;

CREATE INDEX LOC_CITY_IX ON LOCATIONS
    (
     CITY ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE INDEX LOC_STATE_PROV_IX ON LOCATIONS
    (
     STATE_PROVINCE ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE INDEX LOC_COUNTRY_IX ON LOCATIONS
    (
     COUNTRY_ID ASC
    )
    LOGGING
    NOCOMPRESS
    NOPARALLEL
;

CREATE UNIQUE INDEX LOC_ID_PKX ON LOCATIONS
    (
     LOCATION_ID ASC
    )
;

ALTER TABLE LOCATIONS
    ADD CONSTRAINT LOC_ID_PK PRIMARY KEY ( LOCATION_ID ) ;



CREATE TABLE REGIONS
    (
     REGION_ID NUMBER  NOT NULL ,
     REGION_NAME VARCHAR2 (25 BYTE)
    ) LOGGING
;



CREATE UNIQUE INDEX REG_ID_PKX ON REGIONS
    (
     REGION_ID ASC
    )
;

ALTER TABLE REGIONS
    ADD CONSTRAINT REG_ID_PK PRIMARY KEY ( REGION_ID ) ;




ALTER TABLE COUNTRIES
    ADD CONSTRAINT COUNTR_REG_FK FOREIGN KEY
    (
     REGION_ID
    )
    REFERENCES REGIONS
    (
     REGION_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE DEPARTMENTS
    ADD CONSTRAINT DEPT_LOC_FK FOREIGN KEY
    (
     LOCATION_ID
    )
    REFERENCES LOCATIONS
    (
     LOCATION_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE DEPARTMENTS
    ADD CONSTRAINT DEPT_MGR_FK FOREIGN KEY
    (
     MANAGER_ID
    )
    REFERENCES EMPLOYEES
    (
     EMPLOYEE_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE EMPLOYEES
    ADD CONSTRAINT EMP_DEPT_FK FOREIGN KEY
    (
     DEPARTMENT_ID
    )
    REFERENCES DEPARTMENTS
    (
     DEPARTMENT_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE EMPLOYEES
    ADD CONSTRAINT EMP_JOB_FK FOREIGN KEY
    (
     JOB_ID
    )
    REFERENCES JOBS
    (
     JOB_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE EMPLOYEES
    ADD CONSTRAINT EMP_MANAGER_FK FOREIGN KEY
    (
     MANAGER_ID
    )
    REFERENCES EMPLOYEES
    (
     EMPLOYEE_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE JOB_HISTORY
    ADD CONSTRAINT JHIST_DEPT_FK FOREIGN KEY
    (
     DEPARTMENT_ID
    )
    REFERENCES DEPARTMENTS
    (
     DEPARTMENT_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE JOB_HISTORY
    ADD CONSTRAINT JHIST_EMP_FK FOREIGN KEY
    (
     EMPLOYEE_ID
    )
    REFERENCES EMPLOYEES
    (
     EMPLOYEE_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE JOB_HISTORY
    ADD CONSTRAINT JHIST_JOB_FK FOREIGN KEY
    (
     JOB_ID
    )
    REFERENCES JOBS
    (
     JOB_ID
    )
    NOT DEFERRABLE
;



ALTER TABLE LOCATIONS
    ADD CONSTRAINT LOC_C_ID_FK FOREIGN KEY
    (
     COUNTRY_ID
    )
    REFERENCES COUNTRIES
    (
     COUNTRY_ID
    )
    NOT DEFERRABLE
;


CREATE OR REPLACE VIEW EMP_DETAILS_VIEW AS
SELECT
  e.employee_id,
  e.job_id,
  e.manager_id,
  e.department_id,
  d.location_id,
  l.country_id,
  e.first_name,
  e.last_name,
  e.salary,
  e.commission_pct,
  d.department_name,
  j.job_title,
  l.city,
  l.state_province,
  c.country_name,
  r.region_name
FROM
  employees e,
  departments d,
  jobs j,
  locations l,
  countries c,
  regions r
WHERE e.department_id = d.department_id
  AND d.location_id = l.location_id
  AND l.country_id = c.country_id
  AND c.region_id = r.region_id
  AND j.job_id = e.job_id
WITH READ ONLY ;




CREATE SEQUENCE DEPARTMENTS_SEQ
    INCREMENT BY 10
    MAXVALUE 9990
    MINVALUE 1
    NOCACHE
;


CREATE SEQUENCE EMPLOYEES_SEQ
    INCREMENT BY 1
    MAXVALUE 9999999999999999999999999999
    MINVALUE 1
    NOCACHE
;


CREATE SEQUENCE LOCATIONS_SEQ
    INCREMENT BY 100
    MAXVALUE 9900
    MINVALUE 1
    NOCACHE
;


CREATE OR REPLACE TRIGGER SECURE_EMPLOYEES
    BEFORE INSERT OR UPDATE OR DELETE ON EMPLOYEES
    FOR EACH ROW
BEGIN
  secure_dml;
END secure_employees;
/



CREATE OR REPLACE TRIGGER UPDATE_JOB_HISTORY
    AFTER UPDATE OF JOB_ID, DEPARTMENT_ID ON EMPLOYEES
    FOR EACH ROW
BEGIN
  add_job_history(:old.employee_id, :old.hire_date, sysdate,
                  :old.job_id, :old.department_id);
END;
/




CREATE OR REPLACE PROCEDURE add_job_history
  (  p_emp_id          job_history.employee_id%type
   , p_start_date      job_history.start_date%type
   , p_end_date        job_history.end_date%type
   , p_job_id          job_history.job_id%type
   , p_department_id   job_history.department_id%type
   )
IS
BEGIN
  INSERT INTO job_history (employee_id, start_date, end_date,
                           job_id, department_id)
    VALUES(p_emp_id, p_start_date, p_end_date, p_job_id, p_department_id);
END add_job_history;
/


CREATE OR REPLACE PROCEDURE secure_dml
IS
BEGIN
  IF TO_CHAR (SYSDATE, 'HH24:MI') NOT BETWEEN '08:00' AND '18:00'
        OR TO_CHAR (SYSDATE, 'DY') IN ('SAT', 'SUN') THEN
RAISE_APPLICATION_ERROR (-20205,
'You may only make changes during normal office hours');
  END IF;
END secure_dml;
/



----------------

INSERT INTO regions VALUES ('1','REGIONONE');
INSERT INTO regions VALUES ('2','REGIONTWO');
INSERT INTO regions VALUES ('3','REGIONTHREE');
INSERT INTO regions VALUES ('4','REGIONFOUR');

INSERT INTO countries VALUES ('1','COUNTRYONE','1');
INSERT INTO countries VALUES ('2','COUNTRYONE','2');
INSERT INTO countries VALUES ('3','COUNTRYONE','3');
INSERT INTO countries VALUES ('4','COUNTRYONE','4');

INSERT INTO locations VALUES ('1','STREETADDRESSSONE','1','CITYONE','STATEPROVISION1','1');
INSERT INTO locations VALUES ('2','STREETADDRESSSTWO','1','CITYONE','STATEPROVISION1','2');
INSERT INTO locations VALUES ('3','STREETADDRESSSTHREE','1','CITYONE','STATEPROVISION1','3');
INSERT INTO locations VALUES ('4','STREETADDRESSSFOUR','1','CITYONE','STATEPROVISION1','4');


INSERT INTO jobs VALUES ('1','JOBTITLEONE','100','1000');
INSERT INTO jobs VALUES ('2','JOBTITLEONE','200','2000');
INSERT INTO jobs VALUES ('3','JOBTITLEONE','300','3000');
INSERT INTO jobs VALUES ('4','JOBTITLEONE','400','4000');

insert into departments values ('1','DEPARTMENTNAME1','','');
insert into departments values ('2','DEPARTMENTNAME2','','');
insert into departments values ('3','DEPARTMENTNAME3','','');
insert into departments values ('4','DEPARTMENTNAME4','','');

insert into employees values ('1','Siddharatha','Dhumale','shdhumale@gmail.com','1234','01-NOV-15','1','999','','1','1');
insert into employees values ('2','Siddharatha2','Dhumale2','shdhumale2@gmail.com','1234','01-NOV-15','2','999','','1','2');
insert into employees values ('3','Siddharatha3','Dhumale3','shdhumale3@gmail.com','1234','01-NOV-15','3','999','','1','3');
insert into employees values ('4','Siddharatha4','Dhumale4','shdhumale4@gmail.com','1234','01-NOV-15','4','999','','1','4');

insert into employees values ('5','Siddharatha4','Dhumale4','shdhumale5@gmail.com','1234','01-NOV-15','4','26','','1','2');
insert into employees values ('6','Siddharatha4','Dhumale4','shdhumale6@gmail.com','1234','01-NOV-15','4','60','','1','2');

insert into employees values ('7','Siddharatha4','Dhumale4','shdhumale7@gmail.com','1234','01-NOV-15','4','600','','1','2');


INSERT INTO job_history VALUES ('1','01-NOV-15','19-NOV-15','1','1');
INSERT INTO job_history VALUES ('2','01-NOV-15','19-NOV-15','2','2');
INSERT INTO job_history VALUES ('3','01-NOV-15','19-NOV-15','3','3');
INSERT INTO job_history VALUES ('4','01-NOV-15','19-NOV-15','4','4');

Step -2 -

Download the Eclipse with Oracle ADF plugin from the below path.

http://www.oracle.com/technetwork/developer-tools/eclipse/downloads/index.html

Step -3- For execution of the Oracle ADF programe we would also need Weblogic Server. We can download the separate Weblogc for the same.

But I would prefer you to download the complete set of Oracle JDeveloper 11g (11.1.1.7.1) for Java and ADF deployment on Oracle JCS for SaaS Cloud instances from below link

http://www.oracle.com/technetwork/developer-tools/jdev/downloads/index.html

During installation it will ask you to install WebLogic server also. Please give the proper path to installed Web logic server.

Finally Please follow below given link. This will give you step wise creation of Oracle ADF using Eclispe.

http://www.oracle.com/webfolder/technetwork/eclipse/adf/gettingStarted/Tutorial/ADFwithOEPE_1.html

For Performing the same operation using JDeveloper use below link
http://www.oracle.com/webfolder/technetwork/tutorials/obe/jdev/obe11jdev/ps1/ria_application/developriaapplication_long.htm

Friday, November 13, 2015

How to configure JMeter to send JMS message to ActiveMQ

Download Jmeter from following site http://jmeter.apache.org/download_jmeter.cgi. Extract the zip and execute below command to see the ui for JMeter
C:\apache-jmeter-2.13\bin>jmeter

Step -1:- Make sure to add activemq-all-5.12.1 or your latest version jar in lib folder of JMeter
Step -2:- Configure Point-to-point queue as shown below
Image_8
Image_9


Step 3- Finaly check the Active MQ graph and you will find the message inside it.
Image_10

Thursday, November 12, 2015

Now lets discuss about Asynchronous Receiver in Java Messaging Service (JMS)

One of the great benefit of receiving message asychronous is that receiver take care of receiving the message on its own as soon as the sender put the message in the queue.


package com.siddhu.receiver;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.MessageListener;
import javax.jms.JMSException;
import javax.jms.ExceptionListener;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class AsyncReceiver implements MessageListener, ExceptionListener
{
    static QueueConnection queueConn = null;
    public static void main(String[] args) throws Exception
    {
    Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
env.put(Context.PROVIDER_URL, "tcp://localhost:61616");
env.put("queue.queueSampleQueue","MyNewQueue");

       // get the initial context
       InitialContext ctx = new InitialContext(env);

       // lookup the queue object
       Queue queue = (Queue) ctx.lookup("queueSampleQueue");

       // lookup the queue connection factory
       QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");

       // create a queue connection
       queueConn = connFactory.createQueueConnection();

       // create a queue session
       QueueSession queueSession = queueConn.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);

       // create a queue receiver
       QueueReceiver queueReceiver = queueSession.createReceiver(queue);

       // set an asynchronous message listener
       AsyncReceiver asyncReceiver = new AsyncReceiver();
       queueReceiver.setMessageListener(asyncReceiver);

       // set an asynchronous exception listener on the connection
       queueConn.setExceptionListener(asyncReceiver);

       // start the connection
       queueConn.start();
    }

    /**
       This method is called asynchronously by JMS when a message arrives
       at the queue. Client applications must not throw any exceptions in
       the onMessage method.
       @param message A JMS message.
     */
    public void onMessage(Message message)
    {
       TextMessage msg = (TextMessage) message;
       try {
      if(msg.getText().equals("exit")){
      queueConn.close();
      System.out.println("Application Exits");
      }else{
      System.out.println("received: " + msg.getText());
      }
       } catch (JMSException ex) {
          ex.printStackTrace();
       }
    }

    /**
       This method is called asynchronously by JMS when some error occurs.
       When using an asynchronous message listener it is recommended to use
       an exception listener also since JMS have no way to report errors
       otherwise.
       @param exception A JMS exception.
     */
    public void onException(JMSException exception)
    {
       System.err.println("an error occurred: " + exception);
    }
}

Send your message using Sender.java and also start your AsyncReceiver which will remaing in start mode until it is explicitly closed.

package com.siddhu.sender;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSender;
import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;

public class Sender {

public static void main(String[] args) throws Exception {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
env.put(Context.PROVIDER_URL, "tcp://localhost:61616");
env.put("queue.queueSampleQueue", "MyNewQueue");

// get the initial context
InitialContext ctx = new InitialContext(env);

// lookup the queue object
Queue queue = (Queue) ctx.lookup("queueSampleQueue");
// lookup the queue connection factory
QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
// create a queue connection
QueueConnection queueConn = connFactory.createQueueConnection();

// create a queue session
QueueSession queueSession = queueConn.createQueueSession(false,Session.DUPS_OK_ACKNOWLEDGE);

// create a queue sender
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

// create a simple message to say "Hello this is siddharatha"
TextMessage message = queueSession.createTextMessage("Hello this is siddharathass");

// send the message
queueSender.send(message);

System.out.println("sent: " + message.getText());

queueConn.close();
}
}


Now try to send the multiple message and  you will see message is automtically received at the receiver end.