Singleton Implementation with Double Checked Locking in Java

-> Singleton implementation is nothing but creating the object instance only once and reusing the same instance when ever needed, instead creating a new one each time when needed.
-> In the below code instance is of lazy loading initializing type as the instance of class is declared in static variable but which is initialized only when the method getInstacne is invoked. Eager loading will be like declaring and initializing at the same time.like shown below.
private volatile static Singleton instance = new Singleton();

Double checked locking Idiom:

-> Now lets talk about Double checked locking idiom in singleton implementation. Here object is null or not will be checked twice and then object for the class will be created.
-> Why Volatile is used?: In double checked locking idiom, suppose if thread A is entered first null condition and then second null condition, then it will create an object for the class. Now suppose if thread B is entered first null condition, at the same time for thread A object is getting created (but instance creation is not completed yet) then as the null condition fails it’ll just return the same object instance which not completely initialized (half baked object), which leads to unexpected behavior of object.
So when voltaile is used, which makes the object getting published only when it is completely initialized, which avoids above problem.
-> The real problem is that Thread A may assign a memory space for instance before it is finished constructing instance. Thread B will see that assignment and try to use it. This results in Thread B failing because it is using a partially constructed version of instance.

Code:

public class Singleton {
private volatile static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}

 

 

Data Types in Java

Primitive Data types:

In Java there are eight (8) primitive data types. For all those primitive data types wrapper classes also available.

If you define the primitive data types as global variables they hold some default values which are given below.

/* Primitive data types and their default values*/
static byte by; //0
static char c; // space
static short sh; //0
static int i; //0
static long l; //0
static float f; //0.0
static double d; //0.0
static boolean b; //false

Note: If you define the primitive data types as local variables they hold nothing and you must initialize them.

Wrapper Classes (Non Primitive Data types):

If you define the wrapper class data types as global variables then they hold some default values which is null

 /* Wrapper Class data types */
static Byte by1; //null
static Character c1; //null
static Short sh1; //null
static Integer i1; //null
static Long l1; //null
static Float f1; //null
static Double d1; //null
static Boolean b1; //null

static BigDecimal bd; //null
static String s; //null

Note: If you define these wrapper class data types as local variables they hold nothing and you must initialize them.  There are few more wrapper classes like Array, List etc.

Sorting in Java

Sorting the numbers is easy in Java. Collections utility class provides the ‘sort’ method for sorting the numbers. [Don’t confuse with Collection which is an Interface]. To the Collections  Sort method you need to input numbers in List format. Though numbers present in Array also can be converted to List and then can be sorted. Java also provides another utility class ‘Arrays’ which has also Sort method. You can use either class methods for sorting on your convenience.

Collections.sort method expects List as input [array can also be send by converting to list object using Arrays.asList(arrayObj) method]

Arrays.sort method expects Array as input [List can also be send by converting to Array object using listObj.toArray() method]

Eg1: Sorting Numbers in List

List <Integer> numLst1 = new ArrayList<>();
numLst1.add(20);
numLst1.add(10);
numLst1.add(04);
Collections.sort(numLst1);
//Arrays.sort(numLst1.toArray());
int f = 0;
while(f< numLst1.size()){
System.out.println(numLst1.get(f));
f++;
}

o/p:   [4    10     20]

Eg2: Sorting Numbers in Array

Here we are converting Array to List and then sorting.

Integer[] numLst1 = {10, 30, 05};
Arrays.sort(numLst1);
//Collections.sort(Arrays.asList(numLst1));
int f = 0;
while(f<numLst1.length()){
System.out.println(numLst1[f].intValue());
f++;
}

o/p: [5    10     30]

Eg3: Sorting numbers and finding smallest number

Now let’s get into some deep by taking a requirement which returns the smallest number from the Array which should not be 0. Means if the Array contains numbers like [12, 20, 5, 30, 0, 50] then it should return smallest number as ‘5’ (not zero).

Integer[] returnVal = {10, 5, 20, 0};
Arrays.sort(returnVal);
//Collections.sort(Arrays.asList(returnVal));
int f= 0;
int smallValue = 0;
while(f < returnVal.length){
if (returnVal[f].intValue() !=0){
smallValue = returnVal[f].intValue();
break;
}
f++;
}
System.out.println("smallValue:"+smallValue);

