Monday, May 11, 2015

How to write a XML file in java.

Step 1- Create document

Step 2- Create root element

Step 3- Append root into document

Step 4- Create child element of root element

How to read XML file in java.

We can read a xml by following stpes-

Step 1- Create File object of xml file.

1
fXmlFile = new File("src\\myXml.xml");

Step 2- Create a document and parse the xml and sotore it into document object

1
2
3
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

How to transfer ResultSet object over network in java.

As we know the ResultSet object can not be transfer over the network. So for transferring the ResultSet object we need to set this object in Custom object and send this custom object over the network.

Step 1- Create UserTest class

Step 2- Retrieve the ResultSet object and set it into custom object (UserTest)

Saturday, May 9, 2015

How to add two array in java.

Step 1- Create two array

1
2
String a0[] = { "A", "E", "I" };
String b0[] = { "O", "U" };

Step 2- Convert these arrays into List

1
2
List a1 = new ArrayList(Arrays.asList(a0));
List b1 = new ArrayList(Arrays.asList(b0));

How to get the column name of table dynamically in jdbc.

While displaying the table data in jdbc we have to get ResultSet object using ResultSet we are displaying the table data by using rs.getInt(1) or rs.getString(“name”) like this.

But, If we don’t know the column name or number of column in table  we can use ResultSetMetaData for this. Using ResultSetMetaData we can  get the number of column, type of column, name of column and so on.

How to sort the object based on Id and Name in Java

If we have the List, Map or any other collection object we sort by using calling Collections.sort() method directly. But if we have our custom object we have to do some extra to sort our custom object. Assume that we have one Person Object like below

class Person{
    int id;
    String name;

    Setter…
    Getter…
}

Serialize and Deserialize the object in java.

Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization.

In this process there are three steps
1-    Create java class by implementing the Serializable class.
2-    Create SerializeDemo class to write a java object into file.
3-    Create DeserializeDemo class to read the java object from file.