public class JDBCUtil
extends java.lang.Object
implements java.io.Serializable
Modifier and Type | Field and Description |
---|---|
private static java.text.DateFormat |
FMT_DATE
The formatter used to convert date strings (JDBCUtil.format.date).
|
private static java.text.DateFormat |
FMT_TIME
The formatter used to convert time strings (JDBCUtil.format.time).
|
private static java.text.DateFormat |
FMT_TS
The formatter used to convert timestamp strings (JDBCUtil.format.timestamp).
|
private static java.lang.String[] |
RESERVED
The configured set of SQL reserved words (JDBCUtil.reserved.words).
|
private static long |
serialVersionUID
The class' serialization version id.
|
Constructor and Description |
---|
JDBCUtil() |
Modifier and Type | Method and Description |
---|---|
static boolean |
copyTable(JDBCConnectionInfo cninf,
java.lang.String source,
java.lang.String target)
Copies data from a source table to a target table.
|
static int |
countIndexes(JDBCConnectionInfo cninf,
java.lang.String tblname)
Returns the number of existing indexes in the database for a table.
|
static int |
countTableColumns(JDBCConnectionInfo cninf,
java.lang.String tblname)
Returns the number of existing columns in the database for a table.
|
static int |
countTables(JDBCConnectionInfo cninf)
Returns the number of existing tables in the database.
|
static int |
countViews(JDBCConnectionInfo cninf)
Returns the number of existing views in the database.
|
static boolean |
createIndex(JDBCConnectionInfo cninf,
java.lang.String tblname)
Creates a database table's indexes using definitions in the application's
resource file.
|
static boolean |
createTable(JDBCConnectionInfo cninf,
java.lang.String tblname)
Creates a database table using definitions in the application's resource
file.
|
static boolean |
createTable(JDBCConnectionInfo cninf,
java.lang.String source,
java.lang.String target)
Creates a database table that mirrors the definition of an existing table.
|
static boolean |
createView(JDBCConnectionInfo cninf,
java.lang.String viewname)
Creates a database view using definitions in the application's resource
file.
|
static java.lang.String |
dateToString(java.sql.Date date)
Returns a date string for the given date using the FMT_DATE formatter
to format the date.
|
static java.lang.String |
dateToString(java.util.Date date)
Returns a date string for the given date using the FMT_DATE formatter
to format the date.
|
static boolean |
deleteIndex(JDBCConnectionInfo cninf,
java.lang.String tblname)
Deletes a database table's existing indexes.
|
static boolean |
deleteTable(JDBCConnectionInfo cninf,
java.lang.String tblname)
Deletes an existing database table.
|
static boolean |
deleteView(JDBCConnectionInfo cninf,
java.lang.String viewname)
Deletes an existing database view.
|
static void |
dumpFieldNames(java.sql.ResultSet rs)
Dumps the given result set's field names to System.out.
|
static void |
dumpFieldValues(java.sql.ResultSet rs)
Dumps the given result set's field value to System.out.
|
static java.lang.String |
getMetaSchema(JDBCConnectionInfo cninf)
Returns the schema name required when obtaining various types of database
metadata from a JDBC connection; such as tables, columns, keys, and views.
|
static java.sql.ResultSet |
getRSColumn(JDBCConnectionInfo cninf,
java.lang.String tblname,
java.lang.String colname)
Returns a result set of the columns in the given table.
|
static java.sql.ResultSet |
getRSColumns(JDBCConnectionInfo cninf,
java.lang.String tblname)
Returns a result set of the columns in the given table.
|
static java.sql.ResultSet |
getRSIndexes(JDBCConnectionInfo cninf,
java.lang.String tblname)
Returns a result set of all indexes in the database for the given table.
|
static java.sql.ResultSet |
getRSPrimary(JDBCConnectionInfo cninf,
java.lang.String tblname)
Returns a result set of the primary keys in the given table.
|
static java.sql.ResultSet |
getRSTable(JDBCConnectionInfo cninf,
java.lang.String tableName)
Returns a result set of the given table in the database.
|
static java.sql.ResultSet |
getRSTables(JDBCConnectionInfo cninf)
Returns a result set of the tables in the database.
|
static java.sql.ResultSet |
getRSTypes(JDBCConnectionInfo cninf)
Returns a result set of the supported sql types in the database.
|
static java.sql.ResultSet |
getRSViews(JDBCConnectionInfo cninf)
Returns a result set of the views in the database.
|
static java.util.ArrayList<java.lang.String> |
getTableNames(JDBCConnectionInfo cninf)
Returns the names of the tables in the database in table name sequence.
|
static java.lang.String |
getTablePrimary(JDBCConnectionInfo cninf,
java.lang.String tblname)
Returns a comma delimited string of a table's primary key field names.
|
static java.sql.ResultSet |
getTableRecords(JDBCConnectionInfo cninf,
java.lang.String tblname)
Returns a result set of the records in a database table.
|
static java.util.ArrayList<java.lang.String> |
getViewNames(JDBCConnectionInfo cninf)
Returns the names of the views in the database in view name sequence.
|
static java.lang.String |
quoteUnsafeWord(java.lang.String word)
Returns a quoted upper case word for a given word.
|
static java.lang.String |
quoteUnsafeWords(java.lang.String words)
Returns quoted upper case words for a given set of comma delimited words.
|
static java.lang.String[] |
quoteUnsafeWords(java.lang.String[] words)
Returns quoted upper case words for a given array of words.
|
static boolean |
reorgTable(JDBCConnectionInfo cninf,
java.lang.String tblname)
Reorganizes the table with the given name.
|
static java.lang.String |
statusToString(java.lang.String code)
Returns a formatted string for the given object status code.
|
static java.sql.Date |
stringToDate(java.lang.String date)
Returns a date object for the given date string using the FMT_DATE
formatter to parse the given string.
|
static java.sql.Timestamp |
stringToTimestamp(java.lang.String ts)
Returns a timestamp object for the given timestamp string using the
FMT_TS formatter to parse the given string.
|
static java.lang.String |
timestampToString(java.sql.Timestamp ts)
Returns a timestamp string for the given time using the FMT_TS formatter
to format the timestamp.
|
static java.lang.String |
timeToString(java.sql.Date date)
Returns a time string for the given date using the FMT_TIME formatter
to format the date.
|
static java.lang.String |
timeToString(java.util.Date date)
Returns a time string for the given date using the FMT_TIME formatter
to format the date.
|
static java.lang.String |
unquoteWord(java.lang.String word)
Removes beginning and ending double quotes from a word.
|
private static final long serialVersionUID
private static final java.text.DateFormat FMT_DATE
private static final java.text.DateFormat FMT_TIME
private static final java.text.DateFormat FMT_TS
private static final java.lang.String[] RESERVED
public static boolean copyTable(JDBCConnectionInfo cninf, java.lang.String source, java.lang.String target)
NOTE: The target table need not be an exact mirror of the source table; however, fields in the two tables having the same name must be the same data type.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.source
- (String) The name of the database table that contains
the data to copy.target
- (String) The name of the database table where the data
is to be copied.true
if the copying is successful,
otherwise false
.public static int countIndexes(JDBCConnectionInfo cninf, java.lang.String tblname)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a table.public static int countTables(JDBCConnectionInfo cninf)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.public static int countTableColumns(JDBCConnectionInfo cninf, java.lang.String tblname)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a database table.public static int countViews(JDBCConnectionInfo cninf)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.public static boolean createIndex(JDBCConnectionInfo cninf, java.lang.String tblname)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a database table.true
if the database index create is
successful, otherwise false
.public static boolean createTable(JDBCConnectionInfo cninf, java.lang.String tblname)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a database table.true
if the database table create is
successful, otherwise false
.public static boolean createTable(JDBCConnectionInfo cninf, java.lang.String source, java.lang.String target)
NOTE: This method only creates the new table and its primary key. This method does not copy any data from the existing table.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.source
- (String) The name of an existing database table.target
- (String) The name of the database table to create.true
if the database table create is
successful, otherwise false
.public static boolean createView(JDBCConnectionInfo cninf, java.lang.String viewname)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.viewname
- (String) The name of a database view.true
if the database view create is
successful, otherwise false
.public static boolean deleteIndex(JDBCConnectionInfo cninf, java.lang.String tblname)
NOTE: This method assumes a database table will not have more than 10 indexes.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a database table.true
if the database index delete is
successful, otherwise false
.public static boolean deleteTable(JDBCConnectionInfo cninf, java.lang.String tblname)
NOTE: This method assumes all indexes for the table have already been deleted as well as any views that are based on the table.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a database table.true
if the database table delete is
successful, otherwise false
.public static boolean deleteView(JDBCConnectionInfo cninf, java.lang.String viewname)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.viewname
- (String) The name of a database view.true
if the database table delete is
successful, otherwise false
.public static void dumpFieldNames(java.sql.ResultSet rs) throws java.lang.Exception
rs
- (ResultSet) A result set.java.lang.Exception
- - if an error occurs while processing the result set.public static void dumpFieldValues(java.sql.ResultSet rs) throws java.lang.Exception
rs
- (ResultSet) A result set.java.lang.Exception
- - if an error occurs while processing the result set.public static java.lang.String getMetaSchema(JDBCConnectionInfo cninf)
null
if a schema name is not needed when obtaining a
connection's database metadata information.cninf
- (JDBCConnectionInfo) The JDBC connection to use.public static java.sql.ResultSet getRSColumn(JDBCConnectionInfo cninf, java.lang.String tblname, java.lang.String colname) throws java.lang.Exception
The invoker of this method is responsible for closing the result set.
In JDK 1.3.1 javadocs, Sun documents the result set column names as follows; however, not all databases use these column names. For this reason, values should be retrieved from the result set using the column indexes.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a table in the database.colname
- (String) The name of a column in the table.java.lang.Exception
- - if an error occurs when obtaining the result set.public static java.sql.ResultSet getRSColumns(JDBCConnectionInfo cninf, java.lang.String tblname) throws java.lang.Exception
The invoker of this method is responsible for closing the result set.
In JDK 1.3.1 javadocs, Sun documents the result set column names as follows; however, not all databases use these column names. For this reason, values should be retrieved from the result set using the column indexes.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a table in the database.java.lang.Exception
- - if an error occurs when obtaining the result set.public static java.sql.ResultSet getRSIndexes(JDBCConnectionInfo cninf, java.lang.String tblname) throws java.lang.Exception
The invoker of this method is responsible for closing the result set.
In JDK 1.3.1 javadocs, Sun documents the result set column names as follows; however, not all databases use these column names. For this reason, values should be retrieved from the result set using the column indexes.
NOTE: When this method is invoked against an IBM DB2 System i (AS/400) native database, the above column information is imitated, as much as is possible, by the SQL.@base.getIndexes SQL statement.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a table in the database.java.lang.Exception
- - if an error occurs while obtaining the result set.public static java.sql.ResultSet getRSPrimary(JDBCConnectionInfo cninf, java.lang.String tblname) throws java.lang.Exception
The invoker of this method is responsible for closing the result set.
In JDK 1.3.1 javadocs, Sun documents the result set column names as follows; however, not all databases use these column names. For this reason, values should be retrieved from the result set using the column indexes.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.java.lang.Exception
- - if an error occurs when obtaining the result set.public static java.sql.ResultSet getRSTables(JDBCConnectionInfo cninf) throws java.lang.Exception
The invoker of this method is responsible for closing the result set.
In JDK 1.3.1 javadocs, Sun documents the result set column names as follows; however, not all databases use these column names. For this reason, values should be retrieved from the result set using the column indexes.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.java.lang.Exception
- - if an error occurs when obtaining the result set.public static java.sql.ResultSet getRSTable(JDBCConnectionInfo cninf, java.lang.String tableName) throws java.lang.Exception
The invoker of this method is responsible for closing the result set.
In JDK 1.3.1 javadocs, Sun documents the result set column names as follows; however, not all databases use these column names. For this reason, values should be retrieved from the result set using the column indexes.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tableName
- (String) Table name to retrieve meta data.java.lang.Exception
- - if an error occurs when obtaining the result set.public static java.sql.ResultSet getRSTypes(JDBCConnectionInfo cninf) throws java.lang.Exception
The invoker of this method is responsible for closing the result set.
In JDK 1.3.1 javadocs, Sun documents the result set column names as follows; however, not all databases use these column names. For this reason, values should be retrieved from the result set using the column indexes.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.java.lang.Exception
- - if an error occurs when obtaining the result set.public static java.sql.ResultSet getRSViews(JDBCConnectionInfo cninf) throws java.lang.Exception
The invoker of this method is responsible for closing the result set.
In JDK 1.3.1 javadocs, Sun documents the result set column names as follows; however, not all databases use these column names. For this reason, values should be retrieved from the result set using the column indexes.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.java.lang.Exception
- - if an error occurs when obtaining the result set.public static java.util.ArrayList<java.lang.String> getTableNames(JDBCConnectionInfo cninf)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.public static java.lang.String getTablePrimary(JDBCConnectionInfo cninf, java.lang.String tblname)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a database table.public static java.sql.ResultSet getTableRecords(JDBCConnectionInfo cninf, java.lang.String tblname)
NOTE: The invoker of this method is responsible for closing the result set as well as the SQL statement used to create the result set.
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of a database table.public static java.util.ArrayList<java.lang.String> getViewNames(JDBCConnectionInfo cninf)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.public static java.lang.String quoteUnsafeWord(java.lang.String word)
RESERVED
constant.word
- (String) The word to process.public static java.lang.String quoteUnsafeWords(java.lang.String words)
RESERVED
constant.words
- (String) A set of comma delimited words.public static java.lang.String[] quoteUnsafeWords(java.lang.String[] words)
RESERVED
constant.words
- (String[]) An array of words.public static boolean reorgTable(JDBCConnectionInfo cninf, java.lang.String tblname)
cninf
- (JDBCConnectionInfo) The JDBC connection to use.tblname
- (String) The name of the table to reorganize.true
if the reorganization completes
successfully, otherwise false.public static java.lang.String dateToString(java.sql.Date date)
date
- (java.sql.Date) A date object.public static java.lang.String dateToString(java.util.Date date)
date
- (java.util.Date) A date object.public static java.lang.String timeToString(java.sql.Date date)
date
- (java.sql.Date) A date object.public static java.lang.String timeToString(java.util.Date date)
date
- (java.util.Date) A date object.public static java.lang.String timestampToString(java.sql.Timestamp ts)
ts
- (java.sql.Timestamp) A timestamp object.public static java.sql.Date stringToDate(java.lang.String date)
date
- (String) A date formatted string.public static java.sql.Timestamp stringToTimestamp(java.lang.String ts)
ts
- (String) A timestamp formatted string.public static java.lang.String statusToString(java.lang.String code)
code
- (String) The status code to format.public static java.lang.String unquoteWord(java.lang.String word)
word
- (String) The word to process.