Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
466
Chapter 18 JDBC and Database Connection Pooling
while(resultSet.next()) {
  System.out.println(results.getString(1) + " " +
                     results.getString(2) + " " +
                     results.getString(3));
}
In addition to the 
get
Xxx and 
next
 methods, other useful methods in the
ResultSet
 class include 
findColumn
 (get the index of the named column),
wasNull
 (was the last 
get
Xxx result SQL 
NULL
? Alternatively, for strings you
can simply compare the return value to 
null
), and 
getMetaData
 (retrieve
information about the 
ResultSet
 in a 
ResultSetMetaData
 object). 
The 
getMetaData
 method is particularly useful. Given only a 
ResultSet
,
you have to know about the name, number, and type of the columns to be
able to process the table properly. For most fixed format queries, this is a rea 
sonable expectation. For ad hoc queries, however, it is useful to be able to
dynamically discover high level information about the result. That is the role
of the 
ResultSetMetaData
 class: it lets you determine the number, names,
and types of the columns in the 
ResultSet
. Useful 
ResultSetMetaData
methods include 
getColumnCount
 (the number of columns), 
getColumn 
Name(colNumber)
 (a column name, indexed starting at 1), 
getColumnType
(an 
int
 to compare against entries in 
java.sql.Types
), 
isReadOnly
 (is
entry a read only value?), 
isSearchable
 (can it be used in a 
WHERE
 clause?),
isNullable
 (is a null value permitted?), and several others that give details
on the type and precision of the column. 
ResultSetMetaData
 does not
include the number of rows, however; the only way to determine that is to
repeatedly call 
next
 on the 
ResultSet
 until it returns 
false
.
Close the Connection
To close the connection explicitly, you would do:
connection.close();
You should postpone this step if you expect to perform additional database
operations, since the overhead of opening a connection is usually large. In
fact, reusing existing connections is such an important optimization that Sec 
tion 18.7 develops a library just for that purpose and Section 18.8 shows some
typical timing results.
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved