public class DatabaseUtils extends Object
DatabaseUtils.QueryAPI
interface: getQueryStr - Get the query string for this constant. This is the actual query
that's sent to the database.getArgNames - Get the names of the arguments for this query. This provides
diagnostic information if the incorrect number of arguments is specified by the client.getArgCount - Get the number of arguments required by this query. This enables
executeOracleQuery(Class, QueryAPI, Object[]) to verify that the correct number of arguments has been
specified by the client.getConnection - Get the connection string associated with this query. This
eliminates the need for the client to provide this information.getEnum - Get the enumeration to which this query belongs. This enables executeOracleQuery(Class, QueryAPI, Object[]) to retrieve the name of the query's enumerated constant for
diagnostic messages.DatabaseUtils.QueryAPI.Settings API: getConnection with
sub-configurations or other dynamic data sources (e.g. - web service).public class OpctConfig extendsSettingsCore<OpctConfig.OpctValues>{ private static final String SETTINGS_FILE = "OpctConfig.properties"; private OpctConfig() throws ConfigurationException, IOException { super(OpctValues.class); } public enum OpctValues implements SettingsCore.SettingsAPI, QueryAPI { /** args: [ ] */ GET_RULE_HEAD_DETAILS("opct.query.getRuleHeadDetails"), /** args: [ name, zone_id, priority, rule_type ] */ GET_RULE_COUNT("opct.query.getRuleCount", "name", "zone_id", "priority", "rule_type"), /** args: [ role_id, user_id ] */ UPDATE_USER_ROLE("opct.query.updateRsmUserRole", "role_id", "user_id"), /** MST connection string */ MST_CONNECT("opct.connect.mst"), /** RMS connection string */ RMS_CONNECT("opct.connect.rms"); private String key; private String[] args; private String query; private static OpctConfig config; private static String mstConnect; private static String rmsConnect; private staticEnumSet<OpctValues>rmsQueries = EnumSet.of(UPDATE_USER_ROLE); static { try { config = new OpctConfig(); } catch (ConfigurationException | IOException e) { throw new RuntimeException("Unable to instantiate OPCT configuration object", e); } } OpctValues(String key, String... args) { this.key = key; this.args = args; }@Overridepublic String key() { return key; }@Overridepublic String getQueryStr() { if (query == null) { query = config.getString(key); } return query; }@Overridepublic String[] getArgNames() { return args; }@Overridepublic int getArgCount() { return args.length; }@Overridepublic String getConnection() { if (rmsQueries.contains(this)) { return getRmsConnect(); } else { return getMstConnect(); } }@OverridepublicEnum<OpctValues>getEnum() { return this; } /** * Get MST connection string. * * @return MST connection string */ public static String getMstConnect() { if (mstConnect == null) { mstConnect = config.getString(OpctValues.MST_CONNECT.key()); } return mstConnect; } /** * Get RMS connection string. * * @return RMS connection string */ public static String getRmsConnect() { if (rmsConnect == null) { rmsConnect = config.getString(OpctValues.RMS_CONNECT.key()); } return rmsConnect; } }@Overridepublic String getSettingsPath() { return SETTINGS_FILE; } /** * Get OPCT configuration object. * * @return OPCT configuration object */ public static OpctConfig getConfig() { return OpctValues.config; } }
| Modifier and Type | Class and Description |
|---|---|
static interface |
DatabaseUtils.QueryAPI
This interface defines the API supported by database query collections
|
static class |
DatabaseUtils.ResultPackage
This class defines a package of database objects associated with a query.
|
| Modifier and Type | Method and Description |
|---|---|
static Object |
executeOracleQuery(Class<?> resultType,
String connectionStr,
String queryStr,
Object... param)
Execute the specified query with the supplied arguments, returning a result of the indicated type.
|
static int |
getInt(DatabaseUtils.QueryAPI query,
Object... queryArgs)
Execute the specified query object with supplied arguments as a 'query' operation
|
static DatabaseUtils.ResultPackage |
getResultPackage(DatabaseUtils.QueryAPI query,
Object... queryArgs)
Execute the specified query object with supplied arguments as a 'query' operation
|
static String |
getString(DatabaseUtils.QueryAPI query,
Object... queryArgs)
Execute the specified query object with supplied arguments as a 'query' operation
|
static int |
update(DatabaseUtils.QueryAPI query,
Object... queryArgs)
Execute the specified query object with supplied arguments as an 'update' operation
|
public static int update(DatabaseUtils.QueryAPI query, Object... queryArgs)
query - query object to executequeryArgs - replacement values for query place-holderspublic static int getInt(DatabaseUtils.QueryAPI query, Object... queryArgs)
query - query object to executequeryArgs - replacement values for query place-holderspublic static String getString(DatabaseUtils.QueryAPI query, Object... queryArgs)
query - query object to executequeryArgs - replacement values for query place-holderspublic static DatabaseUtils.ResultPackage getResultPackage(DatabaseUtils.QueryAPI query, Object... queryArgs)
query - query object to executequeryArgs - replacement values for query place-holdersDatabaseUtils.ResultPackage objectpublic static Object executeOracleQuery(Class<?> resultType, String connectionStr, String queryStr, Object... param)
DatabaseUtils.ResultPackage - An object containing the connection, statement, and result set is returnedInteger - If rows were returned, row 1 / column 1 is returned as an Integer; otherwise -1String - If rows were returned, row 1 / column 1 is returned as an String; otherwise 'null'ResultSet.getObject(int, Class) to return row 1 / column 1 as that typeresultType - desired result type (see TYPES above)connectionStr - Oracle database connection stringqueryStr - a SQL statement that may contain one or more '?' IN parameter placeholdersparam - an array of objects containing the input parameter valuesDatabaseUtils.ResultPackage as the result type, it's recommended that you close this object
when you're done with it to free up database and JDBC resources that were allocated for it.Copyright © 2017. All rights reserved.