Example 11-4: Generating XML using SQLJ

import java.sql.*;
import sqlj.runtime.ref.DefaultContext;

#sql iterator QuotesIter(String symbol, float price, float change);

class StockQuotesSqlj
{
  public static void main (String arg[]) throws Exception
  {
    QuotesIter quotes;
    // Connect to the Database
    DefaultContext.setDefaultContext(new DefaultContext(Examples.getConnection()));

    // Use first command line arg as customer id
    int id = Integer.parseInt( arg[0] );

    #sql quotes = { 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 = :id };

    System.out.println("<?xml version=\"1.0\"?>");
    System.out.println("<Quotes>");
    while (quotes.next()) {
      System.out.println("<Symbol>" + quotes.symbol() + "</Symbol>");
      System.out.println( "<Price>" + quotes.price()  + "</Price>") ;
      System.out.println("<Change>" + quotes.change() + "</Change>");
    }
    System.out.println("</Quotes>");
    quotes.close();
  }
}