Java Reference
In-Depth Information
is returned by the createStatement() method of a Connection object, as in the follow-
ing example:
Statement lookSee = payday.CreateStatement();
After you have a Statement object, you can use it to conduct an SQL query by calling
the object's executeQuery( String ) method. The String argument should be an SQL
query that follows the syntax of that language.
It's beyond the scope of today's lesson to teach SQL, a rich, data-
retrieval and storage language that has its own book in this
series: Sams Teach Yourself SQL in 21 Days, 4th Edition by Ron
Plew and Ryan Stephens (ISBN: 0-672-32451-2). Although you
need to learn SQL to do any extensive work with it, much of the
language is easy to pick up from any examples you can find, such
as those you will work with today.
CAUTION
The following is an example of an SQL query that could be used on the Coal table of the
world20.mdb database:
SELECT Country, Year, 'Anthracite Production' FROM Coal
WHERE (Country Is Not Null) ORDER BY Year
This SQL query retrieves several fields for each record in the database for which the
Country field is not equal to null. The records returned are sorted according to their
Country field, so Afghanistan would precede Burkina Faso.
The following Java statement executes that query on a Statement object named looksee :
ResultSet set = looksee.executeQuery(
“SELECT Country, Year, 'Anthracite Production' FROM Coal “
+ “WHERE (Country Is Not Null) ORDER BY Year”);
If the SQL query has been phrased correctly, the executeQuery() method returns a
ResultSet object holding all the records that have been retrieved from the data source.
To add records to a database instead of retrieving them, the state-
ment's executeUpdate() method should be called. You will work
with this later.
NOTE
 
Search WWH ::




Custom Search