o/p:   5

Sorting Numbers in HashSet & HashMap [not with the help of Sort method]

This can be achieved with the help of TreeSet. From TreeSet if you try to move to HashSet again then the insertion order will be lost. So keep the sorted result in TreeMap only or store them into LinkedHashSet where the insertion order is maintained. Similarly for HashMap sorting can be achieved with the help of TreeMap.


HashSet<Integer> hs = new HashSet<>();
hs.add(10);
hs.add(6);
hs.add(20);
hs.add(12);
System.out.println("hs:"+hs);
TreeSet<Integer> ts = new TreeSet<>();
ts.addAll(hs);
System.out.println("ts:"+ts);

o/p:     hs:[20, 6, 10, 12]      ts:[6, 10, 12, 20]

HashMap and Object Class:

====================================================================
Object Class:
Before discussing about Object class methods you should know one thing that every class by default extends ‘Object’ class. So you can override its methods.
Methods which you can override:
1) toString()
@Override
public String toString() {
return this.itemName+this.itemsCount;
}
2) equals(Object obj)
@Override
public boolean equals(Object obj) {
if(obj instanceof Price){  //checking that the new object getting created is instance of Price class or different class.
return (((Price)obj).itemName.equals(this.itemName) && (((Price)obj).itemsCount==this.itemsCount)) ;
// ((Price)obj).itemName -> means type casting current obj to Price obj as it is instnce of Price class and getting val of ‘itemName’
// this.itemName -> means getting value of ‘itemName’ from Price class.
}
3)     Hashcode()
@Override
public int hashCode() {
// TODO Auto-generated method stub
return new Integer(this.itemsCount).hashCode()+this.itemName.hashCode();
}

4) Clone()
5) finalize()

====================================================================
toString() method:
-> The wrapper classess like ‘Integer’, ‘Double’ will be having ‘toString()’ method in their calsses.
So you can convert Integer to string using that method which has implementation to convert. That’ll take Integer as input and gives string as output.
eg: Integer i = 10;
String ival = i.toString();
Note that toString() method has the code to convert Integer to String.
You can not overwrite the toString method of Integer class. [you can’t extend Integer class as it is decalred as Final. So you can not overwrite its toString method. This applies to all wrapper classes.]
-> If you want to store your class object as key in map. you can do it, but when you try to print the keys present in the Map then it’ll return the hashcode of address where the class object stored in hash memory, insted returning exact key. So you need to overwrite the ‘toString()’ method to give your own implementaion. By default toString method returns hashcode, so by overwriting this method in your class you can return any value for the class object instead of its hashcode.
Go thorugh post for detailed explanation:

====================================================================
hashcode() & equals(Object obj) methods:

  • In your hashmap if your key is String or any wrapper class type then if you insert record like hm.put(“fruit”,”banana”) and again if you insert record like hm.put(“fruit”,”apple”), then the second record’s value will be overridden on existing record, it won’t create new record. Because all the wrapper classes implements (Object class) hashcode() and equals(Object obj) methods. So when you insert a record whose key of wrapper class type, first hashcode() method will be called where the hashcode of key will be generated and checked with existing hashcodes of buckets and if it finds any similar hashcode present for any key then it’ll call equals method. In equals method it matches the current key with the matched hashcode key and if the Key’s are also same then returns boolean ‘True’. If it returns true then it will override the existing record, instead creating a new record.
  • Suppose your hashmap is using the key of custom type instead of wrapper class type like below HashMap<Price, int>hm = new HashMap<>(); In this case Key ‘Price’ class is not a wrapper class, so by default it won’t implement equals and hashcode methods. So if you insert a record even though the keys are same it’ll create a new record instead overriding the existing record. So you have to manually override the hashcode() and equals(Object obj) methods. In those method write the logic to generate the hashcode in hashcode() method and logic to compare Keys in equal method.

