Monday, April 16, 2012

How to find top three highest salary in emp table using sql


The top three highest salary in emp table using sql is

SELECT *FROM ( SELECT *FROM emp ORDER BY Salary desc ) WHERE rownum <= 3;


Sunday, April 15, 2012

Java 1.5 Feature : Varargs


In past releases, a method that took an arbitrary number of values required you to create an array and put the values into the array prior to invoking the method.

It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process. Furthermore, it is upward compatible with preexisting APIs.

Public static String format(String pattern,
Object... arguments);
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position.

Much more simpler example here..

public class VarArgs {
public static void main(String[] args) {
String[] newArgs = {"a", "b", "c"};
vaMethod(newArgs);
}

public void vaMethod(String[] args) {
System.out.println("You gave me " + args.length + " args! Yay.");
}
}
You can declare it more easily, and not have to construct the array ahead of time:

public class VarArgs {
public static void main(String[] args) {
vaMethod("a", "b", "c");
}

public void vaMethod(String... args) {
System.out.println("You gave me " + args.length + " args! Yay.");
}
}


Java 1.5 Language Features


Generics

This long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting. Refer to JSR 14.

Enhanced for Loop

This new language construct eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays. Refer to JSR 201 .

Autoboxing/Unboxing

This facility eliminates the drudgery of manual conversion between primitive types (such as int) and wrapper types (such as Integer). Refer to JSR 201 .

Typesafe Enums

This flexible object-oriented enumerated type facility allows you to create enumerated types with arbitrary methods and fields. It provides all the benefits of the Typesafe Enum pattern ("Effective Java," Item 21) without the verbosity and the error-proneness. Refer to JSR 201.

Varargs

This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists. Refer to JSR 201.

Static Import

This facility lets you avoid qualifying static members with class names without the shortcomings of the "Constant Interface antipattern." Refer to JSR 201.

Metadata (Annotations)

This language feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a "declarative" programming style where the programmer says what should be done and tools emit the code to do it. Also it eliminates the need for maintaining "side files" that must be kept up to date with changes in source files. Instead the information can be maintained in the source file. Refer to JSR 175.


Friday, April 13, 2012

Client side web site performance


Smaller images,
Omitting quotes in HTML attributes (yes, people went that far!),
Using fixed width tables in IE 5
Balancing server response buffering vs creating the whole page first and then sending it all in one response, etc.

Putting images on different domains (though now other resources are worth considering too)
Compressing the server output using gzip (a bit hit and miss on earlier versions of IIS though)
Cache control using the Expires header
Use CSS (though in those days I used it mostly for font definitions and browser sniffing was needed to load different CSS files!)


Use GZip on all text-based output (which should be optimized anyway via web standards!)

Try to reduce the number of HTTP requests (e.g. combine CSS and JavaScript files during development or during the build process, use CSS sprites, if possible etc)
Help browsers ensure they can cache as many of your files as possible

Let browsers download your page resources in parallel even though HTTP 1.1 limits you to 2 concurrent requests from a domain (through sub-domains, minimizing or eliminating loading CSS and JavaScript in a way that blocks the browser, etc)

Disperse your content through things like content distribution and extra sub-domains (though be aware of extra DNS-lookups that might result)


Diffrence between High Level Desgin and Low Level Design


High level design means abstract view of the system.
Details are not shown.
High level design does contain class diagram at conceptual level no operations are defined.

Low level design uses class diagram at implementation level with most of the required detail.

The main difference is the amount of detail that you show.


What are Entity Relationship Diagrams


Entity Relationship Diagrams (ERDs) illustrate the logical structure of databases.

Entity Relationship Diagram Notations


Entity
An entity is an object or concept about which you want to store information.

Entity

Weak Entity
A weak entity is an entity that must defined by a foreign key relationship with another entity as it cannot be uniquely identified by its own attributes alone.

Weak Entity

Key attribute
A key attribute is the unique, distinguishing characteristic of the entity. For example, an employee's social security number might be the employee's key attribute.
Key attribute

Multivalued attribute
A multivalued attribute can have more than one value. For example, an employee entity can have multiple skill values.
Multivalued attribute

Derived attribute
A derived attribute is based on another attribute. For example, an employee's monthly salary is based on the employee's annual salary.

Derived attribute
Relationships
Relationships illustrate how two entities share information in the database structure.
Relationships


Cardinality
Cardinality specifies how many instances of an entity relate to one instance of another entity.
Cardinality
Ordinality is also closely linked to cardinality. While cardinality specifies the occurences of a relationship, ordinality describes the relationship as either mandatory or optional. In other words, cardinality specifies the maximum number of relationships and ordinality specifies the absolute minimum number of relationships.


Recursive relationship
In some cases, entities can be self-linked. For example, employees can supervise other employees.

Recursive relationship


What is Scalability Testing


Definition

A test designed to prove that both the functionality and the performance of a system will scale up to meet specified requirements.

Comment

Generally, load and stress tests should continue to the point where the system fails and its scalability is proven.

Measurements should be taken during the load and stress testing activities to ensure that the most information possible is available to make a decision on the scalability of the system. The more information that is available, the less risk there is of an unexpected event happening in the future that could turn out to be costly


Difference between Layer and Tier


Layer and Tier are really two very different things:

