Solved

Sample code for how to convert XML elements into JDBC Resultset ?

Posted on 2004-10-25
726 Views
Last Modified: 2012-08-14
Can any one give a sample code for how to upload xml data into MSAccess table ? I have the code for how to convert MSAccess table data to xml. I need a sample code for vice versa.
0
Question by:ramnram
    6 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    You just need to parse the xml and form insert statements. This would be very specific to the column names involved. Please give the ones in question.
    0
     

    Author Comment

    by:ramnram
    hey i am sorry for posting two mistake i want u people to answer any one only. u people pls give me a idea to delete this question. i cant giv 125 pts for the same question
    0
     

    Author Comment

    by:ramnram
    hey i am sorry for posting two questions of same type by mistake i want u people to answer any one only. u people pls give me a idea to delete this question. i cant giv 125 pts for the same question
    0
     

    Accepted Solution

    by:
    try this code. u wil certainly get an idea.


    import java.sql.*;
    import java.io.*;
    import java.util.*;
    import java.lang.*;

    import java.io.StringReader;
    import java.io.StringWriter;
    import java.io.IOException;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;

    import javax.xml.transform.*;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.FactoryConfigurationError;

    import org.w3c.dom.*;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;

    //      Main class JavaXML

    class  JavaXML
    {
          static Connection connection = null;
          Statement statement = null;
          ResultSet resultset = null;
          int rowCount = 1;

    // Static method call for the initial actions to be done

          static
          {
                try
                {
                      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                      connection = DriverManager.getConnection("jdbc:odbc:Test","","");
                }
                catch(Exception e)
                {
                      e.printStackTrace();
                }
          }

    //      Finalize method for the final action

          public void finalize()
          {
                try
                {
                      connection.close();
                }
                catch(SQLException e)
                {
                      e.printStackTrace();
                }
          }

    //      Method for getting the database list using Document object

          public Document getCustomerList()
          {
                Document document = null;
                try
                {
                      statement = connection.createStatement();
                      resultset = statement.executeQuery("select * from temp");
                      document = toDocument(resultset);
                      resultset.close();
                      statement.close();
                }
                catch(Exception e)
                {
                      e.printStackTrace();
                }
                return document;
          }

    //      toDocument method for creating XML content

          public Document toDocument(ResultSet resultset) throws ParserConfigurationException,SQLException
          {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.newDocument();

                ResultSetMetaData rsmd = resultset.getMetaData();
                int colCount = rsmd.getColumnCount();

                Element results = document.createElement("Results");
                document.appendChild(results);

                while(resultset.next())
                {
                      Element rows = document.createElement("Rows");
                      results.appendChild(rows);

                      for(int i = 1;i <= colCount;i++)
                      {
                            String columnName = rsmd.getColumnName(i);
                            Object value = resultset.getObject(i);

                            Element node = document.createElement(columnName);
                            node.appendChild(document.createTextNode(value.toString()));
                            rows.appendChild(node);
                      }
                }
                return document;
          }

    //       Public method

          public static void main(String[] args) throws Exception
          {
                JavaXML jxml = new JavaXML();
                Document document = jxml.getCustomerList();
                try
                {
                      Source source = new DOMSource(document);
                      File file = new File("xmlfile.xml");
                      Result result = new StreamResult(file);
                      Transformer xformer = TransformerFactory.newInstance().newTransformer();
                      xformer.transform(source, result);
                }
                catch(TransformerConfigurationException e)
                {
                      e.printStackTrace();
                }

                catch(TransformerException e)
                {
                      e.printStackTrace();
                }
          }
    }
    0
     

    Author Comment

    by:ramnram
    i am going to do onething
    i am going to change the question

    its relatively simple


    Can any one give a sample code for how to convert a DB table to XML?

    pls giv ur answers immediatly. since there is a answer already posted above. i wil wait for another 2 days.

    0
     

    Author Comment

    by:ramnram
    Dear CEHJ,

    pls do me a favour. pls send ur above comment to ques no.

    http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21182437.html

    i wil consider ur comment in tat question. sorry for the inconvienience.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    Suggested Solutions

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now