Now let’s discuss about equals() method. Lets take an example to understand about this method. You have an HashMap and you are storing the class object as Key and value as String. HashMap allows you to insert duplicate Keys(If your key is not of wrapper class type). If your requirement is like not to allow duplicate Keys then you must have to override equals(Object obj) method along with Hashcode() method and put your own implementation. Why? As shown in above example code snippets, first hashcode method will be triggered in which we are manually assigning hashcode to the key. after hashcode()  method, if the same hashcode generated is already present in bucket then equals(Object obj) method will be triggered. In equals(Object obj) method we are comparing current key object getting inserted is an instance of Price class, If yes then compare current object is already exists in HashMap or not, if yes return True. If you return true in equals method, then it’ll assign the new value to the existing key instead creating a new key value pair in HashMap.

====================================================================
HashMap vs Concurrent Hashmap:
Assume HashMap has a bucket and all the contents(Key Value pairs) are stored in it in one of the 16 slots based on Key’s hash value.
Let’s take a scenario, if many threads are acessing same Hashmap and if one thread is trying to modify the HashMap data(Key) and if another thread trying to read the data from HashMap then it’ll get ‘ConcurrentModificationException’. So to avoid this we can use ConcurrentHashMap which is synchronized. By using ConcurrentHashMap the same instance is cloned/shared by all the threads. So if the key is changed in one thread, then its newly generated Hashcode(address in bucket) is automatically updated in all the threads.
Note: You can synchronize your method also, but using Concurrent Hashmap is preferred over this as only HashMap is synchronized rather than everything in mehtod. And another thing you have to understand is that performance will be decreased when using ConcurrentHashMap as it updates latest records in all the current threads (which depends on number of users accessing the HashMap).
-> When you’ve multiple threads which try to modify data in HashMap, then  (1) use ConcurrentHashMap and use Forloop (don’t use Iterator) (2) use HashMap and use method as Synchronized, and ForLoop. (3) use HashMap and use Iterator (don’t use For loop).
-> If multiple threads try to just read the data(not modify) then use ‘HashMap’ and use ForLoop (don’t use Itertor)

====================================================================

HashMap vs ConcurrentHashMap vs Collections.synchronizedMap( HashMap ) :

The difference between HashMap and ConcurrenHashMap we just discussed above.

ConcurrentHashMap contains segments which are nothing but HashMaps (default 16 segments) so that multiple threads can access these multiple segments at a time. There is no lock/blocking for one thread if another thread is accessing. Means Concurrency is achieved here.

Collections.synchronizedMap(HashMap) is a synchronized version of HashMap. Here lock is enabled to the Map, means if one thread is modifying the map another thread can not access it. Means synchronization is achieved here and Map is accessed in blocking manner. This means if multiple threads try to access synchronizedMap at same time, they will be allowed to get/put key-value pairs one at a time in synchronized manner.

====================================================================

Collections.synchronizedMap( HashMap ) vs HashTable:

Both are synchronized version of collection. Both have synchronized methods inside class. Both are blocking in nature i.e. multiple threads will need to wait for getting the lock on instance before putting/getting anything out of it.

So what is the difference. Well, NO major difference for above said reasons. Performance is also same for both collections.

Only thing which separates them is the fact HashTable is legacy class promoted into collection framework. It got its own extra features like enumerators.

====================================================================

HashMap vs Hashtable detailed:

HashMap Hashtable
Synchronized No Yes
Thread-Safe No Yes
Null Keys and Null values One null key Any null values Not permit null keys and values
Iterator type Fail fast Iterator Fail safe iterator
Performance Fast Slow in comparison
Superclass and Legacy AbstractMap, No Dictionary, Yes

http://javahungry.blogspot.com/2014/03/hashmap-vs-hashtable-difference-with-example-java-interview-questions.html

====================================================================

HashMap, Collections.synchronizedMap, HashTable, ConcurrentHashMap:

  HashMap Collections.synchronizedMap HashTable ConcurrentHashMap
