public enum DatabaseAPI extends Enum<DatabaseAPI>
DriverManager and
DataSource) for connecting to relational databases. It
provides a straightforward means for programs to make use of either
without having to hard-code the API choice. This is useful in developing a
library that may be used in a standalone program or in a container.
Usage:
public class DatabaseAPIExample {
private DatabaseAPI databaseAPI;
private String databaseId;
private String username;
private String password;
public DatabaseAPIExample(DatabaseAPI databaseAPI, String databaseId,
String username, String password) {
this.databaseAPI = databasePID;
this.databaseId = databaseId;
this.username = username;
this.password = password;
}
public void doProcessData() {
ConnectionSpec connectionSpec =
this.databaseAPI.newConnectionSpecInstance(
this.databaseId, this.username, this.password);
...
}
}
| Enum Constant and Description |
|---|
DATASOURCE |
DRIVERMANAGER |
| Modifier and Type | Method and Description |
|---|---|
ConnectionSpec |
newConnectionSpecInstance(String databaseId,
String username,
String password)
Gets a
ConnectionSpec implementation that is specific to the
selected database API for getting/creating a database Connection. |
ConnectionSpec |
newConnectionSpecInstance(String databaseId,
String username,
String password,
boolean autoCommitEnabled)
Gets a
ConnectionSpec implementation that is specific to the
selected database API for getting/creating a database Connection. |
static DatabaseAPI |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static DatabaseAPI[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final DatabaseAPI DRIVERMANAGER
public static final DatabaseAPI DATASOURCE
public static DatabaseAPI[] values()
for (DatabaseAPI c : DatabaseAPI.values()) System.out.println(c);
public static DatabaseAPI valueOf(String name)
name - the name of the enum constant to be returned.IllegalArgumentException - if this enum type has no constant with the specified nameNullPointerException - if the argument is nullpublic ConnectionSpec newConnectionSpecInstance(String databaseId, String username, String password) throws InvalidConnectionSpecArguments
ConnectionSpec implementation that is specific to the
selected database API for getting/creating a database Connection.
Connections created from the returned instance will have auto commit set
to truedatabaseId - the identifier for the database (JDBC URL for
the DriverManager API, JNDI name for the
DataSource API.username - the username (null if not applicable)password - the password (null if not applicable)ConnectionSpec.InvalidConnectionSpecArguments - if the databaseId is invalid.public ConnectionSpec newConnectionSpecInstance(String databaseId, String username, String password, boolean autoCommitEnabled) throws InvalidConnectionSpecArguments
ConnectionSpec implementation that is specific to the
selected database API for getting/creating a database Connection.databaseId - the identifier for the database (JDBC URL for
the DriverManager API, JNDI name for the
DataSource API.username - the username (null if not applicable)password - the password (null if not applicable)autoCommitEnabled - true to make connections created
from the returned instance have auto commit turned on by default,
false to make connections created from the returned
instance to have auto commit turned off by default.ConnectionSpec.InvalidConnectionSpecArguments - if the databaseId is invalid.Copyright © 2012–2020 Emory University. All rights reserved.