Java Reference
In-Depth Information
PL Poland 1994 242.50849909616
PL Poland 1995 304.237935229728
PL Poland 1996 308.64718066784
PL Poland 1997 319.67029426312
PL Poland 1998 319.67029426312
Try running the program with other countries that produce anthracite, such as France,
Swaziland, and New Zealand. For any country that has a space in the name, remember to
put quotation marks around the country name when running the program.
Writing Data to a Database Using SQL
In the CoalReporter application, you retrieved data from a database using an SQL state-
ment prepared as a string, like this:
SELECT * FROM Coal WHERE (Country='Swaziland') ORDER BY YEAR
This is a common way to use SQL. You could write a program that asks a user to enter
an SQL query and then displays the result (though this isn't a good idea—SQL queries
can be used to delete records, tables, and even entire databases).
The java.sql package also supports another way to create an SQL statement: a prepared
statement.
18
A prepared statement, which is represented by the PreparedStatement class, is an SQL
statement that is compiled before it is executed. This enables the statement to return data
more quickly and is a better choice if you are executing an SQL statement repeatedly in
the same program.
Prepared statements also have another advantage on Windows
systems: They make it possible to write data to an Access data-
base using the JDBC-ODBC driver. I've had little luck writing data
from Java to Access using statements but can use prepared state-
ments without any trouble.
TIP
To create a prepared statement, call a connection's prepareStatement( String ) method
with a string that indicates the structure of the SQL statement.
To indicate the structure, you write an SQL statement in which parameters have been
replaced with question marks.
Search WWH ::




Custom Search