@PublicAPI public class ConnectionHandler extends ErrorHandler
Example #1 (connection is not used in transaction):
ConnectionHandler ch = new ConnectionHandler(true); ch.setDriver("oracle.jdbc.OracleDriver"); ch.setUrl("jdbc:oracle:thin:@inhale:1521:demodb"); ch.setLoginName("INTERNAL"); ch.setPassword("oracle"); ch.connectToDatabase();Example #2 (connection might be used in transaction):
Connection con = ... // E.g. from connection pool, ... ConnectionHandler ch = new ConnectionHandler(con, ConnectionHandler.DEFAULT_PROPERTY_TABLE);
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DEFAULT_PROPERTY_TABLE |
CLASS_NOT_FOUND_ERROR, DATABASE_SEARCH_ERROR, exception, ILLEGAL_ACCESS_ERROR, INSTANTIATION_ERROR, IO_ERROR, MAX_SEARCH_FREQUENCY_EXCEEDED_ERROR, MOL_FORMAT_ERROR, NO_ERROR, SQL_ERROR, UNIDENTIFIED_ERROR
Constructor and Description |
---|
ConnectionHandler()
Creates new ConnectionHandler
Connection will automatically be non-commitable (see
isCommitable() )
to preserve compatibility with previous versions. |
ConnectionHandler(boolean isConnectionCommitable)
Creates new ConnectionHandler
|
ConnectionHandler(ConnectionHandler ch)
Creates new ConnectionHandler based on the settings of an other
ConnectionHandler.
|
ConnectionHandler(ConnectionHandler ch,
boolean isConnectionCommitable)
Creates new ConnectionHandler based on the settings of an other ConnectionHandler.
|
ConnectionHandler(java.sql.Connection con,
java.lang.String propertyTableName)
Creates a new ConnectionHandler and sets the connection to an externally created Connection
(e.g.
|
ConnectionHandler(java.sql.Connection con,
java.lang.String propertyTableName,
boolean isConnectionCommitable)
Creates a new ConnectionHandler and sets the connection to an externally created Connection
(e.g.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the connection to the database.
|
void |
connectToDatabase()
Connects to the database.
|
void |
disconnect()
Disconnect from the database.
|
protected void |
finalize() |
java.sql.Connection |
getConnection()
Getter for property
connection . |
java.lang.String |
getDriver()
Getter for property
driver . |
java.lang.String |
getDriverName()
Getter for property
driverName . |
static ConnectionHandler |
getInstance(int dbType,
java.lang.String host,
int port,
java.lang.String database,
java.lang.String username,
java.lang.String password)
Creates a ConnectionHandler using the appropriate driver and url determined
by given database type, host, port and name.
|
java.lang.String |
getLoginName()
Getter for property
loginName . |
java.lang.String |
getPassword()
Getter for property
password . |
java.lang.String |
getPropertyTable()
Getter for property
propertyTable . |
boolean |
getRememberPassword()
Getter for property
rememberPassword . |
java.lang.String |
getUrl()
Getter for property
url . |
boolean |
isCommitable() |
boolean |
isConnected()
Getter for property
connected . |
boolean |
isCopyable() |
boolean |
loadValuesFromProperties(java.util.Properties settings)
Loads connection data from settings.
|
void |
setConnected(boolean connected)
Setter for property
connected . |
void |
setConnection(java.sql.Connection con)
Sets the connection to an externally created Connection (e.g.
|
void |
setDriver(java.lang.String driver)
Setter for property
driver . |
void |
setDriverName(java.lang.String driverName)
Setter for property
driverName . |
void |
setLoginName(java.lang.String loginName)
Setter for property
loginName . |
void |
setPassword(java.lang.String password)
Setter for property
password . |
void |
setPropertyTable(java.lang.String propertyTableName)
Setter for property
propertyTable . |
void |
setRememberPassword(boolean rememberPassword)
Setter for property
rememberPassword . |
void |
setUrl(java.lang.String url)
Setter for property
url . |
void |
storeValuesToProperties(java.util.Properties settings)
Stores connection data in properties.
|
java.lang.String |
toString() |
checkError, checkException, getError, getErrorMessage, getException, getStackTrace
public static final java.lang.String DEFAULT_PROPERTY_TABLE
public ConnectionHandler()
isCommitable()
)
to preserve compatibility with previous versions.public ConnectionHandler(boolean isConnectionCommitable)
isConnectionCommitable
- if true
,
the connection can be committed at the discretion of JChem.
Please note, that no commit or multiple commits may occur in this case.public ConnectionHandler(java.sql.Connection con, java.lang.String propertyTableName)
connectToDatabase()
!con
- An open (connected) Connection object that will be used for database operations.propertyTableName
- name of the jchem property table to usepublic ConnectionHandler(java.sql.Connection con, java.lang.String propertyTableName, boolean isConnectionCommitable)
connectToDatabase()
!con
- An open (connected) Connection object that will be used for database operations.propertyTableName
- name of the jchem property table to useisConnectionCommitable
- if true
,
the connection can be committed at the discretion of JChem.
Please note, that no commit or multiple commits may occur in this case.public ConnectionHandler(ConnectionHandler ch)
ch
- the other ConnectionHandlerpublic ConnectionHandler(ConnectionHandler ch, boolean isConnectionCommitable)
ch
- the other ConnectionHandlerisConnectionCommitable
- if true
,
the connection can be committed at the discretion of JChem.
Please note, that no commit or multiple commits may occur in this case.public static ConnectionHandler getInstance(int dbType, java.lang.String host, int port, java.lang.String database, java.lang.String username, java.lang.String password)
Usage:
ConnectionHandler.getInstance(DatabaseConstants.POSTGRESQL, "localhost", 5432, "demodb", "adminuser", "adminpass");
dbType
- Database type defined in DatabaseConstants
class.host
- Server name or IP-address.port
- Server port number or default if negative number is given.database
- Name of the concrete database (or schema) we want to connect.username
- Username.password
- Password.null
if the database type is unknownpublic java.lang.String getDriverName()
driverName
.driverName
.public void setDriverName(java.lang.String driverName)
driverName
.driverName
- New value of property driverName
.public java.lang.String getDriver()
driver
.driver
.public void setDriver(java.lang.String driver)
driver
.driver
- New value of property driver
.public java.lang.String getUrl()
url
.url
.public void setUrl(java.lang.String url)
url
.url
- New value of property url
.public java.lang.String getLoginName()
loginName
.public void setLoginName(java.lang.String loginName)
loginName
.loginName
- New value of property loginName
.public java.lang.String getPassword()
password
.password
.public void setPassword(java.lang.String password)
password
.password
- New value of property password
.public java.lang.String getPropertyTable()
propertyTable
.propertyTable
.public void setPropertyTable(java.lang.String propertyTableName)
propertyTable
.propertyTableName
- New value of property propertyTable
.public boolean getRememberPassword()
rememberPassword
.rememberPassword
.public void setRememberPassword(boolean rememberPassword)
rememberPassword
.rememberPassword
- New value of property rememberPassword
.public boolean isConnected()
connected
.connected
.public void setConnected(boolean connected) throws java.sql.SQLException, java.lang.ClassNotFoundException, java.lang.InstantiationException, java.lang.IllegalAccessException
connected
.connected
- New value of property connected
.java.sql.SQLException
- if the database encounters a problemjava.lang.ClassNotFoundException
- indicates jdbc driver problemjava.lang.InstantiationException
- indicates jdbc driver problemjava.lang.IllegalAccessException
- indicates jdbc driver problempublic java.sql.Connection getConnection()
connection
.connection
.public final void setConnection(java.sql.Connection con)
setConnection(Connection)
do not call connectToDatabase()
!con
- An open (connected) Connection object that will be used for database operations.public boolean isCommitable()
true
if the connection is commitablepublic void connectToDatabase() throws java.sql.SQLException, java.lang.ClassNotFoundException
setConnection(Connection)
!java.sql.SQLException
- if a database error occurs.java.lang.ClassNotFoundException
- if the JDBC driver class for the required database
is not found on the classpath.public void disconnect() throws java.sql.SQLException
getConnection()
returns null after calling this method.java.sql.SQLException
- if the database encounters a problempublic void close() throws java.sql.SQLException
java.sql.SQLException
- if the database encounters a problempublic boolean loadValuesFromProperties(java.util.Properties settings)
settings
- the connection parameter settingspublic void storeValuesToProperties(java.util.Properties settings)
settings
- the properties to be filled with connection datapublic java.lang.String toString()
toString
in class java.lang.Object
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public boolean isCopyable()
true
if the connection is copyable