|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.cleancode.jdbc.DBprobe
public class DBprobe
A simple probe to explore data and metadata of different vendors' databases. With DBprobe, you specify a "path" to a database, i.e. a driver, a connection, a server, a db name, user name, and password. DBprobe then attempts to query the database for the highest level information, i.e. what tables does it contain, both to ascertain what database type it is (oracle, mysql, etc.) and to display this metadata to you. You may optionally specify a specific table in which case DBprobe will provide metadata about it, i.e. its columns and properties of each column. Alternatively, you may specify a specific query, if you wish to ascertain other information. Invoke the main program with no arguments for details on execution parameters.
DBprobe is a simple, command-line tool for database exploration. For more flexibility and a fancier interface, consider the JDBCTest tool available directly from Sun.
Another excellent learning resource is SQLzoo.net, which allows you to try out a variety of basic SQL statements in 6 different database systems at the click of a button (see the Meta Data section).
Connection details for one or more databases may be stored
in a configuration file (CONFIG_FILE
) in the current directory,
or may be supplied on the command line invocation.
You may store multiple sets using the
InputOptions
parameter engine,
allowing you to select at runtime the set of parameters you wish
to use via the
MODE
property.
For example, here we specify two sets of parameters, one for the
database my_reports
on server ips1
,
and one for database my_data
on machine integration
:
SERVER:ips1 = ips_server1 DBNAME:ips1 = my_reports USERID:ips1 = myuid1 PASSWD:ips1 = mypass1 TABLE:ips1 = COUNTRIES SERVER:integ = integration DBNAME:integ = my_data USERID:integ = myuid2 PASSWD:integ = mypass2 TABLE:integ = STUFFAt runtime, we simply add
MODE=ips1
or MODE=integ
to select one or the other parameter sets.
DEFAULT_DB_DRIVER
) -- the Java object
class that defines the database driver.DEFAULT_DB_CONN
--
the database connection.DB_TABLE
describes the database itself;
any other table name describes that table and dumps its contents,
in addition to describing the database itself.
TABLE
must be the name of an actual table, not the
pseudo-table DB_TABLE
.
If QUERY
is not supplied, all data in the table
is dumped via the query SELECT * FROM TABLE
.
A second configuration file (DB_SPEC_FILE
) specifies
the known database systems to be used to try to identify any
given database connection.
This is an XML file with a dbspecs root, and
a collection of server children.
Each server has two sub-elements,
tableQuery, used to return a list of tables in a database; and
columnQuery, used to return a list of columns in a table.
The tableQuery is used to identify the type of database.
Each one is attempted to be executed; if it returns results and does
not cause an exception, then it has successfully identified a database.
Both the tableQuery and the columnQuery are then used to collect and
display meta-data about the database and optionally, the selected table.
Here is an excerpt from the default configuration file:
<dbspecs> <server name="mysql"> <tableQuery>show tables</tableQuery> <columnQuery>show columns from -TABLENAME-</columnQuery> </server> <server name="oracle"> <tableQuery>SELECT * FROM cat</tableQuery> <columnQuery>SELECT * FROM all_tab_columns WHERE table_name='-TABLENAME-'</columnQuery> </server> </dbspecs>
Field Summary | |
---|---|
static String |
CONFIG_FILE
Configuration file name (in current directory) detailing runtime parameters and specifics of database instances. |
static String |
DB_SPEC_FILE
Configuration file detailing available database systems. |
static String |
DB_TABLE
Pseudo-table name indicating to describe only the database itself. |
static String |
DEFAULT_DB_CONN
Default database connection. |
static String |
DEFAULT_DB_DRIVER
Default database driver. |
static String |
MARKER
Marker to use in database specs file as a placeholder for table name. |
Constructor Summary | |
---|---|
DBprobe(InputOptions inputOptions,
String dbSpecFile)
Create a standard DBprobe object. |
Method Summary | |
---|---|
void |
complete()
Closes the database connection. |
void |
dumpMetaTable(String title,
ResultSet rs)
Describes the results of a database meta-query, that is, a query that returns either a list of tables or a list of fields for a specific table. |
void |
dumpTable(ResultSet rs)
Displays a result set. |
Connection |
getConnection()
Retrieves the database Connection object. |
String |
getFieldQuery(String tableName)
Generates a query to return a list of fields for a given table. |
String |
getServerName()
Retrieves the database server name. |
Statement |
getStatement()
Retrieves the database Statement object. |
boolean |
identifyDB()
Attempts to identify the database (access, mysql, oracle, etc) by running a basic query to list the database tables for each known database type. |
boolean |
init()
Initializes the DB resources, the DB connection, and setting up the DB statement. |
String |
listDBChoices()
Lists the predefined databases known to the system. |
static void |
main(String[] args)
Main program for standalone operation. |
static void |
usage(DBprobe myDB)
Displays the program usage. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String CONFIG_FILE
public static final String DB_SPEC_FILE
public static final String DEFAULT_DB_DRIVER
public static final String DEFAULT_DB_CONN
public static final String DB_TABLE
public static final String MARKER
Constructor Detail |
---|
public DBprobe(InputOptions inputOptions, String dbSpecFile)
inputOptions
- user parameter objectdbSpecFile
- name of file containing DB specsMethod Detail |
---|
public Connection getConnection()
public Statement getStatement()
public String getServerName()
public boolean init()
public void complete()
public String listDBChoices()
public boolean identifyDB()
public String getFieldQuery(String tableName)
tableName
- table for which to construct a query
public void dumpMetaTable(String title, ResultSet rs)
title
- display tablers
- resultSet from an appropriate querypublic void dumpTable(ResultSet rs)
rs
- result set to dump.public static void usage(DBprobe myDB)
myDB
- DBprobe object that can be queried for known databases
to include in the description.public static void main(String[] args)
args
- command-line arguments
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
CleanCode Java Libraries | Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 |