Java Tutorial - Java Script :
The
The JDBC-ODBC Bridge
java QuoteData SUNWThe 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. 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-ODBCdriver (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. It’s easier to send Access strings and let the database program convert them automatically 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.
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. 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-ODBCdriver (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. It’s easier to send Access strings and let the database program convert them automatically 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.
