Package chemaxon.util
Class ConnectionHandler
java.lang.Object
chemaxon.util.ErrorHandler
chemaxon.util.ConnectionHandler
A JavaBean for connecting to relational databases using JDBC. See
JDBC home site
for more details on JDBC connections. The JDBC driver you are using will
provide information on setting the url property.
This JavaBean stores one JChem specific property, the name of the property table. Its default value is "JChemProperties". See the multiuser section for details.
This JavaBean stores one JChem specific property, the name of the property table. Its default value is "JChemProperties". See the multiuser section for details.
If you don't intend to use the connection in transaction, it is preferred to allow ConnectionHandler to commit connection. In this case use constructors containing isConnectionCommitable parameter set to true.
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);
-
Field Summary
Fields inherited from class chemaxon.util.ErrorHandler
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 Summary
ConstructorDescriptionCreates new ConnectionHandler Connection will automatically be non-commitable (seeisCommitable()
) to preserve compatibility with previous versions.ConnectionHandler
(boolean isConnectionCommitable) Creates new ConnectionHandlerCreates 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
(Connection con, String propertyTableName) Creates a new ConnectionHandler and sets the connection to an externally created Connection (e.g.ConnectionHandler
(Connection con, String propertyTableName, boolean isConnectionCommitable) Creates a new ConnectionHandler and sets the connection to an externally created Connection (e.g. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the connection to the database.void
Connects to the database.void
Disconnect from the database.Getter for propertyconnection
.Getter for propertydriver
.Getter for propertydriverName
.static ConnectionHandler
getInstance
(int dbType, String host, int port, String database, String username, String password) Creates a ConnectionHandler using the appropriate driver and url determined by given database type, host, port and name.Getter for propertyloginName
.Getter for propertypassword
.Getter for propertypropertyTable
.boolean
Getter for propertyrememberPassword
.getUrl()
Getter for propertyurl
.boolean
boolean
Getter for propertyconnected
.boolean
boolean
loadValuesFromProperties
(Properties settings) Loads connection data from settings.void
setConnected
(boolean connected) Setter for propertyconnected
.final void
setConnection
(Connection con) Sets the connection to an externally created Connection (e.g.void
Setter for propertydriver
.void
setDriverName
(String driverName) Setter for propertydriverName
.void
setLoginName
(String loginName) Setter for propertyloginName
.void
setPassword
(String password) Setter for propertypassword
.void
setPropertyTable
(String propertyTableName) Setter for propertypropertyTable
.void
setRememberPassword
(boolean rememberPassword) Setter for propertyrememberPassword
.void
Setter for propertyurl
.void
storeValuesToProperties
(Properties settings) Stores connection data in properties.toString()
Methods inherited from class chemaxon.util.ErrorHandler
checkError, checkException, getError, getErrorMessage, getException, getStackTrace
-
Field Details
-
DEFAULT_PROPERTY_TABLE
- See Also:
-
-
Constructor Details
-
ConnectionHandler
public ConnectionHandler()Creates new ConnectionHandler Connection will automatically be non-commitable (seeisCommitable()
) to preserve compatibility with previous versions. -
ConnectionHandler
public ConnectionHandler(boolean isConnectionCommitable) Creates new ConnectionHandler- Parameters:
isConnectionCommitable
- iftrue
, the connection can be committed at the discretion of JChem. Please note, that no commit or multiple commits may occur in this case.- Since:
- JChem 5.9
-
ConnectionHandler
Creates a new ConnectionHandler and sets the connection to an externally created Connection (e.g. from a connection pool).
Since connection data (url, driver, loginName, password) is unknown, these properties are set to null. Warning: after instantiating a ConnectionHandler by using this constructor, do not callconnectToDatabase()
!- Parameters:
con
- An open (connected) Connection object that will be used for database operations.propertyTableName
- name of the jchem property table to use- Since:
- JChem 5.5
-
ConnectionHandler
Creates a new ConnectionHandler and sets the connection to an externally created Connection (e.g. from a connection pool).
Since connection data (url, driver, loginName, password) is unknown, these properties are set to null. Warning: after instantiating a ConnectionHandler by using this constructor, do not callconnectToDatabase()
!- Parameters:
con
- An open (connected) Connection object that will be used for database operations.propertyTableName
- name of the jchem property table to useisConnectionCommitable
- iftrue
, the connection can be committed at the discretion of JChem. Please note, that no commit or multiple commits may occur in this case.- Since:
- JChem 5.9
-
ConnectionHandler
Creates new ConnectionHandler based on the settings of an other ConnectionHandler. Does not copies Connection, and does not connect.- Parameters:
ch
- the other ConnectionHandler- Since:
- JChem 2.2
-
ConnectionHandler
Creates new ConnectionHandler based on the settings of an other ConnectionHandler.- Parameters:
ch
- the other ConnectionHandlerisConnectionCommitable
- iftrue
, the connection can be committed at the discretion of JChem. Please note, that no commit or multiple commits may occur in this case.- Since:
- JChem 5.9
-
-
Method Details
-
getInstance
public static ConnectionHandler getInstance(int dbType, String host, int port, String database, String username, String password) Creates a ConnectionHandler using the appropriate driver and url determined by given database type, host, port and name.Usage:
ConnectionHandler.getInstance(DatabaseConstants.POSTGRESQL, "localhost", 5432, "demodb", "adminuser", "adminpass");
- Parameters:
dbType
- Database type defined inDatabaseConstants
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.- Returns:
- ConnectionHandler or
null
if the database type is unknown - Since:
- JChem 5.2
-
getDriverName
Getter for propertydriverName
.- Returns:
- Value of property
driverName
.
-
setDriverName
Setter for propertydriverName
.- Parameters:
driverName
- New value of propertydriverName
.
-
getDriver
Getter for propertydriver
.- Returns:
- Value of property
driver
.
-
setDriver
Setter for propertydriver
.- Parameters:
driver
- New value of propertydriver
.
-
getUrl
Getter for propertyurl
.- Returns:
- Value of property
url
.
-
setUrl
Setter for propertyurl
.- Parameters:
url
- New value of propertyurl
.
-
getLoginName
Getter for propertyloginName
.- Returns:
- Value of property loginName.
-
setLoginName
Setter for propertyloginName
.- Parameters:
loginName
- New value of propertyloginName
.
-
getPassword
Getter for propertypassword
.- Returns:
- Value of property
password
.
-
setPassword
Setter for propertypassword
.- Parameters:
password
- New value of propertypassword
.
-
getPropertyTable
Getter for propertypropertyTable
.- Returns:
- Value of property
propertyTable
.
-
setPropertyTable
Setter for propertypropertyTable
.- Parameters:
propertyTableName
- New value of propertypropertyTable
.
-
getRememberPassword
public boolean getRememberPassword()Getter for propertyrememberPassword
.- Returns:
- Value of property
rememberPassword
.
-
setRememberPassword
public void setRememberPassword(boolean rememberPassword) Setter for propertyrememberPassword
.- Parameters:
rememberPassword
- New value of propertyrememberPassword
.
-
isConnected
public boolean isConnected()Getter for propertyconnected
.- Returns:
- Value of property
connected
.
-
setConnected
public void setConnected(boolean connected) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException Setter for propertyconnected
.- Parameters:
connected
- New value of propertyconnected
.- Throws:
SQLException
- if the database encounters a problemClassNotFoundException
- indicates jdbc driver problemInstantiationException
- indicates jdbc driver problemIllegalAccessException
- indicates jdbc driver problem
-
getConnection
Getter for propertyconnection
.- Returns:
- Value of property
connection
.
-
setConnection
Sets the connection to an externally created Connection (e.g. from a connection pool).
Since connection data (url, driver, loginName, password) is unknown, these properties are set to null. Warning: after callingsetConnection(Connection)
do not callconnectToDatabase()
!- Parameters:
con
- An open (connected) Connection object that will be used for database operations.
-
isCommitable
public boolean isCommitable()- Returns:
true
if the connection is commitable
-
connectToDatabase
Connects to the database. Creates a new connection using url, driver, loginName, password properties.
Warning: do not call if you've already set a connection by callingsetConnection(Connection)
!- Throws:
SQLException
- if a database error occurs.ClassNotFoundException
- if the JDBC driver class for the required database is not found on the classpath.
-
disconnect
Disconnect from the database.If a connection is open, it will be closed.
getConnection()
returns null after calling this method.- Throws:
SQLException
- if the database encounters a problem
-
close
Closes the connection to the database.- Throws:
SQLException
- if the database encounters a problem
-
loadValuesFromProperties
Loads connection data from settings. Parameter names (keys in properties):- "connection.jdbcDriverName" (optional)
- "connection.jdbcDriver" (mandatory)
- "connection.jdbcUrl" (mandatory)
- "connection.login" (optional, default: "")
- "connection.password" (optional)
- "connection.propertyTable" (optional, default: "JChemProperties")
- Parameters:
settings
- the connection parameter settings- Returns:
- true if the settings provide valid connection data
-
storeValuesToProperties
Stores connection data in properties. Parameter names (keys in properties):- "connection.jdbcDriverName" (optional)
- "connection.jdbcDriver" (mandatory)
- "connection.jdbcUrl" (mandatory)
- "connection.login" (optional, default: "")
- "connection.password" (optional)
- "connection.propertyTable" (optional, default: "JChemProperties")
- Parameters:
settings
- the properties to be filled with connection data
-
toString
-
isCopyable
public boolean isCopyable()- Returns:
true
if the connection is copyable
-