A DataSource object

HCL OneDB™ JDBC Driver extends the standard DataSource interface to allow connection properties (both the standard properties and HCL OneDB environment variables) to be defined in a DataSource object instead of through the URL.

The following table describes how HCL OneDB connection properties correspond to DataSource properties.
HCL OneDB connection property DataSource property Data type Required? Description
HOST host String Yes for client-side JDBC, unless SQLH_TYPE is defined; no for server-side JDBC The IP address or the host name of the computer running the HCL OneDB database server
PORT port int Yes for client-side JDBC, unless SQLH_TYPE is defined The port number of the HCL OneDB database server.
DATABASE database String No, however most applications would want to specify the database. Connections without a database The name of the HCL OneDB database to which you want to connect

If you do not specify the name of a database, a connection is made to the HCL OneDB database server.

USER user String Yes The user name controls (or determines) the session privileges when connected to the HCL OneDB database or database server

Normally, you must specify both user name and password; however, if the user running the JDBC application is trusted by the DBMS, you might omit both.

PASSWORD password String Yes The password of the user

Normally, you must specify both the user name and the password; however, if the user running the JDBC application is trusted by the DBMS, you might omit both.

Specify connection information

If an LDAP (Lightweight Directory Access Protocol) server or sqlhosts file provides the IP address, host name, or port number or service name of the HCL OneDB database server through the SQLH_TYPE property, you do not have to specify them using the standard DataSource properties. For more information, see Dynamically reading the HCL OneDB sqlhosts file.

Connection Properties

For a list of supported connection properties, see HCL OneDB JDBC Driver properties. For a list of HCL OneDB DataSource extensions, which allow you to define connection properties, see DataSource extensions. The driver does not consult the users environment to determine configuration property values.

High-availability data replication

You can use a DataSource object with High-Availability Data Replication. For more information, see Connections to the servers of a high-availability cluster.

Example: Use of a DataSource object in an example program

The following example defines and uses a DataSource object to connect to the database server using key/value pairs as properties:
import com.onedb.jdbcx.OneDBDataSource;
import com.onedb.jdbcx.OneDBParams;

OneDBDataSource ds = new OneDBDataSource();
ds.setProperty(OneDBParams.HOST, "localhost"); // default is localhost
ds.setProperty(OneDBParams.PORT, "9088"); //default is 9088
ds.setProperty(OneDBParams.DATABASE, "sysmaster");  //choose a database to connect to
try(java.sql.Connection con = ds.getConnection("username", "password")) {
   //do database work
}
catch (SQLException e) {
   e.printStackTrace();
}

Example: Setting parameters for a OneDBDataSource object using methods

The following are examples of using methods from the OneDBDataSource object to set parameters for establishing a database connection.

Example 1
OneDBDataSource ds = new OneDBDataSource();
ds.setLockTimeout(65);    // wait up to 65 seconds to obtain alock on the server
int waitMode = ds.getLockTimeout();