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

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.
ramnramAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
sriven10Connect With a Mentor Commented:
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
 
CEHJCommented:
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
 
ramnramAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ramnramAuthor Commented:
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
 
ramnramAuthor Commented:
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
 
ramnramAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.