public abstract class DatabaseProperties extends Object
This is an abstract class and can't be used directly in the creation of objects. This means that subclasses must be created.
When creating subclasses, many fields will have to be set,
including
handlerClass
,
accountName
,
connectionString
, and
password
.
By including the objects for the JDBC connection and statement, it is possible to encapsulate a large amount of the code for the database connectivity.
Please note that the methods in this class do not include one for obtaining the password used in connecting to the database. If required, this method can be added in the subclass. Eliminating this method adds to the security of the system. By encapsulating the password in a field in this class for use by the connect method, there is no need for other classes to have access to the information.
SQL Server notes
Wants the string dbo.
in front of table names.
Specify the default database as part of the user configuration rather than including it in the connection string.
Oracle notes
Will not accept semicolons at the end of SQL statements.
MySQL notes
Modifier and Type | Field and Description |
---|---|
protected String |
accountName
The account name to be used on the database server.
|
protected boolean |
autoCommit
True indicates that autoCommit is set for this database connection when
the connection is opened.
|
protected Connection |
connection
The object representing the connection to the database.
|
protected String |
connectionString
The connection string to be used to connect to the database using
JDBC (Java DataBase Connectivity)
|
protected int |
debugLevel
Amount of diagnostic messages.
|
protected String |
defaultCatalog
Default catalog for methods allowing the use
of a default value.
|
protected String |
defaultEngine
When creating MySQL tables, this field can be
used by the getEngineClause to add the ENGINE
statement to CREATE TABLE statements.
|
protected String |
defaultSchema
Default schema for methods allowing the use
of a default value.
|
protected String |
domainName
Domain name for the system containing the database.
|
protected Driver |
driver |
protected DatabaseExtras |
extras
Used to represent associated DatabaseExtras object.
|
protected String |
handlerClass
Name of the handler class for the database.
|
protected boolean |
invalidParameters
If the parameters supplied for instantiating the object are
invalid, this field should be set to true.
|
protected boolean |
isConnected
Flag indicating whether database has been connected.
|
protected String |
password
The password to be used for accessing the database server.
|
protected int |
portNumber
Port number of the database connection.
|
protected String |
systemDescription
Optional field containing a human readable description of the
database.
|
protected String |
systemName
This is the name of the system identifier to identify
the database in database systems that can support multiple
databases.
|
protected String[] |
tableNamePrefixes
Set of prefixes for table names
|
Constructor and Description |
---|
DatabaseProperties()
Constructor using a default system and account name.
|
DatabaseProperties(String value)
Constructor specifying identifier for system and using
a default account name.
|
DatabaseProperties(String systemName,
String accountName)
Constructor specifying system and account name.
|
Modifier and Type | Method and Description |
---|---|
void |
clearTableNamePrefixes()
Clears the list of table name prefix values.
|
void |
close()
Close the database connection.
|
String |
comment(String text)
Returns the parameter as a DDL comment
if the database is of a type that allows comments in
DDL statements.
|
void |
commit()
Execute a commit statement for the SQL connection.
|
void |
connect()
Open the database connection.
|
static String |
decodeSqlType(int value)
Decode the values of constants in java.sql.TYPES to
String values.
|
void |
dumpResultSet(ResultSet rs,
String targetTableName,
GenericPrinter output)
Dumps a result set as a set of INSERT SQL statements.
|
void |
dumpResultSet(ResultSet rs,
String targetTableName,
PrintStream outputStream)
Dumps a result set to a PrintStream object
|
void |
dumpResultSet(ResultSet rs,
String targetTableName,
PrintWriter outputWriter)
Dumps a result set to a PrintWriter object
|
void |
dumpTable(String table,
PrintStream output)
Dump contents of database to PrintStream object
|
void |
dumpTable(String table,
PrintWriter output)
Dump contents of database table to PrintWriter object.
|
void |
dumpTable(String sourceTableName,
String targetTableName,
GenericPrinter output) |
protected void |
dumpTable(String sourceTableName,
String targetTableName,
PrintWriter outputWriter,
PrintStream outputStream)
Dumps the contents of a table as a series of SQL statements.
|
ResultSet |
executeQuery(String sqlCode)
This class executes an SQL query and returns a
ResultSet.
|
String |
executeSimpleQuery(String sqlCode)
This class reads the first field on the first record from
an SQL Query.
|
int |
executeUpdate(String sqlCode)
Execute SQL statement without returning results.
|
String |
getAccountName()
Return the account name used for the database connection.
|
Connection |
getConnection()
Obtain the connection object for the database connection.
|
DatabaseExtras |
getDatabaseExtras()
Get value of extras.
|
String |
getDateTerm()
Return the expression used in SQL statements to
represent a data type for holding a date.
|
String |
getDatetimeTerm()
Return the expression used in SQL statements to
represent a data type for holding a date and time.
|
String |
getDbms()
Get name of database management system
|
int |
getDebugLevel()
Gets the amount of diagnostic messages to be printed.
|
String |
getDefaultCatalog()
Get the value of defaultCatalog.
|
String |
getDefaultSchema()
Gets value of defaultSchema
|
Driver |
getDriver()
Returns the instance of the JDBC driver used to connect
to the database.
|
String |
getEngineClause()
Create optional ENGINE clause
|
String |
getLongblobTerm()
Return the data type used for large BLOBS
(Binary Large Objects).
|
String |
getNowTerm()
Return the expression used in SQL statements to
obtain the current time.
|
String |
getSystem()
Return the system identifier used for the database connection.
|
String |
getSystemDescription()
Getter for systemDescription
|
String |
getTableNamePrefixes()
Get the value of the default table name prefix (item 0)
|
String |
getTableNamePrefixes(int i)
Get the value of a table name prefix
|
boolean |
hasInvalidParameters()
Determine whether parameters for constructor were valid.
|
void |
insertRow(String tableName,
String[] columnNames,
Object... value)
Deprecated.
|
void |
insertRowWithKeys(String tableName,
String[] columnNames,
int columnCount,
Object... value)
Insert rows in a table where there is more than one column in
the primary key.
|
String |
prefixTableName(int type,
String value)
Prefixes a table name with a value that was set as part of the
database connection.
|
String |
prefixTableName(String value)
Prefixes a table name with a value that was set as part of the database
connection.
|
PreparedStatement |
prepareStatement(String query)
Generate a prepared statement for this database.
|
void |
rollback()
Execute a rollback statement for the SQL connection.
|
void |
setAccountName(String value)
Setter for accountName.
|
void |
setAutoCommit(boolean value)
Set autoCommit flag
|
void |
setConnectionString(String value)
Specify the connection string to be used.
|
void |
setDatabaseExtras(DatabaseExtras value)
Set value of extras.
|
abstract void |
setDatabaseInstance()
Internal program for setting up connection using default
database and account.
|
abstract void |
setDatabaseInstance(String system)
Internal program for setting up connection using specified
database with a default account for the database.
|
abstract void |
setDatabaseInstance(String system,
String user)
Internal program for setting up connection with a specified
database and account name.
|
void |
setDebugLevel(int level)
Set the amount of diagnostic messages.
|
void |
setDefaultCatalog(String value)
Sets value of defaultCatalog.
|
void |
setDefaultSchema(String value)
Sets value of defaultSchema.
|
void |
setDomainName(String value)
Setter for domainName
|
void |
setHandlerClass(String value)
Set the class name to be used for the handler class.
|
protected void |
setInvalidParameters(boolean value)
Set value of invalidParameters to indicate that constructor is not valid.
|
void |
setPassword(String value)
Sets the password to be used for the connection
when it is desired to avoid placing the value in
the program.
|
void |
setPortNumber(int value)
Setter for port number
|
void |
setSystemName(String value)
Setter for systemName.
|
String |
showAttributes()
Return attributes of the connection properties
|
protected int debugLevel
A value of zero indicates that no diagnostic messages should be displayed. The higher the value, the more diagnostic messages will be generated.
protected DatabaseExtras extras
protected String[] tableNamePrefixes
protected boolean autoCommit
protected String systemDescription
protected String systemName
protected String handlerClass
Product | Handler Class | Default Port |
MySQL | com.mysql.jdbc.Driver | 3306 |
Oracle | oracle.jdbc.driver.OracleDriver | 1521 |
SQL Server | com.microsoft.sqlserver.jdbc.SQLServerDriver | 1433 |
It may be necessary to use the name of the handlerClass in generating the SQL statements, since the syntax for different databases such as MySQL, Oracle, DB2, Postgres, etc. differ for many of the built in functions.
Although there is no get method for this field, one can be added when creating subclasses.
protected int portNumber
The default port number is shown in the discussion of the handler class names. If a port number other than the default is used, it is included as part of the domain name and connection string.
protected String connectionString
The connection string is composed as a series of tokens
separated by colons. The first token is always
jdbc
while the second is the name
of the database manager product. The remaining
tokens are dependent on the DBMS package used.
The following are examples of connection strings.
MySQL | jdbc:mysql://server.foo.com/alpha |
alpha is default database |
Oracle | jdbc:oracle:thin:@//server.foo.com/weather |
Connects to weather service The options thin and oci refer to the two types of JDBC connections. The OCI method requires the installation of additional pieces of software and uses SQL*Net to connect to the database. |
SQL Server | jdbc:sqlserver://server.foo.com |
SQL Server allows you to specify a default database as part of the user configuration. |
protected String domainName
The domain name is included as part of the connection string.
protected String accountName
protected String password
It should be noted that there is no get method for the field. This improves security for the package. If necessary, a get method can be added in a subclass.
protected String defaultEngine
getEngineClause()
protected String defaultCatalog
getDefaultCatalog()
,
setDefaultCatalog(String)
protected String defaultSchema
getDefaultSchema()
,
setDefaultSchema(String)
protected Connection connection
getConnection()
protected boolean isConnected
protected boolean invalidParameters
public DatabaseProperties()
public DatabaseProperties(String value)
value
- Identifier code for systempublic DatabaseProperties(String systemName, String accountName)
systemName
- Identifier code for systemaccountName
- Account name to be usedpublic abstract void setDatabaseInstance()
setDatabaseInstance(String, String)
public abstract void setDatabaseInstance(String system)
system
- Identifying code for databasepublic abstract void setDatabaseInstance(String system, String user)
system
- Identifier code for databaseuser
- Account name to be usedpublic void setDatabaseExtras(DatabaseExtras value)
value
- Object to be used for DatabaseExtras object.extras
public DatabaseExtras getDatabaseExtras() throws NotSupportedException
NotSupportedException
extras
public String getTableNamePrefixes()
public String getTableNamePrefixes(int i)
i
- Location of table name prefix in listpublic String getSystemDescription()
systemDescription
public String getDefaultCatalog()
defaultCatalog
public void setDefaultCatalog(String value)
value
- Value to be used for defaultCatalogdefaultCatalog
public String getDefaultSchema()
defaultSchema
public void setDefaultSchema(String value)
value
- Value to be used for defaultSchemadefaultSchema
public void setDebugLevel(int level)
level
- Amount of diagnostic messages.public int getDebugLevel()
public Driver getDriver()
public String getSystem()
public String getDbms()
The database name is extracted from the connection string as the second token of the string.
public String getAccountName()
public String getEngineClause()
defaultEngine
public void connect() throws SQLException
SQLException
public void close() throws SQLException
SQLException
public Connection getConnection()
public int executeUpdate(String sqlCode) throws SQLException
This method is usually used to make updates to the database.
sqlCode
- SQL code to be executed.SQLException
public String executeSimpleQuery(String sqlCode) throws SQLException
Since only the first field is read, the statement is closed after reading the field. This eliminates the possibility of leaving a statement open if it returns multiple rows.
sqlCode
- SQL code to be executed.SQLException
public ResultSet executeQuery(String sqlCode) throws SQLException
sqlCode
- SQL code to be executed.SQLException
public void commit()
public void rollback()
public void setAutoCommit(boolean value)
value
- Value to be used for setting autoCommit flagpublic String showAttributes()
public void setAccountName(String value)
Must be used before database connection is made.
value
- Value for accountNamepublic void setConnectionString(String value)
Do not specify the account name or password as part of the connection string.
value
- Connection string to be usedconnectionString
public void setDomainName(String value)
Does not include port number
value
- Value to be used for domain namedomainName
public void setHandlerClass(String value)
value
- Name of handler classhandlerClass
public void setPassword(String value)
value
- Value to be used for the passwordpublic void setPortNumber(int value)
value
- port number to be used for connectionportNumber
public void setSystemName(String value)
value
- Value to be used for system namesystemName
public void clearTableNamePrefixes()
public String prefixTableName(int type, String value)
type
- Location of the desired prefix in the tableNamePrefixes listvalue
- Table name to be prefixedpublic String prefixTableName(String value)
This version of the method uses the first entry in the tableNamePrefixes array. (index=0)
value
- Table name to be prefixedpublic PreparedStatement prepareStatement(String query) throws SQLException
query
- SQL Query to be used in statementSQLException
public String comment(String text)
SQL Server does not allow comments in DDL statements.
text
- Text to be used in commentpublic String getLongblobTerm()
public String getNowTerm()
public String getDateTerm()
SQL Server 2000 and 2005 do not have a DATE data type, and SMALLDATETIME should be used instead.
public String getDatetimeTerm()
It appears that Oracle refers to this data type as TIMESTAMP.
public static String decodeSqlType(int value)
value
- Value of constantTypes
protected void setInvalidParameters(boolean value)
value
- Value to be used for notSupportedpublic boolean hasInvalidParameters()
public void dumpTable(String table, PrintWriter output)
table
- Name of table to be dumpedoutput
- Destination PrintWriterpublic void dumpTable(String table, PrintStream output)
table
- Name of table to be dumpedoutput
- Destination PrintStreamprotected void dumpTable(String sourceTableName, String targetTableName, PrintWriter outputWriter, PrintStream outputStream)
An OutputStreamWriter can be created from an OutputStream using the
constructor OutputStreamWriter(OutputStream out)
.
The objects System.out
and System.err
are
PrintStream objects. A PrintStream object can be created from an
OutputStream object using the constructor
PrintStream(OutputStream out)
.
A PrintStream object can be created from a File object using the
constructor
PrintStream (File out)
.
sourceTableName
- Table to be dumpedtargetTableName
- Table name to be used in SELECT statementoutputWriter
- PrintWriter object to which information is to be passedoutputStream
- PrintStream object to which information is to be passedpublic void dumpTable(String sourceTableName, String targetTableName, GenericPrinter output)
public void dumpResultSet(ResultSet rs, String targetTableName, PrintWriter outputWriter)
rs
- Result SettargetTableName
- Table name to be used in INSERT statementoutputWriter
- Destination PrintWriter objectpublic void dumpResultSet(ResultSet rs, String targetTableName, PrintStream outputStream)
rs
- Result settargetTableName
- Table name to be used in INSERT statementoutputStream
- Destination PrintStream objectpublic void dumpResultSet(ResultSet rs, String targetTableName, GenericPrinter output)
rs
- Result set to be dumpedtargetTableName
- Table name to be used in INSERT statementsoutput
- GenericPrinter object to which information is passedpublic void insertRow(String tableName, String[] columnNames, Object... value) throws SQLException
The primary key should be the first column in the list.
tableName
- Name of the tablecolumnNames
- Array containing column names in the database
table for the columns to be populated with data.value
- Values to be inserted in the columnsSQLException
public void insertRowWithKeys(String tableName, String[] columnNames, int columnCount, Object... value) throws SQLException
tableName
- Name of the tablecolumnNames
- Array containing column names in the database
table.columnCount
- Number of columns in primary key. These are the first
columns in the list.value
- Values to be inserted in the columnsSQLException