Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-14-2008, 10:50 AM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default ResultSet to XML
ResultSet can be converted to XML as shown below:

Code:
class JDBCapp  {

  static Connection con;

  public static void main (String args[]) {
    ResultSet rs = null;
    Statement stmt = null;
    String sql;

    try {
      DocumentBuilderFactory factory = 
         DocumentBuilderFactory.newInstance();
      DocumentBuilder builder =factory.newDocumentBuilder();
      Document doc = builder.newDocument();
      Element results = doc.createElement("Results");
      doc.appendChild(results);

      // connection to an ACCESS MDB
      con = AccessCon.getConnection();

      sql =  "select objet from Email";
      stmt = con.createStatement();
      rs = stmt.executeQuery(sql);

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

      while (rs.next()) {
        Element row = doc.createElement("Row");
        results.appendChild(row);
        for (int ii = 1; ii <= colCount; ii++) {
           String columnName = rsmd.getColumnName(ii);
           Object value = rs.getObject(ii);
           Element node = doc.createElement(columnName);
           node.appendChild(doc.createTextNode(value.toString()));
           row.appendChild(node);
        }
      }

      System.out.println(getDocumentAsXml(doc));

    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
      try {
        if (con != null) con.close();
        if (stmt != null) stmt.close();
        if (rs != null) rs.close();
      }
      catch (Exception e) {
      }
    }
 }

 public static String getDocumentAsXml(Document doc)
      throws TransformerConfigurationException, TransformerException {
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
    // we want to pretty format the XML output
    // note : this is broken in jdk1.5 beta!
    transformer.setOutputProperty
       ("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    //
    java.io.StringWriter sw = new java.io.StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    return sw.toString();
 }
}

class AccessCon {
  public static Connection getConnection() throws Exception {
   Driver d = (Driver)Class.forName
    ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
   Connection c = DriverManager.getConnection
  ("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/tech97.mdb");
   return c;
   /*
   To use an already defined ODBC Datasource :

     String URL = "jdbc:odbc:myDSN";
     Connection c = DriverManager.getConnection(URL, "user", "pwd");

   */
  }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ResultSet size bugger Database 16 12-06-2009 08:39 PM
ResultSet to HTML Java Tip Java Tips 0 02-12-2008 10:32 AM
ResultSet to JTable Java Tip Java Tips 0 02-11-2008 10:01 AM
Empty ResultSet Java Tip Java Tips 0 02-09-2008 09:36 PM
ResultSet example Java Tip Java Tips 0 01-20-2008 09:59 AM


All times are GMT +2. The time now is 04:19 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org