//MsSqlConnectionData.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class MsSqlConnectionData {
public static Connection getconnection()
{
Properties properties = new Properties();
Connection con = null;
try {
String URL = "jdbc:sqlserver://20.1.1.4;databaseName=SampleTestDB";
String username = "sa";
String password = "Esales123";
Properties info = new Properties();
info.put("user", username);
info.put("password", password);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(URL, info);
con.setAutoCommit(false);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("MsSQL Connection Error " + e.toString());
e.printStackTrace();
}
return con;
}
}
--------------------------------------------------------------------------------------------------------------------------
//ReadXML.java
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ReadXML
{
public static void main (String[] args)throws Exception
{
Connection connectionobj = null;
Statement insertStmt = null;
try
{
connectionobj = MsSqlConnectionData.getconnection();
insertStmt = connectionobj.createStatement();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("D://userData.xml"));
doc.getDocumentElement().normalize();
NodeList listOfstate = doc.getElementsByTagName("state");
for (int loop = 0; loop < listOfstate.getLength(); loop++)
{
Node prop1 = listOfstate.item(loop);
NodeList listchd = prop1.getChildNodes();
NamedNodeMap attr1 = prop1.getAttributes();
if (attr1 != null)
{
Node countryNode = attr1.getNamedItem("name");
String state = (String)countryNode.getNodeValue();
for (int innerLoop = 0; innerLoop < listchd.getLength(); innerLoop++)
{
if (listchd.item(innerLoop).getNodeType() == Node.ELEMENT_NODE)
{
String countyName = "";
Node chdnode = listchd.item(innerLoop);
Attr attrName = ((Element) chdnode).getAttributeNode("NAME");
if (attrName.getValue() != null) {
countyName = attrName.getValue();
String sqlStatement = "INSERT INTO T601_US_Cities (state,city) VALUES ('"+ state + "','"+ countyName + "')";
insertStmt.addBatch(sqlStatement);
}
}
}//end of inner for Loop
}
}//end of for Loop
int[] rowsInsert = insertStmt.executeBatch();
connectionobj.commit();
insertStmt.clearBatch();
System.out.println("No. of Rows Inserted :"+rowsInsert.length);
}
catch (Exception e)
{
System.out.println("Exception :" + e.getMessage());
}
finally{
try {
connectionobj.close();
insertStmt.close();
} catch (SQLException e) {
System.out.println("ERROR IN Finally : "+e.getMessage());
}
}
}//end of main Program
}

