Wednesday, 6 December 2017

The SQL Query to find the data between the range(NUMBER,TEXT, DATE)

The SQL BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.

SELECT * FROM Display_Info WHERE Price BETWEEN 10 AND 20;

The above query gives the data like price between 10 and 20.

SQL Query to Find the Duplicate records form Table

SELECT name, partner, COUNT(*) FROM display_info GROUP BY name, partner HAVING COUNT(*) > 1

In above query display_info  is the table name. this query gives to the duplicate data and number of times repeated that data

Wednesday, 27 September 2017

The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

Example:
To find the recent joining employees in each department:

select * from emp order by deptno,hiredate desc;

Friday, 15 September 2017

Spring4Restful WebServices Annotations

Basically, @RestController , @RequestBody, ResponseEntity & @PathVariable are all you need to know to implement a REST API in Spring 4. Additionally, spring provides several support classes to help you implement something customized.

@RestController
--->@RestController : First of all, we are using Spring 4′s new @RestController annotation. This annotation eliminates the need of annotating each method with @ResponseBody. @RestController is itself annotated with @ResponseBody, and can be considered as combination of @Controller and @ResponseBody.

@RequestBody
--->@RequestBody : If a method parameter is annotated with @RequestBody, Spring will bind the incoming HTTP request body(for the URL mentioned in @RequestMapping for that method) to that parameter. While doing that, Spring will use HTTP Message converters to convert the HTTP request body into domain object [deserialize request body to domain object], based on ACCEPT or Content-Type header present in request.

@ResponseBody
--->@ResponseBody : If a method is annotated with @ResponseBody, Spring will bind the return value to outgoing HTTP response body. While doing that, Spring will use HTTP Message converters to convert the return value to HTTP response body, based on Content-Type present in request HTTP header.

@PathVariable
--->This annotation indicates that a method parameter should be bound to a URI template variable [the one in '{}'].

ResponseEntity
--->ResponseEntity is a real deal. It represents the entire HTTP response. Good thing about it is that you can control anything that goes into it. You can specify status code, headers, and body. It comes with several constructors to carry the information you want to sent in HTTP Response.

MediaType
--->MediaType : With @RequestMapping annotation, you can additionally, specify the MediaType to be produced or consumed (using produces or consumes attributes) by that particular controller method, to further narrow down the mapping.

---> Accept header says about what type client can understand. Content-Type header says what type of data actually 

Thursday, 14 September 2017

@Controller VS @RestController in Spring

The key difference between a traditional Spring MVC controller and the RESTful web service controller is the way the HTTP response body is created. While the traditional MVC controller relies on the View technology, the RESTful web service controller simply returns the object and the object data is written directly to the HTTP response as JSON/XML/TEXT

Saturday, 29 July 2017

Serialization and Deserialization Examaple (convert HashMap to encoded txt file and read that file to HashMap)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class SerializationTest {

/**
* HashMap converted .txt file and .txt file to HashMap
*/
public static void main(String[] args) {

HashMap mainMap=new HashMap<String,HashMap>();  //main hashmap

HashMap mainMap1=new HashMap<String,HashMap>();
 
HashMap<String, String> childMap=new HashMap<String,String>();
  childMap.put("DocName", "DocumentName");
  childMap.put("Language", "English");
  childMap.put("Status", "Success");
  childMap.put("Location", "D:/dhsgds/hjasdhjgs");
  childMap.put("Path", "D:/dhsgds/hjasdhjgs");

 mainMap.put("docID1", childMap);
 mainMap.put("docID2", childMap);

 try
          {
                 FileOutputStream fos =
                    new FileOutputStream("F:/Education/test.txt");
//The above hashmap converted to text file in that direcotry with encoded format
                 ObjectOutputStream oos = new ObjectOutputStream(fos);
                 oos.writeObject(mainMap);
                 oos.close();
                 fos.close();
                 System.out.printf("Serialized HashMap data is saved in hashmap.ser");
          }catch(IOException ioe)
           {
                 ioe.printStackTrace();
           }
 try
     {
 System.out.println("enter try for read");
        FileInputStream fis = new FileInputStream("F:/Education/test.txt");

        ObjectInputStream ois = new ObjectInputStream(fis);
        mainMap1 = (HashMap) ois.readObject();
        ois.close();
        fis.close();
     }catch(IOException ioe)
     {
        ioe.printStackTrace();
        return;
     }catch(ClassNotFoundException c)
     {
        System.out.println("Class not found");
        c.printStackTrace();
        return;
     }
     System.out.println("Deserialized HashMap..");
     // Display content using Iterator
     Set set = mainMap1.entrySet();
     Iterator iterator = set.iterator();
     while(iterator.hasNext()) {
        Map.Entry mentry = (Map.Entry)iterator.next();
        System.out.print("key: "+ mentry.getKey() + " & Value: ");
        System.out.println(mentry.getValue());
     }
}

}

Wednesday, 18 January 2017

Sorting Order for List using Comparator in Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortingForList {
public static void main(String[] args){
List<String> fruits = new ArrayList<String>();

fruits.add("Pineapple");
fruits.add("1");
fruits.add("3");
fruits.add("2");
fruits.add("9");
fruits.add("apple");
fruits.add("apricot");
fruits.add("Banana");
fruits.add("mango");
fruits.add("melon");        
fruits.add("peach");
    Collections.sort(fruits);
System.out.println("System generated sorting order::" + fruits);

Collections.sort(fruits, new Comparator<String>() {
   public int compare(String o1, String o2) {              
       return o1.compareToIgnoreCase(o2);
   }
});

System.out.println("Customized sorting order:::" + fruits);
}
}

Out put:
================
System generated sorting order::[1, 2, 3, 9, Banana, Pineapple, apple, apricot, mango, melon, peach]
Customized sorting order:::[1, 2, 3, 9, apple, apricot, Banana, mango, melon, peach, Pineapple]

Saturday, 14 January 2017

Remove the Duplicate Values from the ArrayList without using Set in Java

mport java.util.List;
import java.util.ArrayList;
public class DupTest {
    public static void main(String[] args){
        List ls = new ArrayList();
        ls.add("1");
        ls.add("3");
        ls.add("5");
        ls.add("1");
        ls.add("8");
         ls.add("3");
        ls.add("9");
    System.out.println("with duplicates:::::"+ls);
    List<String> uniqueList = new ArryaList<>();
    for(String elements : ls){
        if(!uniqueList .contains(elements)){
            uniqueList.add(elements);
        }
    }
    System.out.println("without duplicates::::"+uniqueList);

    }
}


Out Put
======================
with duplicates:::::[1, 3, 5, 1, 8, 3, 9]
without duplicates::::[1, 3, 5, 8, 9]