What? Map which stores (K, V) pairs. Which doesn’t allow multiple threads to modify the structure of Map. [e.g.: while iterating map, if you try to insert a record, it throws ConcurrentModificationException] Synchronized version of HashMap. [which allows only one thread to access Map a time as the lock is at object level. So lock will be for put, get methods.] Synchronized version of HashMap. [which allows only one thread to access Map a time as the lock is at object level. So lock will be for put, get methods.] This is also synchronized version of HashMap.  which allows multiple threads to modify the records(add/remove) at a time, as the lock is only at HashMap bucket level.
Can Multiple threads can read at a time? Yes Yes Yes Yes
Can Multiple threads can modify size of Map (Add or remove records while iterating) No (gives ConcurrentModificationException) No (gives ConcurrentModificationException) No (gives ConcurrentModificationException) Yes
Syntax HashMap<String, String> hm = new HashMap<> (); HashMap<String, String> hm = Collections.synchronizedMap(new HashMap<> ()); HashTable<String, String> ht = new HashTable<> (); HashMap<String, String> hm = new ConcurrentHashMap<> ();
Null Keys and Null values Can insert one Null key & any no of Null values. Can insert one Null key & any no of Null values. Can’t insert Null keys and Null Values. Can insert one Null key & any no of Null values.
Performance Faster than remaining all. Less compare to HashMap, but same as HashTable. Less compare to HashMap, but same as Collections.synchronizedMap Less compare to HashMap.
Synchronized? No Yes, at Object level Yes, at Object level Yes, at HashMap bucket level.

====================================================================
Default Constructor vs Parameterized Constructor:
-> If you have Default Constructor in your class, when ever you create an object to the class, the default constructor will assign default values to the class objects (global variables). Note that even though you didn’t define default constructor in your class, compiler will creates it.
-> If your have Parameterized Constructor in your class, when ever you create an object to the class by passing parameters defined constructor then those values will be assigned to the global variables.

====================================================================
Flow when record is inserted into HashMap:
HashMap<Price, String> hm = new HashMap<>();
hm.put(new Price(10, “Oranges”),”Fruit Orange”);
Insert Record to HashMap using put-> Hashcode() method’ll be called to get hashcode for key where it stores key, value pair -> Internally it comapares Hashcode generated is already present in Bucket or not -> If already present enters equals(Object obj) method.
====================================================================
Sample Code:

package model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class Price {

int itemsCount;
String itemName;

Price(int item, String name) {
this.itemsCount = item;
this.itemName = name;
}
Price() {
}

public static void main(String[] args) {

HashMap&amp;amp;lt;Price, String&amp;amp;gt; hm = new HashMap&amp;amp;lt;&amp;amp;gt;();
hm.put(new Price(10, "Oranges"),"Fruit Orange");
hm.put(new Price(20, "Mangos"), "Fruit Mango");
hm.put(new Price(30, "Bananas"), "Fruit Banana");
hm.put(new Price(30, "Bananas"), "Fruit Fruit");
hm.put(new Price(40, "Grapes"), "Fruit Grapes");
System.out.println("size:"+hm.size());

for (Entry&amp;amp;lt;Price, String&amp;amp;gt; entry : hm.entrySet()) {
System.out.println("Key:" + entry.getKey() + " &amp;amp;amp; Value:" + entry.getValue());
}

/*Iterator&amp;amp;lt;Price&amp;amp;gt; keySetIterator = hm.keySet().iterator();
while(keySetIterator.hasNext()){
Object key = keySetIterator.next();
System.out.println("key: " + key+ " value: " + hm.get(key));
}&amp;amp;nbsp;&amp;amp;nbsp; &amp;amp;nbsp;*/
}

//Alt+Shift+S -&amp;amp;gt; Override/Implement methods

@Override
public String toString() {
return this.itemName+this.itemsCount;
//return "ravi";
}

@Override
public int hashCode() {
System.out.println("Hashcode entered..");
// TODO Auto-generated method stub
return new Integer(this.itemsCount).hashCode()+this.itemName.hashCode();
}

@Override
public boolean equals(Object obj) {
if(obj instanceof Price){
System.out.println("equals method..obj is instance of class");
Boolean b = ((Price)obj).itemName.equals(this.itemName) &amp;amp;amp;&amp;amp;amp; (((Price)obj).itemsCount==this.itemsCount);
return (b) ;
}
return false;
//return super.equals(obj);
}
}

