Java Reference
In-Depth Information
You can click this link to open the file or save it to a folder on your system. The file,
which is only one line long, contains the stock's price and volume data saved at the last
market close. Here's an example of what Sun's data looked like on Feb. 23, 2007:
“SUNW”,6.27,”2/23/2007”,”4:00pm”,0.00,6.30,6.31,6.22,50254356
The fields in this data, in order, are the ticker symbol, closing price, date, time, price
change since yesterday's close, daily low, daily high, daily open, and volume.
The QuoteData application uses each of these fields except one—the time, which isn't
particularly useful because it's always the time the market closed.
The following takes place in the program:
The ticker symbol of a stock is taken as a command-line argument.
n
A QuoteData object is created with the ticker symbol as an instance variable called
ticker .
n
The object's retrieveQuote() method is called to download the stock data from
Yahoo! and return it as a String .
n
The object's storeQuote() method is called with that String as an argument. It
saves the stock data to a database using a JDBC-ODBC connection.
n
The last task requires a stock quote database, which can be reached through JDBC-
ODBC, set up to collect this data.
Windows users can download quotedata.mdb , an Access 2000 database created to hold
Yahoo!'s stock quote data, from the topic's website. Visit http://www.java21days.com
and open the Day 18 page. After you download the database (or create one of your own),
use the ODBC Data Source Administrator to create a new data source associated with the
database. This application assumes that the name of the source is QuoteData .
Enter the text of Listing 18.2 into your editor and save the file as QuoteData.java .
LISTING 18.2
The Full Text of QuoteData.java
1: import java.io.*;
2: import java.net.*;
3: import java.sql.*;
4: import java.util.*;
5:
6: public class QuoteData {
7: private String ticker;
8:
9: public QuoteData(String inTicker) {
Search WWH ::




Custom Search