Thursday, 12 March 2015

How to Read the XML file in JAVA

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

  public static void main(String argv[]) {

    try {

File fXmlFile = new File("D://test.xml"); //path of the xml file
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

doc.getDocumentElement().normalize();

System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("staff");

System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

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

}

==================================
test.xml file
==================================
<?xml version="1.0"?>
<company>
<staff id="1001">
<firstname>xxxx</firstname>
<lastname>vvvv</lastname>
<nickname>ssss</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>yyyy</firstname>
<lastname>gggg</lastname>
<nickname>kkkkkk</nickname>
<salary>200000</salary>
</staff>
</company>

Tuesday, 10 March 2015

Object Restriction for the class in java?(How to restrict object creation not more than 3 in Java class?)

We can restrict the creation of Object for a particular class by little modification in Singleton design pattern. Following code is to create the three objects for the class;

public class LimitClass {
private static LimitClass limInstance;
    public static int objCount = 0;

    private LimitClass(){
        objCount++;
    }

    public static synchronized LimitClass getLimInstance(){
        if(objCount < 3 ){
            limInstance = new LimitClass();
        }
        return limInstance;
    }

    public static void main(String[] args) {

        LimitClass obj1 = LimitClass.getLimInstance();
        LimitClass obj2 = LimitClass.getLimInstance();
        LimitClass obj3 = LimitClass.getLimInstance();
        LimitClass obj4 = LimitClass.getLimInstance();
        LimitClass obj5 = LimitClass.getLimInstance();
        LimitClass obj6 = LimitClass.getLimInstance();

        System.out.println(obj1);
        System.out.println(obj2);

        System.out.println(obj3);
        System.out.println(obj4);
        System.out.println(obj5);
        System.out.println(obj6);
      }
}

Saturday, 7 March 2015

inverse attribute in hibernate what is use of inverse attribute in hibernate

Inverse attribute informs the hibernate that the relation ship is Bi Directional
If we write inverse = “false” then hibernate understands that relationship as unidirectional and generates additional update operations on the database, so in order to reduce the internal operations, we need to include inverse=”true

remember, default value of inverse =”false
If we make inverse =”true” the performance will be increased

Thursday, 5 March 2015

What is orphan record in hibernate

an orphan record means it is a record in child table but it doesn’t have association with its parent in the application.
In an application, if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan”.

Tuesday, 3 March 2015

what is difference between ClassNotFoundException and NoClassDefFoundError in JAVA

ClassNotFoundException:
For dynamically provided class names at runtime if the corresponding .class file name not available then we will get the runtime exception saying CallsNotFoundException. It is Checked Exception.

NoClassDefFoundError:

For hard coded class names at runtime the corresponding .class file not availbale then we will get exception called NoClassDefFoundError. It is UnChecked Exception

For better understanding see the link:
https://www.youtube.com/watch?v=pZw6T-hNhjM