chemaxon.util
Class ConnectionHandler

java.lang.Object
  extended by chemaxon.util.ErrorHandler
      extended by chemaxon.util.ConnectionHandler
All Implemented Interfaces:
java.io.Serializable

public class ConnectionHandler
extends ErrorHandler
implements java.io.Serializable

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. 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);
 

Author:
Ferenc Csizmadia, Tamas Csizmazia
See Also:
Serialized Form

Field Summary
static java.lang.String DEFAULT_PROPERTY_TABLE
           
 
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, PROPERTY_NOT_SET_ERROR, SQL_ERROR, UNIDENTIFIED_ERROR
 
Constructor Summary
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(java.sql.Connection con)
          Deprecated. this constructor lacks the jchemproperty table name that has to be set separately on the constructed object. Please use ConnectionHandler(Connection, String) or ConnectionHandler(Connection, String, boolean) instead!
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. from a connection pool).
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. from a connection pool).
 
Method Summary
 void close_NE()
          Deprecated. since JChem 5.2 use close()
 void close()
          Closes the connection to the database.
 void connect_NE()
          Deprecated. since JChem 5.2 use connect()
 void connect()
          Deprecated. since JChem 5.4, use connectToDatabase() to avoid having to catch useless exceptions.
 void connectToDatabase()
          Connects to the database.
 void disconnect()
          Disconnect from the database.
 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 loadValuesFromProperties(java.util.Properties 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. from a connection pool).
 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)
           
 java.lang.String toString()
           
 
Methods inherited from class chemaxon.util.ErrorHandler
checkError, checkException, getError, getErrorMessage, getException, getStackTrace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_PROPERTY_TABLE

public static final java.lang.String DEFAULT_PROPERTY_TABLE
See Also:
Constant Field Values
Constructor Detail

ConnectionHandler

public ConnectionHandler()
Creates new ConnectionHandler Connection will automatically be non-commitable (see isCommitable()) to preserve compatibility with previous versions.


ConnectionHandler

public ConnectionHandler(boolean isConnectionCommitable)
Creates new ConnectionHandler

Parameters:
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.
Since:
JChem 5.9

ConnectionHandler

@Deprecated
public ConnectionHandler(java.sql.Connection con)
Deprecated. this constructor lacks the jchemproperty table name that has to be set separately on the constructed object. Please use ConnectionHandler(Connection, String) or ConnectionHandler(Connection, String, boolean) instead!

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 call connectToDatabase()!

Parameters:
con - An open (connected) Connection object that will be used for database operations.
Since:
JChem 5.5

ConnectionHandler

public ConnectionHandler(java.sql.Connection con,
                         java.lang.String propertyTableName)
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 call connectToDatabase()!

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

public 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. 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 call connectToDatabase()!

Parameters:
con - An open (connected) Connection object that will be used for database operations.
propertyTableName - name of the jchem property table to use
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.
Since:
JChem 5.9

ConnectionHandler

public ConnectionHandler(ConnectionHandler ch)
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

public ConnectionHandler(ConnectionHandler ch,
                         boolean isConnectionCommitable)
Creates new ConnectionHandler based on the settings of an other ConnectionHandler.

Parameters:
ch - the other ConnectionHandler
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.
Since:
JChem 5.9
Method Detail

getInstance

public 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.

Usage:

 ConnectionHandler.getInstance(DatabaseConstants.POSTGRESQL, "localhost", 5432, "demodb", "adminuser", "adminpass");
 

Parameters:
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.
Returns:
ConnectionHandler or null if the database type is unknown
Since:
JChem 5.2

getDriverName

public java.lang.String getDriverName()
Getter for property driverName.

Returns:
Value of property driverName.

setDriverName

public void setDriverName(java.lang.String driverName)
Setter for property driverName.

Parameters:
driverName - New value of property driverName.

getDriver

public java.lang.String getDriver()
Getter for property driver.

Returns:
Value of property driver.

setDriver

public void setDriver(java.lang.String driver)
Setter for property driver.

Parameters:
driver - New value of property driver.

getUrl

public java.lang.String getUrl()
Getter for property url.

Returns:
Value of property url.

setUrl

public void setUrl(java.lang.String url)
Setter for property url.

Parameters:
url - New value of property url.

getLoginName

public java.lang.String getLoginName()
Getter for property loginName.

Returns:
Value of property loginName.

setLoginName

public void setLoginName(java.lang.String loginName)
Setter for property loginName.

Parameters:
loginName - New value of property loginName.

getPassword

public java.lang.String getPassword()
Getter for property password.

Returns:
Value of property password.

setPassword

public void setPassword(java.lang.String password)
Setter for property password.

Parameters:
password - New value of property password.

getPropertyTable

public java.lang.String getPropertyTable()
Getter for property propertyTable.

Returns:
Value of property propertyTable.

setPropertyTable

public void setPropertyTable(java.lang.String propertyTableName)
Setter for property propertyTable.

Parameters:
propertyTableName - New value of property propertyTable.

getRememberPassword

public boolean getRememberPassword()
Getter for property rememberPassword.

Returns:
Value of property rememberPassword.

setRememberPassword

public void setRememberPassword(boolean rememberPassword)
Setter for property rememberPassword.

Parameters:
rememberPassword - New value of property rememberPassword.

isConnected

public boolean isConnected()
Getter for property connected.

Returns:
Value of property connected.

setConnected

public void setConnected(boolean connected)
                  throws java.sql.SQLException,
                         java.lang.ClassNotFoundException,
                         java.lang.InstantiationException,
                         java.lang.IllegalAccessException
Setter for property connected.

Parameters:
connected - New value of property connected.
Throws:
java.sql.SQLException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException

getConnection

public java.sql.Connection getConnection()
Getter for property connection.

Returns:
Value of property connection.

setConnection

public final void setConnection(java.sql.Connection con)
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 calling setConnection(Connection) do not call connectToDatabase()!

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

connect

public void connect()
             throws java.sql.SQLException,
                    java.lang.ClassNotFoundException,
                    java.lang.InstantiationException,
                    java.lang.IllegalAccessException
Deprecated. since JChem 5.4, use connectToDatabase() to avoid having to catch useless exceptions.

Connects to the database.

Throws:
java.sql.SQLException
java.lang.ClassNotFoundException
java.lang.InstantiationException
java.lang.IllegalAccessException

connectToDatabase

public void connectToDatabase()
                       throws java.sql.SQLException,
                              java.lang.ClassNotFoundException
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 calling setConnection(Connection)!

Throws:
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.

disconnect

public void disconnect()
                throws java.sql.SQLException
Disconnect from the database. If a connection is open, it will be closed. getConnection() returns null after calling this method.

Throws:
java.sql.SQLException

connect_NE

public void connect_NE()
Deprecated. since JChem 5.2 use connect()

Same as connect(), but exceptions are caught and stored.

See Also:
ErrorHandler, connect()

close

public void close()
           throws java.sql.SQLException
Closes the connection to the database.

Throws:
java.sql.SQLException

close_NE

public void close_NE()
Deprecated. since JChem 5.2 use close()

Same as close(), but exceptions are caught and stored.

See Also:
ErrorHandler, close()

loadValuesFromProperties

public boolean loadValuesFromProperties(java.util.Properties settings)

storeValuesToProperties

public void storeValuesToProperties(java.util.Properties settings)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object