Order the Book

coreservlets
Class DatabaseUtilities

java.lang.Object
  |
  +--coreservlets.DatabaseUtilities

public class DatabaseUtilities
extends Object

Three database utilities:
1) getQueryResults. Connects to a database, executes a query, retrieves all the rows as arrays of strings, and puts them inside a DBResults object. Also places the database product name, database version, and the names of all the columns into the DBResults object. This has two versions: one that makes a new connection and another that uses an existing connection.

2) createTable. Given a table name, a string denoting the column formats, and an array of strings denoting the row values, this method connects to a database, removes any existing versions of the designated table, issues a CREATE TABLE command with the designated format, then sends a series of INSERT INTO commands 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 connects to the specified database, retrieves all the rows, and prints them on the standard output.

Taken from Core Servlets and JavaServer Pages from Prentice Hall and Sun Microsystems Press, http://www.coreservlets.com/. © 2000 Marty Hall; may be freely used or adapted.


Constructor Summary
DatabaseUtilities()
           
 
Method Summary
static Connection createTable(Connection connection, String username, String password, String tableName, String tableFormat, String[] tableRows, boolean close)
          Like the previous method, but uses existing connection.
static Connection createTable(String driver, String url, String username, String password, String tableName, String tableFormat, String[] tableRows, boolean close)
          Build a table with the specified format and rows.
static DBResults getQueryResults(Connection connection, String query, boolean close)
          Retrieves results as in previous method but uses an existing connection instead of opening a new one.
static DBResults getQueryResults(String driver, String url, String username, String password, String query, boolean close)
          Connect to database, execute specified query, and accumulate results into DBRresults object.
static void printTable(Connection connection, String tableName, int entryWidth, boolean close)
          Prints out all entries in a table.
static void printTable(String driver, String url, String username, String password, String tableName, int entryWidth, boolean close)
           
static void printTableData(String tableName, DBResults results, int entryWidth, boolean printMetaData)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DatabaseUtilities

public DatabaseUtilities()
Method Detail

getQueryResults

public static DBResults getQueryResults(String driver,
                                        String url,
                                        String username,
                                        String password,
                                        String query,
                                        boolean close)
Connect to database, execute specified query, and accumulate results into DBRresults object. If the database connection is left open (use the close argument to specify), you can retrieve the connection with DBResults.getConnection.

getQueryResults

public static DBResults getQueryResults(Connection connection,
                                        String query,
                                        boolean close)
Retrieves results as in previous method but uses an existing connection instead of opening a new one.

createTable

public static Connection createTable(String driver,
                                     String url,
                                     String username,
                                     String password,
                                     String tableName,
                                     String tableFormat,
                                     String[] tableRows,
                                     boolean close)
Build a table with the specified format and rows.

createTable

public static Connection createTable(Connection connection,
                                     String username,
                                     String password,
                                     String tableName,
                                     String tableFormat,
                                     String[] tableRows,
                                     boolean close)
Like the previous method, but uses existing connection.

printTable

public static void printTable(String driver,
                              String url,
                              String username,
                              String password,
                              String tableName,
                              int entryWidth,
                              boolean close)

printTable

public static void printTable(Connection connection,
                              String tableName,
                              int entryWidth,
                              boolean close)
Prints out all entries in a table. Each entry will be printed in a column that is entryWidth characters wide, so be sure to provide a value at least as big as the widest result.

printTableData

public static void printTableData(String tableName,
                                  DBResults results,
                                  int entryWidth,
                                  boolean printMetaData)

Order the Book

Order
CSAJSP @ amazon.com

Return to Top-Level Source Code Archive