Response:
Hashcode entered..
// When First record inserted it entered Hascode() method to get the Hashcode address for key where it stores Key& Value.
Hashcode entered..
// When Second record inserted it entered Hascode() method to get the Hashcode address for key where it stores Key& Value.
Hashcode entered..
// When third record inserted it entered Hascode() method to get the Hashcode address for key where it stores Key& Value.
Hashcode entered..
// When fourth record inserted it entered Hascode() method to get the Hashcode address for key where it stores Key& Value.
equals method..obj is instance of class
// After fourth record entered as hashcode genreated in fourth reocord is same as hashcode generated in third record, it enteres equals() method
Hashcode entered..
// When fifth record inserted it entered Hascode() method to get the Hashcode address for key where it stores Key& Value.
size:4  //here you can see only four records in HashMap as fourth record is overriden on third record address.
Key:Oranges10 & Value:Fruit Orange
Key:Bananas30 & Value:Fruit Fruit
Key:Mangos20 & Value:Fruit Mango
Key:Grapes40 & Value:Fruit Grapes

====================================================================
HashMap methods:
put(k,v), get(k), remove(k), size(), clear(), isEmpty(), containsKey(k), containsValue(v), entrySet(), keySet(), putAll(Map)
====================================================================
HashMap memory Allocation:
Default Size of HashMap: 16 and threshold is 0.75% i.e [0.75(16)=12]
So when you insert records in HashMap, when ever it reaches sixe of 12, then HashMap size will be extended to 12+16 = 28.
When ever the records in HashMap reaches [0.75(28)=21], then HashMap size will be extended to 21+16 = 35.
and so on the size wil be extended when ou keep on inserting records to HashMap.
====================================================================
// Do you know that you can not assign null value to int, if you do so, you’ll get NullPointer Exception. You can assign Null value to Integer.
Integer i = null;
int j = i;
System.out.println(“j”+j);  //java.lang.NullPointerException
====================================================================
Loop HashMap records:
/*  HashMap For Loop:
*  HashMap<Integer, String> hm = new HashMap<>();
*  for(Entry<Integer, String> m: hm.entrySet()){ }
*  for(Map.Entry<Integer, String> m:hm.entrySet()){  }
*  for(HashMap.Entry<Integer, String> m: hmap.entrySet()){ }
*  for(Entry m:hm.entrySet()){ } –without type casting
*  Inside for to get key and values -> m.getKey(), m.getValue()
*/

/*  HashMap Itertor and while Loop:
*  HashMap<Integer, String> hm = new HashMap<>();
*  Iterator<Price> keySetIterator = hm.keySet().iterator();
while(keySetIterator.hasNext()){
Object key = keySetIterator.next();
System.out.println(“key: ” + key+ ” value: ” + hm.get(key));
}
*/
====================================================================

HashMap References :
http://www.java2novice.com/java-collections-and-util/hashmap/iterate/
http://www.java2novice.com/java-collections-and-util/hashmap/

Overwrite toString() method in a Java class

  • The wrapper classes like ‘Integer’, ‘Double’, ‘Float’ will be having ‘toString()’ method in their classes. Implementation in that method is like they take input as respective type and return String value.
    eg: Integer i = 10;
    String ival = i.toString();
    Note that toString() method has the code to convert Integer to String.
  • You can not overwrite the toString() method of Integer class or any other wrapper classes. [you can’t extend Integer class as it is declared as Final. So you can not overwrite its toString() method. This applies to all wrapper classes.]
  • If you want to store your class object as key in map. you can do it, but when you try to print the keys present in the Map then it’ll return the hashcode of address where the class object stored in hash memory, instead returning exact key. So you need to overwrite the ‘toString()’ method to give your own implementation. By default toString() method returns hashcode, so by overwriting this method in your class you can return any value for the class object instead of its hashcode.
  • Check the below Java code.

package model;

import java.util.HashMap;
import java.util.Map.Entry;

