Java Reference
In-Depth Information
LISTING 18.2
Continued
59: prep2.executeUpdate();
60: conn.close();
61: } catch (SQLException sqe) {
62: System.out.println(“SQL Error: “ + sqe.getMessage());
63: } catch (ClassNotFoundException cnfe) {
64: System.out.println(cnfe.getMessage());
65: }
66: }
67:
68: private String stripQuotes(String input) {
69: StringBuffer output = new StringBuffer();
70: for (int i = 0; i < input.length(); i++) {
71: if (input.charAt(i) != '\”') {
72: output.append(input.charAt(i));
73: }
74: }
75: return output.toString();
76: }
77:
78: public static void main(String[] arguments) {
79: if (arguments.length < 1) {
80: System.out.println(“Usage: java QuoteData tickerSymbol”);
81: System.exit(0);
82: }
83: QuoteData qd = new QuoteData(arguments[0]);
84: String data = qd.retrieveQuote();
85: qd.storeQuote(data);
86: }
87: }
After you compile the QuoteData application, connect to the Internet and run the pro-
gram. Remember to specify a valid ticker symbol as a command-line argument. To load
the current quote for SUNW (Sun Microsystems):
java QuoteData SUNW
The retrieveQuote() method (lines 13-33) downloads the quote data from Yahoo! and
saves it as a string. The techniques used in this method were covered on Day 17,
“Communicating Across the Internet.”
The storeQuote() method (lines 35-66) uses the SQL techniques covered in this
section.
The method begins by splitting up the quote data into a set of string tokens, using the
comma character (“,”) as the delimiter between each token. The tokens are then stored in
a String array with nine elements.
 
Search WWH ::




Custom Search