Database Reference
In-Depth Information
If you use execute() , that method returns true or false to indicate whether the statement
produces a result set. For statements such as UPDATE or DELETE that return no result set,
execute() returns false and the row count is available by calling the getUpdate
Count() method:
Statement s = conn . createStatement ();
if (! s . execute ( stmt ))
{
// there is no result set, print the row count
System . out . println ( "Number of rows affected: " + s . getUpdateCount ());
}
s . close ();
10.2. Obtaining Result Set Metadata
Problem
You already know how to retrieve the rows of a result set (see Recipe 2.4 ). Now you want
to know things about the result set, such as the column names and data types, or how
many rows and columns there are.
Solution
Use the capabilities provided by your API.
Discussion
Statements such as SELECT that generate a result set produce several types of metadata.
This section discusses the information available through each API, using programs that
show how to display the result set metadata available after executing a sample statement
( SELECT name, birth FROM profile ). The example programs illustrate one of the sim‐
plest uses for this information: when you retrieve a row from a result set and you want
to process the column values in a loop, the column count stored in the metadata serves
as the upper bound on the loop iterator.
Perl
The scope of result set metadata available from Perl DBI depends on how you process
queries:
• Using a statement handle
In this case, invoke prepare() to get the statement handle. This handle has an
execute() method. Invoke it to generate the result set, then fetch the rows in a loop.
With this approach, access to the metadata is available while the result set is active
—that is, after the call to execute() and until the end of the result set is reached.
Search WWH ::




Custom Search