[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-10-25
6
Medium Priority
?
941 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
Comment
Question by:ramnram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12398230
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
ID: 12418637
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
ID: 12418650
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
sriven10 earned 375 total points
ID: 12419414
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
ID: 12419518
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
ID: 12419529
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

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

650 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