Tuesday 24 May 2016

Get actual file Content type after extension change - java

To identify the exact file content type, not go by extensions. Because there could be a scenario where file can be uploaded by changing the extesions.

eg: test.jpg file is renamed to test.jpg.txt - browser shows this file content type as text/plain. But the actual file is of image/jpeg.

This can be handled by below code:

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

public class ActualContentType {
    public static void main(String[] args) throws Exception {

        Boolean status = isJPEG(new File("C:\\test.txt.jpg"));
        System.out.println("Status: " + status);
    }

    //http://www.filesignatures.net/index.php?page=search&search=FFD8FFE0&mode=SIG    

private static Boolean isJPEG(File filename) throws Exception {
        DataInputStream ins = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)));
        try {
            if (ins.readInt() == 0xffd8ffe0) {
                return true;
            } else {
                return false;
            }
        } finally {
            ins.close();
        }
    }
}

Friday 6 May 2016

SQL - Having Clause

The HAVING clause enables you to specify conditions that filter which group results appear in the final results.
The WHERE clause places conditions on the selected columns, whereas the HAVING clause places conditions on groups created by the GROUP BY clause.
Syntax:
The following is the position of the HAVING clause in a query:
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
The HAVING clause must follow the GROUP BY clause in a query and must also precede the ORDER BY clause if used. The following is the syntax of the SELECT statement, including the HAVING clause:
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
Example:
Consider the CUSTOMERS table having the following records:
+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+
Following is the example, which would display record for which similar age count would be more than or equal to 2:
SQL > SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 2;
This would produce the following result:
+----+--------+-----+---------+---------+
| ID | NAME   | AGE | ADDRESS | SALARY  |
+----+--------+-----+---------+---------+
|  2 | Khilan |  25 | Delhi   | 1500.00 |
+----+--------+-----+---------+---------

Steps to a complete understanding of SQL

What are the top 3 average government debts in percent of the GDP for those countries whose GDP per capita was over 40’000 dollars in every year in the last four years
Whew. Some (academic) business requirements.
In SQL (PostgreSQL dialect), we would write:
select code, avg(govt_debt)
from countries
where year > 2010
group by code
having min(gdp_per_capita) >= 40000
order by 2 desc
limit 3
Or, with inline comments
-- The average government debt
select code, avg(govt_debt)

-- for those countries
from countries

-- in the last four years
where year > 2010

-- yepp, for the countries
group by code

-- whose GDP p.c. was over 40'000 in every year
having min(gdp_per_capita) >= 40000

-- The top 3
order by 2 desc
limit 3

The result being:

code     avg
-----------------
JP    193.00
US     91.95

DE     56.00

1.    FROM generates the data set
2.    WHERE filters the generated data set
3.    GROUP BY aggregates the filtered data set
4.    HAVING filters the aggregated data set
5.    SELECT transforms the filters aggregated data set
6.    ORDER BY sorts the transformed data set

7.    LIMIT .. OFFSET frames the sorted data set

Sorting 2 columns in different order : SQL

To sort by multiple columns in SQL, and in different directions. column1 would be sorted descending, and column2 ascending.
Given the following People table:
 FirstName |  LastName   |  YearOfBirth
----------------------------------------
  Thomas   | Alva Edison |   1847
  Benjamin | Franklin    |   1706
  Thomas   | More        |   1478
  Thomas   | Jefferson   |   1826
If you execute the query below:
SELECT * FROM People ORDER BY FirstName DESC, YearOfBirth ASC
The result set will look like this:
 FirstName |  LastName   |  YearOfBirth
----------------------------------------
  Thomas   | More        |   1478
  Thomas   | Jefferson   |   1826
  Thomas   | Alva Edison |   1847

  Benjamin | Franklin    |   1706

Usage of ORDER BY for multiple columns - SQL

Sorting in an ORDER BY is done by the first column, and then by each additional column in the specified statement.
For instance, consider the following data:
Column1    Column2
=======    =======
1          Smith
2          Jones
1          Anderson
3          Andrews
The query
SELECT Column1, Column2 FROM thedata ORDER BY Column1, Column2
would first sort by all of the values in Column1
and then sort the columns by Column2 to produce this:
Column1    Column2
=======    =======
1          Anderson
1          Smith
2          Jones
3          Andrews
In other words, the data is first sorted in Column1 order, and then each subset (Column1 rows that have 1 as their value) are sorted in order of the second column.
The difference between the two statements you posted is that the rows in the first one would be sorted first by prod_price (price order, from lowest to highest), and then by order of name (meaning that if two items have the same price, the one with the lower alpha value for name would be listed first), while the second would sort in name order only (meaning that prices would appear in order based on the prod_name without regard for price).