public class Price {

int itemsCount;
String itemName;

Price(int item, String name) {
this.itemsCount = item;
this.itemName = name;
}

public static void main(String[] args) {
HashMap<Price, String> hm = new HashMap<>();
hm.put(new Price(10, "Oranges"), "Fruit Orange");
hm.put(new Price(20, "Mangos"), "Fruit Mango");
hm.put(new Price(30, "Bananas"), "Fruit Banana");
hm.put(new Price(30, "Grapes"), "Fruit Grapes");

for (Entry<Price, String> entry : hm.entrySet()) {
System.out.println("Key:" + entry.getKey() + " & Value:" + entry.getValue());
}

}

@Override
public String toString() {
return this.itemName+this.itemsCount;
//return "ravi";
}

}

 

Output if you don’t overwrite the toString() method:

Key:model.Price@7852e922 & Value:Fruit Banana
Key:model.Price@15db9742 & Value:Fruit Orange
Key:model.Price@6d06d69c & Value:Fruit Mango
Key:model.Price@4e25154f & Value:Fruit Grapes

Output after overwriting the toString() method:

Key:Bananas30 & Value:Fruit Banana
Key:Oranges10 & Value:Fruit Orange
Key:Mangos20 & Value:Fruit Mango
Key:Grapes30 & Value:Fruit Grapes

// If you return ‘ravi’ in toString() method then it’ll return ‘ravi’ as key for all records.

 

 

 

 

 

Implement Auto suggest in JSP using Ajax call in Jquery

This solution is: When user enters any data on the input field, the entered data is taken and using ajax call servlet method will be triggered, which returns the matched records in DB as JSON string. Then the response data will be shown as suggestion under input text field.

Files Created to implement this functionality:

autoSuggest.jsp, searchServlet.java.

Code for autoSuggest.jsp:


<!DOCTYPE html>
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
         <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
  var val = $("#project").val();
  var projects;

    $( "#project" ).autocomplete({
      minLength: 1,
      source: function( request, response ) {
       $.ajax({
            type: "Get",
            url:"searchservlet",            
            contentType : "application/json", dataType : 'json',
            data:{"empName":$("#project").val()},
            success: function(data){
                projects = data;
                response(data);
            },
            failure: function(){
                alert("failed..");
            }
         });
      },
      focus: function( event, ui ) {
        $( "#project" ).val( ui.item.EmpName );
        return false;
      },
      select: function( event, ui ) {
        $( "#project" ).val( ui.item.EmpName );        
        return false;
      }
    })  
   
    .data("ui-autocomplete")._renderItem = function (ul, item) {
                return $("
	<li style='font-size:16px;color:#777;padding:1px;'>")
                    .data("ui-autocomplete-item", item)
                    .append("<a> " + item.EmpName + "
 </a>")
                    .appendTo(ul);
            };
            
    /*  [This code also works similar as above [you can change the item name. But you have to use only "instance" with .autocomplete or
     * "ui-autocomplete" with .data] 
    * .autocomplete( "instance" )._renderItem = function( ul, listitem ) {
      return $( "
	<li>" )
        .append( "
<div>" + listitem.EmpName + "
" + "</div>
" )
        .appendTo( ul );        
    };*/        
  } );
  </script>
</head>
<body>
<div id="project-label">Search an Employee (type "E" for a start)::</div>
<input style="padding:10px;font-size:18px;margin:10px;" id="project">
<!--<input type="hidden" id="project-id"> -->
</body>

</html>

searchServlet.java


package model;

import java.io.IOException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@WebServlet(name = "searchServlet", urlPatterns = { "/searchservlet" })
public class searchServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("entered servlet...");
String empName = request.getParameter("empName").concat("%");
System.out.println("request value:"+empName);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr");
PreparedStatement ps = conn.prepareStatement("select empname from emp where empname like ? order by empname");
ps.setString(1, empName);
ResultSet rs = ps.executeQuery();
JSONArray jarray = new JSONArray();

int i =1;
while(rs.next()){
JSONObject jobj = new JSONObject();
String lable = "EmpName";
jobj.put(lable, rs.getObject(1));
jarray.add(jobj);
i++;
}

String JsonString = jarray.toJSONString();
System.out.println("JsonString:"+JsonString);
conn.close();
response.getWriter().write(JsonString);
} catch (Exception e) {
}

}
}

 

UI Page:

3

 

 

 

 

 

Ajax Call in JSP to fetch and display data from Database

We knew that with the help of JQuery we can dynamically display data on a webpage. So here in this post we are using  JQuery to make an Ajax call to Servlet, [Servlet connects to DB, fetches data and returns that data to Ajax call] and display the returned data as a table on JSP web page.

Create Servlet first

Create dataServlet.java

package model;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import org.eclipse.persistence.oxm.json.JsonArrayBuilderResult;
import org.eclipse.persistence.oxm.json.JsonGeneratorResult;
import org.eclipse.persistence.oxm.json.JsonObjectBuilderResult;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@WebServlet(name = "dataServlet", urlPatterns = { "/dataservlet" })
public class dataServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("enterred servlet...");
String empID = "";
try {
empID = request.getParameter("empID");
System.out.println("empID is:" + empID);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr");
PreparedStatement ps = con.prepareStatement("select * from emp where empid != ? order by empid");
ps.setString(1, empID);
ResultSet rs = ps.executeQuery();
System.out.println("got response....");

JSONArray json = new JSONArray();
ResultSetMetaData metadata = rs.getMetaData();
int numColumns = metadata.getColumnCount();

while (rs.next()) //iterate rows
{
JSONObject obj = new JSONObject(); //extends HashMap
for (int i = 1; i <= numColumns; ++i) //iterate columns
{
String column_name = metadata.getColumnName(i);
obj.put(column_name, rs.getObject(column_name));
System.out.println("rs.getObject('" + column_name + "')........." + rs.getObject(column_name));
}
json.add(obj);
System.out.println("Added JSON object to JSON Array..");

}

response.setContentType("application/json;charset=UTF-8");
String jsonString = json.toJSONString();
System.out.println("jsonString:" + jsonString);
response.getWriter().write(jsonString);

Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("Status", "Success");
jsonMap.put("Rows", 100);

con.close();
} catch (Exception e) {
e.printStackTrace();
}

}
}

 

Create empData.jsp

  <!DOCTYPE html>
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
         <script type="text/javascript">
          $.ajax( {
              type : "Get", url : "dataservlet", contentType : "application/json", dataType : 'json', data :  {
                  "empID" : "31"
              },
              success : function (data) {
                  $("#mytbl").append('

<tr id="headerRow"></tr>

');
                 /* In below function you can take data[any row number] data[0].
                    (a, b) are (key, value) for 0th json object. as we are shoiwng only Keys so we used only 'a' below.
                 */
                  $.each(data[0], function (a, b) {
                      $("#headerRow").append('

<td style="padding:10px;background:orange;color:white;">' + a + '</td>

');
                  });
                 /* now we have to display data [multiple row with multiple column data].
                 * Here you have to observe one thing is we have multiple rows of data [multiple JSON objects in a JSON Array &
                 * Each JSON object contains multiple key value pairs]. So we must loop through each row and and then loop through each column[key value].
                 * function $.each(data, function(a, b) will get all JSON objects inside JSON Array, where 'a' indicates JSON object number.
                 * 'b' indicates the 'JSON object'. similarly
                 * function $.each(data[a], function(c, d) will get 'a'th JSON object, And here 'c' indicates Key where as 'd' indicates value.
                 * */
                  $.each(data, function (a, b) {
                      $("#mytbl").append('

<tr id="' + a + '"></tr>

');
                      var dataRowId = '#' + a;
                      $.each(data[a], function (c, d) {
                          $(dataRowId).append('

<td style="padding:10px;background:green;color:white;">' + d + '</td>

');
                      });
                  });
              },
              failure : function () {
                  alert("error...");
              }
          });
        </script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>first</title>
</head>
<body>
<?audit suppress oracle.ide.xml.mismatched-end-tag?>
<table id='mytbl'></table>
<div style="background:Green;width:100%;padding:5px 0px;margin:10px 0px; ">
<p style="color:#fff;margin-left:10px;"> copyrights @teja.com</p>

</div>
</body>
</html>

 

Data in the DB is like below:

1

JSON Array that the servlet returns to Ajax call will be like below.

JSON_Array.png

The JSP page will look like below.

2