Example 11-3: Using XMLForResultSet.print to produce XML

import java.sql.*;
import java.io.*;

public class StockQuotesXml {
  public static void main (String arg[]) throws Exception
  {
    // Use first command line arg as customer id
    int id = Integer.parseInt( arg[0] );
    print(id, System.out);
  }
  public static void print(int customerId, OutputStream out) throws Exception {
    // Connect to the database
    Connection cn = Examples.getConnection();
    // Prepare the query statement (Note column aliases!)
    PreparedStatement ps =
      cn.prepareStatement("SELECT q.symbol as \"Symbol\", " +
                          "       q.price  as \"Price\", " +
                          "       q.change as \"Change\" " +
                          "  FROM quotes q, portfolio_stocks ps" +
                          " WHERE q.symbol = ps.symbol" +
                          "   AND ps.owner = ?");
    // Bind value of the customerId parameter
    ps.setInt( 1, customerId );
    // Execute the Query
    ResultSet rs = ps.executeQuery();
    PrintWriter pw = new PrintWriter(out);
    // Generate the XML document for this ResultSet
    XMLForResultSet.print(rs,"Quotes","Quote", pw);
    pw.close(); rs.close(); ps.close(); cn.close();
  }
}