i have written the Java program that extract the data from xml and provides below output. Once the extraction done, data has to store in SQL Database. Please help me to add the data in SQL database,
this below output has to store in Database,
Output:-
Root element of the doc is geodata
Total no of people : 2
Address : 2344 States Drive, MA 01213, USA
Phone Number : 333-222-2222
Salary : $3000
Pension : $3000
Address : 2345 Gates Drive, PA 11213, USA
Phone Number : 444-222-2222
Salary : $3500
Pension : $3500
Source Code:-
package com.techassignment;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class readxml
{
public static void main (String argv [])
{
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("C:\\Users\\mkar\\workspace\\techassignment - new\\src\\com\\techassignment\\testFinal.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList addressList = firstPersonElement.getElementsByTagName("address");
Element addressElement = (Element)addressList.item(0);
NodeList textFNList = addressElement.getChildNodes();
System.out.println("Address : " +
((Node)textFNList.item(0)).getNodeValue().trim());
//-------
NodeList phonenumberList = firstPersonElement.getElementsByTagName("phonenumber");
Element phonenumberElement = (Element)phonenumberList.item(0);
NodeList textLNList = phonenumberElement.getChildNodes();
System.out.println("Phone Number : " +
((Node)textLNList.item(0)).getNodeValue().trim());
//----
NodeList salaryList = firstPersonElement.getElementsByTagName("salary");
Element salaryElement = (Element)salaryList.item(0);
NodeList textAgeList = salaryElement.getChildNodes();
System.out.println("Salary : " +
((Node)textAgeList.item(0)).getNodeValue().trim());
//----
NodeList PensionList = firstPersonElement.getElementsByTagName("pension");
Element PensionElement = (Element)salaryList.item(0);
NodeList textPensionList = salaryElement.getChildNodes();
System.out.println("Pension : " +
((Node)textPensionList.item(0)).getNodeValue().trim());
//------
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
create a method say insert(root,noOfPeople,Address,phNo,salary) and pass all the data you wanted to insert into the data base in the parameters values one by one so that it inserts row wise
Row 1 in the data base will be -
Address : 2344 States Drive, MA 01213, USA
Phone Number : 333-222-2222
Salary : $3000
Pension : $3000
Row 2 in the data will be -
Address : 2345 Gates Drive, PA 11213, USA
Phone Number : 444-222-2222
Salary : $3500
Pension : $3500
also if you don't have any idea how to store data then read for JDBC in java. also you can try 1. How to store data in Java? Database? 2. http://www.tutorialspoint.com/jdbc/jdbc-db-connections.htm 3. http://www.tutorialspoint.com/jdbc/jdbc-insert-records.htm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.