import java.sql.*; class StockQuotesXmlJdbc { public static void main (String arg[]) throws Exception { // Connect to the database Connection cn = Examples.getConnection(); // Prepare the query statement containing a bind variable "?" PreparedStatement ps = cn.prepareStatement("SELECT q.symbol, q.price, q.change" + " FROM quotes q, portfolio_stocks ps" + " WHERE q.symbol = ps.symbol" + " AND ps.owner = ?"); // Use first command line arg as customer id int id = Integer.parseInt( arg[0] ); // Bind value of customer id to first (and only) bind variable ps.setInt( 1, id ); // Execute the Query ResultSet rs = ps.executeQuery(); // Generate the XML document System.out.println("<?xml version=\"1.0\"?>"); System.out.println("<Quotes>"); // Loop over the rows in the query result while (rs.next ()) { System.out.println("<Quote>"); System.out.println("<Symbol>" + rs.getString(1) + "</Symbol>"); System.out.println( "<Price>" + rs.getString(2) + "</Price>") ; System.out.println("<Change>" + rs.getString(3) + "</Change>"); System.out.println("</Quote>"); } System.out.println("</Quotes>"); rs.close(); ps.close(); cn.close(); } } |