Java Tutorial - Java Script : The JDBC-ODBC Bridge

Java Tutorial - Java Script : 

The JDBC-ODBC Bridge

The next project demonstrates the use of a prepared statement to add stock quote data to a database. Quotes are collected from the Yahoo! website. As a service to people who follow the stock market, Yahoo! offers a Download Spreadsheet link on its main stock quote page for each ticker symbol. To see this link, look up a stock quote on Yahoo! or go directly to a page such as this one: http://quote.yahoo.com/q?s=sunw&d=v1 Below the price chart, you can find a Download Data link. Here’s what the link looks like for Sun Microsystems:
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.
·         A QuoteData object is created with the ticker symbol as an instance variable called ticker.
·         The object’s retrieveQuote() method is called to download the stock data from Yahoo! and return it as a String.
·         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.
The last task requires a stock quote database, which can be reached through JDBCODBC, 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 book’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.
After you compile the QuoteData application, connect to the Internet and run the program. Remember to specify a valid ticker symbol as a command-line argument. To load the current quote for SUNW (Sun Microsystems):