Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
18.3 Some JDBC Utilities
473
18.3 Some JDBC Utilities
In many applications, you don't need to process query results a row at a time.
For example, in servlets and JSP pages, it is common to simply format the
database results (treating all values as strings) and present them to the user in
an HTML table (see Sections 18.4 and 18.8), in an Excel spreadsheet (see
Section 11.2), or distributed throughout the page. In such a case, it simplifies
processing to have methods that retrieve and store an entire 
ResultSet
 for
later display. 
This section presents two classes that provide this basic functionality along
with a few formatting, display, and table creation utilities. The core class is
DatabaseUtilities
, which implements static methods for four common
tasks:
1.
getQueryResults
This method connects to a database, executes a query, retrieves 
all the rows as arrays of strings, and puts them inside a 
DBResults
 object (see Listing 18.7). This method also places 
the database product name, database version, the names of all 
the columns and the 
Connection
 object into the 
DBResults
object. There are two versions of 
getQueryResults
: one that 
makes a new connection and another that uses an existing con 
nection.
2.
createTable
Given a table name, a string denoting the column formats, and 
an array of strings denoting the row values, this method con 
nects to a database, removes any existing versions of the desig 
nated table, issues a 
CREATE TABLE
 command with the 
designated format, then sends a series of 
INSERT INTO
 com 
mands for each of the rows. Again, there are two versions: one 
that makes a new connection and another that uses an existing 
connection. 
3.
printTable
Given a table name, this method connects to the specified data 
base, retrieves all the rows, and prints them on the standard 
output. It retrieves the results by turning the table name into a 
query of the form  
SELECT * FROM tableName
  and passing it 
to 
getQueryResults
.
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