Tuesday 30 September 2014

How to close the popup window by pressing escape key button or clicking outside of the popup window with jQuery.

For closing the open popup window on the click of escape key or clciking outside the popup window, We need to create one jQuery method in every modal panel .xhtml file or any other .xhtml file which is used to show the popup window as:

<script type="text/javascript">
    jQuery(document).keyup(function (e){
            var clickedID = e.target.id;
            var unicode=e.keyCode? e.keyCode : e.charCode /*This will capture the key pressed event*/
                 if(unicode==27){//Key code for escape key is 27
                 Richfaces.hideModalPanel(#{modalPanelId});
                /*var win = window.open("","_self"); r
                 win.close();*/
                }
</script>


The above method will automatically be called whenever we press any key and if pressed key is escape key then it would close the opened popup window so it will work when we press theescape key.

To close popup window when we click on the outside of the popup window, we have to call “Richfaces. hideModalPanel(‘modelPanel Id’)” on “onmaskclick”  event of rich:modalPanel as:

onmaskclick="Richfaces.hideModalPanel('#{modalPanelId}')"

Monday 29 September 2014

How to compile .JRXML file and produce .jasper file with JAVA code

The Jasper Report design file specifies the layout and appearance of the report and they works best with the .JRXML extension files. We use iReport to deal with the jrxml reports but sometimes we need to compile JRXML file through our java code. We can do this with the help of net.sf.jasperreports package.
Change the path of the JRXML and jasper files in below code and run this code to compile any JRXML file and generate jasper report.

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;


public class compileJasper{

public static void main(String[] args)
    {
    //    below code will compile the jasper file which is located on path given below
        try {
    JasperCompileManager.compileReportToFile("PathOfTheJRXMLFile(which needs to be compiled)" ,"PathOfJasperFile(Where to save generated jasper file)");
        } catch (JRException e) {
           
        }
    }
}

What is the difference between JDK, JRE, JVM


This question is very common in all java interviews. Basically it checks the candidates knowledge of how java works. This question can be extended in further questions like java class loader and how class files are loaded in java etc. but for that firstly we should know that what these three terms actually means

JDK : -
JDK means Java Development Kit. JDK contains Java Run-time Environment (JRE) , Compilers and debuggers required to develop applets and applications.

JRE :-
JRE is subset of JDK, It contains Java Virtual Machine (JVM) along with Java Class Libraries which implement the Java application programming interface (API) form the Java Runtime Environment (JRE).

JVM : -
Java Compilers compile JAVA source code and create a .class file having the byte code. Java Virtual Machine(JVM) interpreters Java byte code and send necessary commands to underlying hardware or Just in time compiled to native machine code and execute it directly on the underlying hardware . If we have already compiled code of java (.class files) then we just only needs the JVM to interpreters .class file codes.

Monday 6 January 2014

How to skip grants in MySQLdatabase or start without password

If you wants to start MySql in 'skip-grant-tables' mode on Windows or without password.

Please follow the steps below
1). Stop the MySQL service through Administrator tools, Services.

2). Modify the my.ini configuration file (assuming default paths)
     C:\Program Files\MySQL\MySQL Server 5.5\my.ini

3). Change the contents as:
      In the SERVER SECTION, under [mysqld], add the following line:
     skip-grant-tables

    As:

    # SERVER SECTION
    # ----------------------------------------------------------------------
    #
    # The following options will be read by the MySQL Server. Make sure that
    # you have installed the server correctly (see above) so it reads this
    # file.
   #
   [mysqld]

   skip-grant-tables

4). Start the service again and you should be able to log into your database without a password.
share|improve this answer

Friday 23 August 2013

How to sort HashTable and HashMap in java collection?



Map interface does not provide a direct sort() method like collection interface so we have to sort them manually. We took the example of HashMap and HashTable for sorting. HashMap is by default sorted on the basis of Key but HashTable is not sorted at all. 

1 - Hashtable sorting:
We have two ways to sort HashTable.
  • On the basis of key.
HashTable  --> TreeMap or HashMap
  • On the basis of value.
HashTable --> ArrayList --> Comparator sorting of ArrayList --> convert to map again

Example Code:-
 import java.util.*;
public class demoSortHashTable
{
 public static void main(String s[])
        {
            Map mp = new Hashtable();
            mp.put(1,"First");
            mp.put(2,"Second");
            mp.put(3,"Third");
            mp.put(4,"Fourth");
          
            System.out.println("Print HashTable contents without sorting");
            System.out.println(mp);

           //First way to sort hashmap by converting it to TreeMap(it will sort on the basis of key)
           System.out.println("HashTable contents after sorting by key");
           Map mpt = new HashMap(mp);
           System.out.println(mpt);

           //Second way to sort hashmap by converting it to List(it will sort on the basis of value also)
           System.out.println("HashTable contents after sorting by value");
           //Converting HashMap to ArrayList
           List lt = new ArrayList(mp.entrySet());
           //Sorting the ArrayList with Comparator
           Collections.sort(lt, new Comparator() {
                                                public int compare(Object o1, Object o2) {
                                                                return ((Comparable) ((Map.Entry) (o1)).getValue())
                                       .compareTo(((Map.Entry) (o2)).getValue());
                                                }
                                });
            //Putting the value of the List into LinkedHashSet
            Map mp1 = new LinkedHashMap();
            Iterator it1 = lt.iterator();
            while (it1.hasNext())
             {
                 Map.Entry ent = (Map.Entry) it1.next();
                 mp1.put(ent.getKey(),ent.getValue());
             }
          System.out.println(mp1);
        }
}

Output:
Print HashTable contents without sorting
{4=Fourth, 3=Third, 2=Second, 1=First}
HashTable contents after sorting by key
{1=First, 2=Second, 3=Third, 4=Fourth}
HashTable contents after sorting by value
{1=First, 4=Fourth, 2=Second, 3=Third}



2 - HashMap sorting:
HashMap is sorted by default (on key) so we just need to sort it on the basis of Value if needed.

Example Code:
import java.util.*;
public class demoSortHashMap
{
 public static void main(String s[])
        {
            Map mp = new HashMap();
            mp.put(1,"First");
            mp.put(2,"Second");
            mp.put(3,"Third");
            mp.put(4,"Fourth");
            System.out.println("HashMap contents are sorted on keys by default");
            System.out.println(mp);

          //Sort hashmap by converting it to List(it will sort on the basis of value also)
           System.out.println("HashMap after sorting by value");
           //Converting HashMap to ArrayList
           List lt = new ArrayList(mp.entrySet());
           //Sorting the ArrayList with Comparator
           Collections.sort(lt, new Comparator() {
                                                public int compare(Object o1, Object o2) {
                                                                return ((Comparable) ((Map.Entry) (o1)).getValue())
                                       .compareTo(((Map.Entry) (o2)).getValue());
                                                }
                                });
            //Putting the value of the List into LinkedHashSet
            Map mp1 = new LinkedHashMap();
            Iterator it1 = lt.iterator();
            while (it1.hasNext())
             {
                 Map.Entry ent = (Map.Entry) it1.next();
                 mp1.put(ent.getKey(),ent.getValue());
             }
          System.out.println(mp1);
        }
}

Output: -
HashMap contents are sorted on keys by default
{1=First, 2=Second, 3=Third, 4=Fourth}
HashMap after sorting by value
{1=First, 4=Fourth, 2=Second, 3=Third}