Package chemaxon.util

Class ConnectionHandler


  • @PublicAPI
    public class ConnectionHandler
    extends ErrorHandler
    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);
     
    • 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

        public ConnectionHandler​(Connection con,
                                 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​(Connection con,
                                 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,
                                                    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 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 String getDriverName()
        Getter for property driverName.
        Returns:
        Value of property driverName.
      • setDriverName

        public void setDriverName​(String driverName)
        Setter for property driverName.
        Parameters:
        driverName - New value of property driverName.
      • getDriver

        public String getDriver()
        Getter for property driver.
        Returns:
        Value of property driver.
      • setDriver

        public void setDriver​(String driver)
        Setter for property driver.
        Parameters:
        driver - New value of property driver.
      • getUrl

        public String getUrl()
        Getter for property url.
        Returns:
        Value of property url.
      • setUrl

        public void setUrl​(String url)
        Setter for property url.
        Parameters:
        url - New value of property url.
      • getLoginName

        public String getLoginName()
        Getter for property loginName.
        Returns:
        Value of property loginName.
      • setLoginName

        public void setLoginName​(String loginName)
        Setter for property loginName.
        Parameters:
        loginName - New value of property loginName.
      • getPassword

        public String getPassword()
        Getter for property password.
        Returns:
        Value of property password.
      • setPassword

        public void setPassword​(String password)
        Setter for property password.
        Parameters:
        password - New value of property password.
      • getPropertyTable

        public String getPropertyTable()
        Getter for property propertyTable.
        Returns:
        Value of property propertyTable.
      • setPropertyTable

        public void setPropertyTable​(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.
      • getConnection

        public Connection getConnection()
        Getter for property connection.
        Returns:
        Value of property connection.
      • setConnection

        public final void setConnection​(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
      • disconnect

        public void disconnect()
                        throws SQLException
        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

        public void close()
                   throws SQLException
        Closes the connection to the database.
        Throws:
        SQLException - if the database encounters a problem
      • loadValuesFromProperties

        public boolean loadValuesFromProperties​(Properties settings)
        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

        public void storeValuesToProperties​(Properties settings)
        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
      • isCopyable

        public boolean isCopyable()
        Returns:
        true if the connection is copyable