second highest salary

Posted: June 18, 2012 in MySQL

we can get 2nd highest salary by inner query ,  not in , order by column desc limit 0,1

Create table named employee:

CREATE TABLE `employee` (

`eno` int(5) NOT NULL AUTO_INCREMENT,

`ename` varchar(50) DEFAULT NULL,

`sal` float DEFAULT NULL,

PRIMARY KEY (`eno`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

Insert the following records:

1              gopal       16000

2              sanjana   14000

3              prem      10000

Queries:

1) select sal from employee where sal = (select max(sal) from employee where sal < (select max(sal) from employee))

Ans: 14000

2) select max(sal) from employee where sal not in (select max(sal) from employee)

Ans: 14000

3)select sal from employee order by sal desc limit 1,1 (1 means 2nd highest means position ,1 no of records)

Ans: 14000

Highest salary in table using above query:

1)select sal from employee order by sal desc limit 0,1

Ans: 16000

2)select sal from employee order by sal desc limit 2,1

Ans: 10000

3) select sal from employee order by sal desc limit 0,2

Ans: 16000

14000

 

Advertisements

Filter Vs Interceptor

Posted: June 8, 2012 in struts

Filter: 

filter dynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be “attached” to any type of servlet or JSP page.

Filters can perform many different types of functions. We’ll discuss examples of the italicized items in this paper:

  •   Authentication-Blocking requests based on user identity.
  •   Logging and auditing-Tracking users of a web application.
  •   Image conversion-Scaling maps, and so on.
  •   Data compression-Making downloads smaller.
  •   Localization-Targeting the request and response to a particular locale.

Request Filters can:

  •    perform security checks
  •   reformat request headers or bodies
  •   audit or log requests

Response Filters can:

  •   Compress the response stream
  •   append or alter the response stream
  •   create a different response altogether

Examples that have been identified for this design are

  •   Authentication Filters
  •   Logging and Auditing Filters
  •   Image conversion Filters
  •   Data compression Filters
  •   Encryption Filters
  •   Tokenizing Filters
  •   Filters that trigger resource access events
  •   XSL/T filters
  •   Mime-type chain Filter

Interceptors

Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods in conjunction with method invocations or lifecycle events on an associated target class. Common uses of interceptors are logging, auditing, or profiling.

Interceptors can be defined within a target class as an interceptor method, or in an associated class called an interceptor class. Interceptor classes contain methods that are invoked in conjunction with the methods or lifecycle events of the target class.

1)  Cookie Interceptor

2)  Checkbox Interceptor

3)  FileUpload Interceptor

Difference:

A Servlet Filter is used in the web layer only, you can’t use it outside of a web context. Interceptors can be used anywhere. That’s the main difference.

for authentication of web pages you would use a servlet filter. For security stuff in your business layer or logging/bugtracing (a.k.a. independent of the web layer) you would use an Interceptor.

Apart from the fact that both Interceptors and filters are based on intercepting filter,there are few differences when it comes to Struts2.

Filters: (1)Based on Servlet Specification (2)Executes on the pattern matches on the request.(3) Not configurable method calls
Interceptors: (1)Based on Struts2. (2)Executes for all the request qualifies for a front controller( A Servlet filter ).And can be configured to execute additional interceptor for a particular action execution.(3)Methods in the Interceptors can be configured whether to execute or not by means of excludemethods or includeMethods

 

sql

Posted: June 7, 2012 in JDBC

 

SQL Statement Syntax
AND / OR SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
ALTER TABLE ALTER TABLE table_name
ADD column_name datatype

or

ALTER TABLE table_name
DROP COLUMN column_name

AS (alias) SELECT column_name AS column_alias
FROM table_name

or

SELECT column_name
FROM table_name  AS table_alias

BETWEEN SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
CREATE DATABASE CREATE DATABASE database_name
CREATE TABLE CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name2 data_type,

)
CREATE INDEX CREATE INDEX index_name
ON table_name (column_name)