Layers are a means of logical separation, and are an architectural pattern to separate concerns. Typical layers are the storage / data layer, the data access layer, a business logic layer, and a presentation layer. When deploying an application, these layers might exist either on one machine, or on multiple machines.

When architecting the layers of an application, one must consider various factors:

What is this layer responsible for?
Will this layer translate to a tier at any point?
Are other applications expected to communicate with this layer?
What are the dependencies for this layer?
Tiers are the physical separation of an application. Typically, a regular web application will have a data tier which maps to the database, a business and presentation tier (potentially together) that map to the underlying business logic and web pages respectively, and a front-end tier that maps to the browser. There are scenarios wherein the business and presentation layers are split, so the business layer components reside on a separate node, and the web application itself resides on another node. Tiers necessarily imply layers, but not vice versa.

While layer decisions are driven primarily by separation of concerns, a tiering decision is driven differently. In fact decisions around tiers need to be well thought-out, especially since physical tiers may result in a performance degrade, due to network latency, and marshalling / unmarshalling overheads. For the most part, having the data sit in a separate tier on a database server is fairly well accepted, but the decision of whether or not to separate web pages into a tier separate from business logic components is a question that is often debated. A few points that support this separation are:

If the web server is expected to serve up a large amount of static content, it is more feasible to scale out the web servers independent of the business logic processing, and hence keep them on separate tiers.
If the processing of the businss logic requires a significant amount of processing, and this acts as a bottleneck for the rest of the web page processing, then it is more feasible to scale out the application servers independent of the web servers, and hence keep them on separate tiers.
If the functionality encapsulated by the business logic components is expected to be consumed by clients other than the presentation layer, then it is better to have these hosted on a separate tier, and front-end it with a web service / WCF facade.
If there is a need to have more security for the business functionality, then it is better to host the businss logic components in a separate tier, and separate it from the web server using a firewall.
If, on the other hand, the intent is only to scale out, without a deep need to scale out business and presentation tiers independently, then keeping them in one tier is better: Consider the case when I have five machines, and I use all five to host both my presentation and business tiers. In comparison, if I use two of these for my presentation tier, and three for my business tier, then I still have the same compute power, but now have to contend with the additional overhead of marshal/unmarshal, and latency. I’m hence better off with the single tier in such a case.

All said though, while a physical separation improves scale-out capability and security, it does impose overheads due to the network. The design for tiers needs to take into account the communication between tiers – too chatty, and performance degrades rapidly. The choice of protocol is equally important – the protocol of communication should be the most lightweight, and be as native as possible – so if both the web pages and the business logic components are built on the .Net stack, then a NetTcp binding is more suitable than a Http-based binding.


Thursday, April 12, 2012

Decorator Design Pattern in Java


In this pattern(Decorator Design pattern), a decorator object is wrapped around the original object. This is typically achieved having the original object as a member of the decorator, the decorator object also provides a way to implement the new functionality. The decorator must conform to the interface of the original object (the object being decorated).

The definition of Decorator Design pattern is :

Attach additional responsibilities to an object dynamically. Decorators
provide a flexible alternative to subclassing for extending functionality.

Where to implement-

For example I have an Interface and its implementation class, and if i want to provide an extra functionality with out effecting the original interface and implementation class.

Also we should able to withdraw the extra functionality or responsibility that we are providing dynamically.

For Example: The java.util.Collections.unmodifiableCollection(Collection) removies the ability to change a given collection by wrapping it with a decorator that throws an UnSupportedException when you try to modify the Collection.

example..

Have an interface IComponent, with one method getDetails();
Also I have an implementation class Component for IComponent

If I have to provide extra functionality with out effecting the above 2 I can do the following..

Extend the IComponent With IDecorator interface which has the extra method declared doSomething()

that is

public interface IDecorator extends IComponent {
public void doSomething();
}

and provide the implementation to the IDecorator which has two methods getDetails, doSomething.

public class Decorator implements IDecorator {

IComponent component;

public Decorator(IComponent component) {
super();
this.component = component;
}

public void doSomething() {
System.out.println("Decorator does some stuff too");

}

public void getDetails() {
component.getDetails();
doSomething();

}

}


Here, with the same getDetails() of IComponent, we are achieving the extra functionality.


Here is the client class


public class Client {
public static void main(String[] args) {
IComponent comp = new Component();
IDecorator decorator = new Decorator(comp);
decorator.doStuff();
}

}


Difference between Comparable and Comparator?


Comparable-

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator-

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.

Usage-

There are two interfaces in Java to support these concepts, and each of these has one method to be implemented by user.
Those are;

java.lang.Comparable: int compareTo(Object o1)
This method compares this object with o1 object. Returned int value has the following meanings.
positive – this object is greater than o1
zero – this object equals to o1
negative – this object is less than o1

java.util.Comparator: int compare(Object o1, Objecto2)
This method compares o1 and o2 objects. Returned int value has the following meanings.
positive – o1 is greater than o2
zero – o1 equals to o2
negative – o1 is less than o2

java.util.Collections.sort(List) and java.util.Arrays.sort(Object[]) methods can be used to sort using natural ordering of objects.
java.util.Collections.sort(List, Comparator) and java.util.Arrays.sort(Object[], Comparator) methods can be used if a Comparator is available for comparison.