public class DatabaseUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_DRIVER
Holds the default JDBC driver.
|
static java.lang.String |
ORACLE_DRIVER |
Constructor and Description |
---|
DatabaseUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
closeAll(java.sql.ResultSet rs,
java.sql.Statement st,
java.sql.Connection conn)
Convenience method for closing sql objects.
|
static int |
execDeleteStatement(java.sql.Connection con,
java.lang.String table,
java.lang.String whereCl)
Purpose: execute delete statement, can be used to do update or insert Assumptions: Note: this version does not take into account special characters. |
static int |
execStatement(java.sql.Connection con,
java.lang.String strStatement)
Purpose: execute statement, can be used to do update or insert Assumptions: Note: this version does not take into account special characters. |
static java.sql.Connection |
getConnection(java.lang.String jdbcUrl,
java.lang.String username,
java.lang.String password)
Opens a connection to the JDBC database indicated by the 'dbName'
parameter.
|
static java.lang.String |
getDBVal(java.lang.Object m)
Purpose: getDBVal as a string if the type is "java.lang.Double", "java.sql.Timestamp" or "java.sql.Date" the we remove trailing .0 if exists. |
static int |
getNumColumns(java.sql.Connection con,
java.lang.String strStatement,
java.lang.String table,
java.lang.String field,
java.lang.String attr,
java.lang.Object value,
java.util.ArrayList columns)
Purpose: simple get(all), as in: "SELECT field FROM table WHERE attr = 'value'" or if attr is null then simply "SELECT field FROM table"; in order to get number of columns, and/or to get the names of the columns in parameter 'columns' Assumptions: Note: this version does not take into account special characters. |
static int |
getNumRows(java.sql.Connection con,
java.lang.String table,
java.lang.String wherePart)
Purpose: get number of rows |
static java.sql.ResultSet |
getResultSet(java.sql.Connection con,
java.lang.String strStatement,
java.lang.String table,
java.lang.String field,
java.lang.String attr,
java.lang.Object value,
java.sql.Statement[] st)
Purpose: simple query, as in: "SELECT field FROM table WHERE attr = 'value'" or if attr is null then simply "SELECT field FROM table" Assumptions: Note: this version does not take into account special characters. |
static java.util.List |
getRow(java.sql.ResultSet rs)
Purpose: get the next row from the ResultSet as a Collection Assumptions: that rs.next() was already called and is true. |
static java.util.List |
getRow(java.sql.ResultSet rs,
java.sql.ResultSetMetaData meta,
int numberOfColumns,
java.lang.Integer maxColCount)
Purpose: get the next row from the ResultSet as a Collection Assumptions: that rs.next() was already called and is true |
static java.util.List |
getValue(java.sql.Connection con,
java.lang.String strStatement,
java.lang.String table,
java.lang.String field,
java.lang.String attr,
java.lang.Object value,
java.util.ArrayList columns)
Purpose: simple get, as in: "SELECT field FROM table WHERE attr = 'value'" or if attr is null then simply "SELECT field FROM table" Assumptions: Note: this version does not take into account special characters. |
static java.util.List |
getValues(java.sql.Connection con,
java.lang.String strStatement,
java.lang.String table,
java.lang.String field,
java.lang.String attr,
java.lang.Object value,
java.util.ArrayList columns,
java.lang.Integer maxRowCount,
java.lang.Integer maxColCount)
Purpose: simple get(all), as in: "SELECT field FROM table WHERE attr = 'value'" or if attr is null then simply "SELECT field FROM table" Assumptions: Note: this version does not take into account special characters. |
static java.lang.Class |
loadDriver(java.lang.String driver)
load the driver, if 'driver' is null then uses DEFAULT_DRIVER
|
static void |
main(java.lang.String[] arg)
test main method.
|
static void |
printHeader(java.lang.StringBuffer buf,
java.lang.String table,
java.lang.String col)
Purpose: print the header |
static void |
printValues(java.lang.StringBuffer buf,
java.util.List list,
java.util.ArrayList columns)
Purpose: print the values of 'list' to 'buf' |
protected static java.lang.String |
setupSelect(java.lang.StringBuffer buf,
java.lang.String table,
java.lang.String field,
java.lang.String attr,
java.lang.Object value)
Purpose: setup select statement |
static void |
writefile(java.lang.String filename,
java.util.Collection list,
java.lang.String delim)
write to file 'filename' the toString() values contained in list.
|
public static final java.lang.String DEFAULT_DRIVER
public static final java.lang.String ORACLE_DRIVER
public static java.lang.Class loadDriver(java.lang.String driver) throws java.sql.SQLException
driver,
- Stringjava.sql.SQLException
public static java.sql.Connection getConnection(java.lang.String jdbcUrl, java.lang.String username, java.lang.String password) throws java.sql.SQLException
jdbcUrl
- The database urlusername
- String, if null then uses the 1-parameter version of DriverManager.getConnectionpassword
- Stringjava.sql.SQLException
- if the connection fails.protected static java.lang.String setupSelect(java.lang.StringBuffer buf, java.lang.String table, java.lang.String field, java.lang.String attr, java.lang.Object value)
buf,
- StringBuffertable,
- String name of the tablefield,
- String the select field nameattr,
- String the attr part for the where, if null then no wherevalue,
- if attr!=null, then the value part of where, if null,
then the where part will be "attr NOT NULL"public static java.sql.ResultSet getResultSet(java.sql.Connection con, java.lang.String strStatement, java.lang.String table, java.lang.String field, java.lang.String attr, java.lang.Object value, java.sql.Statement[] st) throws java.sql.SQLException
con,
- ConnectionstrStatement,
- String the statement, if null, then construct from the other parameterstable,
- String name of the tablefield,
- String the select field nameattr,
- String the attr part for the where, if null then no wherevalue,
- if attr!=null, then the value part of where, if null,
then the where part will be "attr NOT NULL"st,
- Statement[], pointer to a return'ed Statementjava.sql.SQLException
public static int execStatement(java.sql.Connection con, java.lang.String strStatement) throws java.sql.SQLException
con
- ConnectionstrStatement
- String the statementjava.sql.SQLException
public static int execDeleteStatement(java.sql.Connection con, java.lang.String table, java.lang.String whereCl) throws java.sql.SQLException
con,
- Connectiontable,
- String the table namewhereCl,
- String the where clausejava.sql.SQLException
public static java.util.List getValue(java.sql.Connection con, java.lang.String strStatement, java.lang.String table, java.lang.String field, java.lang.String attr, java.lang.Object value, java.util.ArrayList columns) throws java.sql.SQLException
con,
- ConnectionstrStatement,
- String the statement, if null, then construct from the other parameterstable,
- String name of the tablefield,
- String the select field nameattr,
- String the attr part for the where, if null then no wherevalue,
- if attr!=null, then the value part of where, if null,
then the where part will be "attr NOT NULL"columns,
- if not null, then we add the columns to this listjava.sql.SQLException
public static java.util.List getValues(java.sql.Connection con, java.lang.String strStatement, java.lang.String table, java.lang.String field, java.lang.String attr, java.lang.Object value, java.util.ArrayList columns, java.lang.Integer maxRowCount, java.lang.Integer maxColCount) throws java.sql.SQLException
con,
- ConnectionstrStatement,
- String the statement, if null, then construct from the other parameterstable,
- String name of the tablefield,
- String the select field nameattr,
- String the attr part for the where, if null then no wherevalue,
- if attr!=null, then the value part of where, if null,
then the where part will be "attr NOT NULL"columns,
- if not null, then we add the columns to this listmaxRowCount,
- if not null, then don't return more rows then specifiedmaxColCount,
- if not null, then don't return more cols then specifiedjava.sql.SQLException
public static int getNumColumns(java.sql.Connection con, java.lang.String strStatement, java.lang.String table, java.lang.String field, java.lang.String attr, java.lang.Object value, java.util.ArrayList columns) throws java.sql.SQLException
con,
- ConnectionstrStatement,
- String the statement, if null, then construct from the other parameterstable,
- String name of the tablefield,
- String the select field nameattr,
- String the attr part for the where, if null then no wherevalue,
- if attr!=null, then the value part of where, if null,
then the where part will be "attr NOT NULL"columns,
- if not null, then we add the columns to this listjava.sql.SQLException
public static int getNumRows(java.sql.Connection con, java.lang.String table, java.lang.String wherePart) throws java.sql.SQLException
con
- Connectiontable
- String name of the tablewherePart
- String the where partjava.sql.SQLException
public static java.util.List getRow(java.sql.ResultSet rs) throws java.sql.SQLException
rs,
- ResultSetjava.sql.SQLException
public static java.util.List getRow(java.sql.ResultSet rs, java.sql.ResultSetMetaData meta, int numberOfColumns, java.lang.Integer maxColCount) throws java.sql.SQLException
rs,
- ResultSetmeta,
- ResultSetMetaData (obtained externally via rs.getMetaData())numberOfColumns,
- int (obtained externally via meta.getColumnCount())maxColCount,
- Integerjava.sql.SQLException
public static void closeAll(java.sql.ResultSet rs, java.sql.Statement st, java.sql.Connection conn) throws java.sql.SQLException
rs
- ResultSet to be closedst
- Statement (or subclass) to be closedconn
- Connection to be closedjava.sql.SQLException
- when any problem occurs while closing objectspublic static void main(java.lang.String[] arg)
public static void printValues(java.lang.StringBuffer buf, java.util.List list, java.util.ArrayList columns)
buf,
- StringBufferlist,
- Listcolumns,
- ArrayListpublic static void printHeader(java.lang.StringBuffer buf, java.lang.String table, java.lang.String col)
buf,
- StringBuffertable,
- Stringcol,
- Stringpublic static void writefile(java.lang.String filename, java.util.Collection list, java.lang.String delim) throws java.io.IOException
If an item in the list is another Collection, then special handling is performed via the getDBVal function to potentially filter the value to be written. The items in this secondary Collection are treated as delimited fields and will be separated in the output by the field delimiter provided.
filename
- String full absolute path filename of file to write.list
- Collection of lines to write.delim
- String field separator to use on values stored in Collections in the list.java.io.FileNotFoundException
- if file cannot be created for any reason.java.io.IOException
- if an error occurs during write operations.FileUtilities.getSystemBufferedFileWriter(String)
,
getDBVal(Object)
public static java.lang.String getDBVal(java.lang.Object m)
m,
- ObjectCopyright © SAS Institute. All Rights Reserved.