or

CREATE UNIQUE INDEX index_name
ON table_name (column_name)

CREATE VIEW CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
DELETE DELETE FROM table_name
WHERE some_column=some_value

or

DELETE FROM table_name
(Note: Deletes the entire table!!)

DELETE * FROM table_name
(Note: Deletes the entire table!!)

DROP DATABASE DROP DATABASE database_name
DROP INDEX DROP INDEX table_name.index_name (SQL Server)
DROP INDEX index_name ON table_name (MS Access)
DROP INDEX index_name (DB2/Oracle)
ALTER TABLE table_name
DROP INDEX index_name (MySQL)
DROP TABLE DROP TABLE table_name
GROUP BY SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
IN SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)
INSERT INTO INSERT INTO table_name
VALUES (value1, value2, value3,….)

or

INSERT INTO table_name
(column1, column2, column3,…)
VALUES (value1, value2, value3,….)

INNER JOIN SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
LEFT JOIN SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
RIGHT JOIN SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
FULL JOIN SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
LIKE SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
ORDER BY SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]
SELECT SELECT column_name(s)
FROM table_name
SELECT * SELECT *
FROM table_name
SELECT DISTINCT SELECT DISTINCT column_name(s)
FROM table_name
SELECT INTO SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_table_name

or

SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_table_name

SELECT TOP SELECT TOP number|percent column_name(s)
FROM table_name
TRUNCATE TABLE TRUNCATE TABLE table_name
UNION SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
UNION ALL SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
UPDATE UPDATE table_name
SET column1=value, column2=value,…
WHERE some_column=some_value
WHERE SELECT column_name(s)
FROM table_name
WHERE column_name operator value



IllegalStateException

It is child class of Runtime Exception and its is unchecked. It is raised programmatically when ever a method has invoked at illegal or in appropriate time.

Example 1:

After invalidating a session we are not allowed to call getId() method, violation leads to  illegalThreadStateException.

session.invalidate();

session.getId(1);

output:

Exception in thread “main” java.lang.IllegalStateException


IllegalThreadStateException

It is child class of Runtime Exception and its is unchecked. It is raised programmatically when ever a method has invoked at illegal or in appropriate time.

Example 1:

After Starting a Thread we are not allowed to start the same thread again violation leads to illegalThreadStateException

public class Test extends Thread

{

public void run(){

System.out.println(“I am from run “);

}

public static void main(String[] args) {

Test test1 = new Test();

Thread thread = new Thread(test1);

thread.start();

thread.start();

}            }

output:

I am from run Exception in thread “main”
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:595)
at com.clarity.test1.main(test1.java:108)

 


Number Format Exception

 It is child class of illegalArgumentException and it is unchecked.

It is thrown programmatically to indicate that the application has attempted to convert a string to number but the string is not format properly

Example:

public static void main(String[] args) {

      Integer integer = new Integer(“10”);

      System.out.println(integer);

      Integer integer2 = new Integer(“ten”); —–> string to number but the string is  not format properly.                     

      System.out.println(integer2);

            }}

Output:

10

Exception in thread “main” java.lang.NumberFormatException: For input string: “ten”

      at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

      at java.lang.Integer.parseInt(Integer.java:447)

      at java.lang.Integer.<init>(Integer.java:620)

            at com.clarity.test1.main(test1.java:97)


IllegalArgumentException:

It is the child class of Runtime Exception and it is unchecked. Thrown by programmer or API developer to indicate that a method has invoked with an illegal or in appropriate argument.

It is not JVM Exception.

Example:

public class Test {

 public static void main(String[] args) {

Thread.currentThread().setPriority(100);   ———-> here possible values are 1 to 10. (except 1 to 10 throws illegalArguementException)

}}

Output:

Exception in thread “main” java.lang.IllegalArgumentException

at java.lang.Thread.setPriority(Thread.java:1015)

at com.clarity.test1.main(test1.java:95)