Java Reference
In-Depth Information
The array contains the same fields as the Yahoo! data in the same order: ticker symbol,
closing price, date, time, price change, low, high, open, and volume.
Next, a data connection to the QuoteData data source is created using the JDBC-ODBC
driver (lines 41-45).
This connection is then used to create a prepared statement (lines 46-50). This statement
uses the INSERT INTO SQL statement, which causes data to be stored in a database. In
this case, the database is quotedata.mdb , and the INSERT INTO statement refers to the
Stocks table in that database.
Eight placeholders are in the prepared statement. Only eight are needed, instead of nine,
because the application does not use the time field from the Yahoo! data.
A series of setString() methods puts the elements of the String array into the prepared
statement, in the same order that the fields exist in the database: ticker symbol, closing
price, date, price change, low, high, open, and volume (lines 51-58).
Some fields in the Yahoo! data are dates, floating-point numbers, and integers, so you
might think that it would be better to use setDate() , setFloat() , and setInt() for that
data.
Some versions of Access, including Access 2000, do not support some of these methods
when you are using SQL to work with the database, even though they exist in Java. If
you try to use an unsupported method, such as setFloat() , an SQLException error
occurs.
18
It's easier to send Access strings and let the database program convert them automati-
cally into the correct format. This is likely to be true when you are working with other
databases; the level of SQL support varies based on the product and ODBC driver
involved.
After the prepared statement has been prepared and all the placeholders are filled, the
statement's executeUpdate() method is called (line 59). This either adds the quote data
to the database or throws an SQL error. The private method stripQuotes() is used to
remove quotation marks from Yahoo!'s stock data. This method is called on line 39 to
take care of three fields that contain extraneous quotes: the ticker symbol, date, and time.
Moving Through Resultsets
The default behavior of resultsets permits one trip through the set using its next()
method to retrieve each record.
Search WWH ::




Custom Search